gpt4 book ai didi

c# - 如何打印用户选择的文档?

转载 作者:行者123 更新时间:2023-11-30 20:53:47 24 4
gpt4 key购买 nike

我想使用文件对话框选择一个文件,然后使用 PrintDocument.Print 方法打印所选文件。

下面是一些代码,是我要完成的部分实现:

     using System;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;


namespace InstalledAndDefaultPrinters
{
class Program
{


static void Main(string[] args)
{
string filename="";
foreach (string printer in PrinterSettings.InstalledPrinters)
Console.WriteLine(printer);
var printerSettings = new PrinterSettings();
Console.WriteLine(string.Format("The default printer is: {0}", printerSettings.PrinterName));

Console.WriteLine(printerSettings.PrintFileName);
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Open File Dialog";
fdlg.InitialDirectory = @"C:\ ";
fdlg.RestoreDirectory = true;
fdlg.ShowDialog();
Console.WriteLine(fdlg.Title);
if (fdlg.ShowDialog() == DialogResult.OK)
{
filename = String.Copy(fdlg.FileName);
}
Console.WriteLine(filename);

PrintDialog printdg = new PrintDialog();
PrintDocument pd_doc = new PrintDocument();
printdg.ShowDialog();
if (printdg.ShowDialog() == DialogResult.OK)
{

此时我想打印所选文件。

            pd_doc.Print();
}
}

我上面的代码显然不能满足我的需要。哪种替代方法可以引导我朝着正确的方向前进?

最佳答案

您可以使用以下代码片段解决该问题。它可以按您的意愿工作。

       private void button1_Click(object sender, EventArgs e)
{
PrintDialog printdg = new PrintDialog();
if (printdg.ShowDialog() == DialogResult.OK)
{
PrintDocument pd = new PrintDocument();
pd.PrinterSettings = printdg.PrinterSettings;
pd.PrintPage += PrintPage;
pd.Print();
pd.Dispose();
}
}
private void PrintPage(object o, PrintPageEventArgs e)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(@"C:\Users\therath\Desktop\372\a.jpg");
// You can replace your logic @ here to load the image or whatever you want
Point loc = new Point(100, 100);
e.Graphics.DrawImage(img, loc);
}

关于c# - 如何打印用户选择的文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19686123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com