Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
C#
#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


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
Posted
Updated 29-Jun-16 5:24am
v2
Comments
Duncan Edwards Jones 29-Jun-16 9:27am    
What user is the windows service running as - and does that user have access to the printer and file system?
Ema112 29-Jun-16 9:40am    
local system account and access is there to print. the issue is while printing from the hosted service. Else simply in initailisecomponent it works.
Richard Deeming 29-Jun-16 10:32am    
Two things to look at:

1) The "local system" account probably doesn't have the correct printer set up. You might need to copy some registry keys around to make it work.

2) Your empty catch block is silently ignoring all errors, which will make it impossible to diagnose any problems. You should add some code to log the exceptions, so that you'll at least have a clue where to start looking.

1 solution

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.
 
Share this answer
 
Comments
Ema112 30-Jun-16 8:31am    
I changed the service account with my account credentials.I have hosted the service in my machine itself. I have admin rights. But still the print fails while running the windows service. All else goes fine. If I debug by running in the initialisecomponent() of windows service the print functionality works. No issues as such seen. The printer used is a network printer.
Dave Kreskowiak 30-Jun-16 8:38am    
When this code works as a normal app do you have to do anything at all to get it to print, other than launch the code you posted?
Dave Kreskowiak 30-Jun-16 8:42am    
Looking at your other question on this, is this service running on a machine when Acrobat Reader is installed?
Ema112 4-Jul-16 0:04am    
Yes Service is running on a machine with Acrobat Reader installed. Code while working as normal app nothing is being done and it prints fine.But as a service on hosting print fails. No error shown.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900