我一直在使用visual c#开发一个应用程序,只需按一下按钮即可打印现有的pdf文件。
我一直在使用System.Diagnostics.Process
方法进行打印,但在Windows 8环境中无法正常工作,因为“Printto”命令似乎无法在那里工作。
我想使用一个替代方案,例如System.Drawing.Printing.PrintDocument
可能有一个与文本文件一起使用的示例,但是当我尝试使用pdf时,它会打印随机字符。
我的谷歌搜索似乎是空的或我没有输入正确的关键字,但我需要一个解决方案,不仅打印pdf到目标打印机,但可以确定打印机是否准备好,离线或缺纸以及错误捕获。
请告知我,我也愿意查看任何可推荐的SDK或第三方路线。
编辑:添加了我正在使用的代码段:
string defFile = (Path.Combine(GlobalVars.pdfPath, tkt_no + "_DEF.pdf"));
string rwPrinter = "";
if (GlobalVars.useDefaultPrinter == false)
{
foreach (string strPrinter in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
if (strPrinter.StartsWith("ZDesigner"))
{
rwPrinter = strPrinter;
break;
}
}
}
if (jobdo.Equals("print"))
{
Process process = new Process();
//process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.FileName = defFile;
if (rwPrinter.Length > 0)
{
process.StartInfo.Verb = "printto";
//process.StartInfo.Verb = (Path.Combine(System.Windows.Forms.Application.StartupPath, "printto.exe"));
process.StartInfo.Arguments = "\"" + rwPrinter + "\"";
}
else
{
process.StartInfo.Verb = "print";
}
try
{
process.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
process.WaitForInputIdle();
//while (process.MainWindowHandle == IntPtr.Zero)
//{
// Thread.Sleep(20);
// process.Refresh();
//}
Thread.Sleep(7000);
try
{
process.Kill();
}
catch { }
// close any occurrences of Adobe Reader that may not close through a citrix environment regardless
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
if (p.ProcessName.Equals("AcroRd32"))
{
ObjectQuery sq = new ObjectQuery
("Select * from Win32_Process where ProcessID = '" + p.Id + "'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(sq);
foreach (ManagementObject oReturn in searcher.Get())
{
string[] o = new string[2];
oReturn.InvokeMethod("GetOwner", (object[])o);
if(o[0] != null)
if(o[0].Equals(System.Environment.UserName))
p.Kill();
}
}
}
if (rwPrinter == "")
{
rwPrinter = "the default printer";
}
else
MessageBox.Show("Ticket " + tkt_no + " was printed to " + rwPrinter + ".");
}