【发布时间】:2016-11-15 06:04:58
【问题描述】:
大家好,我正在尝试打印 pdf 文件。为此,我创建了一个 Windows 窗体应用程序。如果我使用系统的默认打印机,我的应用程序会打印 pdf。但问题是,当我尝试使用从下拉列表中选择的打印机打印 PDF 文件时,我的代码不起作用。
private bool SendToPrinter(string filePath, string filename)
{
bool Status = false;
string PrinterName;
string PaperName;
PrinterName = "";
try
{
PrinterName = Convert.ToString(cmbPrinterList.SelectedItem);
// Set the printer.
AddPrinterConnection(PrinterName);
SetDefaultPrinter(PrinterName);
//Print the file with number of copies sent.
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.Arguments = "\"" + PrinterName + "\"";
info.FileName = @filePath + @"\" + filename;
info.CreateNoWindow = false;
info.WindowStyle = ProcessWindowStyle.Hidden;
info.UseShellExecute = true;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(2);
if (false == p.CloseMainWindow())
p.Kill();
Status = true;
}
catch (Exception ex)
{
}
return Status;
}
//Set the added printer as default printer.
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetDefaultPrinter(string Name);
//Add the printer connection for specified pName.
[DllImport("winspool.drv")]
public static extern bool AddPrinterConnection(string pName);
public bool viewTabOrder = true;
【问题讨论】:
-
请说明您的错误。你的代码不起作用是什么意思。
-
@active92 我想说我的代码没有用我选择的打印机打印 pdf 文件
-
@active92 但使用系统默认打印机