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
public void SendPDFToPrinter(string pathPdf)
{
try
{
ProcessStartInfo infoPrintPdf = new ProcessStartInfo();
infoPrintPdf.FileName = pathPdf;
string printerName = System.Configuration.ConfigurationManager.AppSettings["PrinterName"].ToString();
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();
System.Threading.Thread.Sleep(10000);
if (!printPdf.CloseMainWindow())
printPdf.Kill();
printPdf.WaitForExit();
printPdf.Close();
}
catch (Exception ex)
{
}
}
#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