为什么静默打印pdf文件在Windows服务C#中不起作用? [英] Why printing pdf file silently is not working in a windows service C#?

查看:148
本文介绍了为什么静默打印pdf文件在Windows服务C#中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过Windows服务静默打印文件。如果我在initialisecomponent()事件中运行打印功能,服务打印工作正常。



Windows服务间隔3分钟。我已经附加了托管的Windows服务并尝试调试。然后还打印不起作用。文件生成成功。



我的代码如下:

I am unable to print file silently via a windows service. If I am running the print function in the initialisecomponent() event of the service printing works fine.

Windows service is having 3 minutes interval. I have attached the windows service hosted and tried debugging. Then also print is not working. File is generated successfully.

My code is as below:

#region SendPDFToPrinter
       //Added on 17.06.2016  for printing PDF silently
       public void SendPDFToPrinter(string pathPdf)
       {
           try
           {
               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();
               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
           }
           catch (Exception ex)
           {

           }


       }
       //End
       #endregion





我的尝试:



我的代码如下:

#region SendPDFToPrinter

//于17.06.2016添加,用于静默打印PDF

public void SendPDFToPrinter(string pathPdf)

{

尝试

{

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();

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();



//这个时间取决于打印机,但必须足够长到

//让打印机开始打印

System.Threading.Thread.Sleep(10000);



if(!printPdf.CloseMainWindow())// CloseMainWindow似乎永远不会成功

printPdf.Kill();

printPdf.WaitForExit(); //杀死AcroRd32.exe



printPdf.Close(); //关闭流程并释放资源

}

catch(例外情况)

{



}





}

//结束

#endregion



What I have tried:

My code is as below:
#region SendPDFToPrinter
//Added on 17.06.2016 for printing PDF silently
public void SendPDFToPrinter(string pathPdf)
{
try
{
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();
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
}
catch (Exception ex)
{

}


}
//End
#endregion

推荐答案

系统帐户没有设置打印机。您需要在计算机上创建一个帐户才能运行您的服务。您还需要使用此帐户登录并设置打印机才能使用。一旦你有了这个设置,你可以设置你的服务来使用这个帐户。



另外,就像上面建议的那样,拥有一个空的catch块只会隐藏任何错误你无法知道出现了什么问题以及错误信息是什么。
The System account doesn't hav eany printers setup. You need to create an account on the machine to run your service under. You also need to login with this account and setup a printer to use first. Once you have that setup, you can setup your service to use this account.

Also, like was suggested above, having an empty catch block will just hide any errors from you making it impossible to know if something went wrong and what the error message is.


这篇关于为什么静默打印pdf文件在Windows服务C#中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆