本文介绍了如何在Windows服务C#中打印pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 通过Windows服务打印pdf正在作为应用程序运行。但是服务失败了。 服务是在安装了打印机的帐户下运行的,但仍然打印失败。 ProcessStartInfo infoPrintPdf = new ProcessStartInfo(); // pathPdf = @D:\ ITC.pdf; infoPrintPdf.FileName = pathPdf; // 这里的打印机名称是硬编码的,但通常我会从所有打印机的组合框中获取此信息 string printerName = System.Configuration.ConfigurationManager.AppSettings [ PrinterName的]的ToString(); // string printerName =HP LaserJet Professional P1606dn; string driverName = System.Configuration.ConfigurationManager.AppSettings [ DriverName ]的ToString(); string portName = System.Configuration.ConfigurationManager.AppSettings [ PORTNAME]的ToString(); string passwordStr = july2016。 // System.Security.SecureString password = new System.Security.SecureString(); // foreach(charc in passwordStr) // password.AppendChar(c); // infoPrintPdf.UserName [email protected]; // infoPrintPdf.Password = password; infoPrintPdf.FileName = System.Configuration.ConfigurationManager.AppSettings [ AcrobatExePath]。ToString(); infoPrintPdf.Arguments = string .Format( / t {0} \{1} \\{2} \\{3} \, pathPdf,printerName,driverName,portName ); infoPrintPdf.CreateNoWindow = true ; infoPrintPdf.UseShellExecute = false ; infoPrintPdf.WindowStyle = ProcessWindowStyle.Hidden; 处理printPdf = new Process(); printPdf.StartInfo = infoPrintPdf; printPdf.Start(); // 此时间取决于打印机,但必须长足以 // 让打印机开始打印 系统.Threading.Thread.Sleep( 10000 ); if (!printPdf.CloseMainWindow()) // CloseMainWindow似乎永远不会成功 printPdf.Kill(); printPdf.WaitForExit(); // Kill AcroRd32.exe printPdf.Close(); // 关闭流程并释放资源 我尝试了什么: ProcessStartInfo infoPrintPdf = new ProcessStartInfo(); // pathPdf = @D:\ ITC.pdf; infoPrintPdf.FileName = pathPdf; // 这里的打印机名称是硬编码的,但通常我会从所有打印机的组合框中获取此信息 string printerName = System.Configuration.ConfigurationManager.AppSettings [ PrinterName的]的ToString(); // string printerName =HP LaserJet Professional P1606dn; string driverName = System.Configuration.ConfigurationManager.AppSettings [ DriverName ]的ToString(); string portName = System.Configuration.ConfigurationManager.AppSettings [ PORTNAME]的ToString(); string passwordStr = july2016。 // System.Security.SecureString password = new System.Security.SecureString(); // foreach(charc in passwordStr) // password.AppendChar(c); // infoPrintPdf.UserName [email protected]; // infoPrintPdf.Password = password; infoPrintPdf.FileName = System.Configuration.ConfigurationManager.AppSettings [ AcrobatExePath]。ToString(); infoPrintPdf.Arguments = string .Format( / t {0} \{1} \\{2} \\{3} \, pathPdf,printerName,driverName,portName ); infoPrintPdf.CreateNoWindow = true ; infoPrintPdf.UseShellExecute = false ; infoPrintPdf.WindowStyle = ProcessWindowStyle.Hidden; 处理printPdf = new Process(); printPdf.StartInfo = infoPrintPdf; printPdf.Start(); // 此时间取决于打印机,但必须长足以 // 让打印机开始打印 系统.Threading.Thread.Sleep( 10000 ); if (!printPdf.CloseMainWindow()) // CloseMainWindow似乎永远不会成功 printPdf.Kill(); printPdf.WaitForExit(); // Kill AcroRd32.exe printPdf.Close(); // 关闭流程并释放资源 解决方案 这样的事情可能会起作用如何以编程方式(C#.NET)将pdf文件直接打印到打印机vishalsbsinha [ ^ ] 它发送到默认打印机 - 相当容易适应 Print a pdf via windows service is working as application. but with service it fails.The service is run with the account where printer is installed but still print fails.ProcessStartInfo infoPrintPdf = new ProcessStartInfo(); //pathPdf = @"D:\ITC.pdf"; infoPrintPdf.FileName = pathPdf; // The printer name is hardcoded here, but normally I get this from a combobox with all printers string printerName = System.Configuration.ConfigurationManager.AppSettings["PrinterName"].ToString(); //string printerName = "HP LaserJet Professional P1606dn"; string driverName = System.Configuration.ConfigurationManager.AppSettings["DriverName"].ToString(); string portName = System.Configuration.ConfigurationManager.AppSettings["portName"].ToString(); string passwordStr = "july2016."; //System.Security.SecureString password = new System.Security.SecureString(); //foreach (char c in passwordStr) // password.AppendChar(c); //infoPrintPdf.UserName = "[email protected]"; //infoPrintPdf.Password = password; infoPrintPdf.FileName = System.Configuration.ConfigurationManager.AppSettings["AcrobatExePath"].ToString(); infoPrintPdf.Arguments = string.Format("/t {0} \"{1}\" \"{2}\" \"{3}\"", pathPdf, printerName, driverName, portName); infoPrintPdf.CreateNoWindow = true; infoPrintPdf.UseShellExecute = false; infoPrintPdf.WindowStyle = ProcessWindowStyle.Hidden; Process printPdf = new Process(); printPdf.StartInfo = infoPrintPdf; printPdf.Start(); // This time depends on the printer, but has to be long enough to // let the printer start printing System.Threading.Thread.Sleep(10000); if (!printPdf.CloseMainWindow()) // CloseMainWindow never seems to succeed printPdf.Kill(); printPdf.WaitForExit(); // Kill AcroRd32.exe printPdf.Close(); // Close the process and release resourcesWhat I have tried:ProcessStartInfo infoPrintPdf = new ProcessStartInfo(); //pathPdf = @"D:\ITC.pdf"; infoPrintPdf.FileName = pathPdf; // The printer name is hardcoded here, but normally I get this from a combobox with all printers string printerName = System.Configuration.ConfigurationManager.AppSettings["PrinterName"].ToString(); //string printerName = "HP LaserJet Professional P1606dn"; string driverName = System.Configuration.ConfigurationManager.AppSettings["DriverName"].ToString(); string portName = System.Configuration.ConfigurationManager.AppSettings["portName"].ToString(); string passwordStr = "july2016."; //System.Security.SecureString password = new System.Security.SecureString(); //foreach (char c in passwordStr) // password.AppendChar(c); //infoPrintPdf.UserName = "[email protected]"; //infoPrintPdf.Password = password; infoPrintPdf.FileName = System.Configuration.ConfigurationManager.AppSettings["AcrobatExePath"].ToString(); infoPrintPdf.Arguments = string.Format("/t {0} \"{1}\" \"{2}\" \"{3}\"", pathPdf, printerName, driverName, portName); infoPrintPdf.CreateNoWindow = true; infoPrintPdf.UseShellExecute = false; infoPrintPdf.WindowStyle = ProcessWindowStyle.Hidden; Process printPdf = new Process(); printPdf.StartInfo = infoPrintPdf; printPdf.Start(); // This time depends on the printer, but has to be long enough to // let the printer start printing System.Threading.Thread.Sleep(10000); if (!printPdf.CloseMainWindow()) // CloseMainWindow never seems to succeed printPdf.Kill(); printPdf.WaitForExit(); // Kill AcroRd32.exe printPdf.Close(); // Close the process and release resources 解决方案 Maybe something like this would work How to programmatically (C# .NET) print a pdf file directly to the printer | vishalsbsinha[^]it sends to the default printer - fairly easy to adapt 这篇关于如何在Windows服务C#中打印pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-17 17:16