Click here to Skip to main content
15,919,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


C#
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 = "veena.cjohn@chn.nestgroup.net";
             //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


What I have tried:

C#
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 = "veena.cjohn@chn.nestgroup.net";
                //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
Posted
Updated 1-Aug-16 1:33am
v2
Comments
F-ES Sitecore 1-Aug-16 6:23am    
Services can't interact with the desktop so if your process shows any dialog boxes etc it isn't going to work, you need a printing method that does not require desktop access.
Ema112 1-Aug-16 6:44am    
service is running with a user account credentials where the network printer is installed. Still printing is not invoking while running as a windows service. This code works fine as a windows application or by calling a function with in initialise component Of windows service.

1 solution

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
 
Share this answer
 
Comments
Ema112 2-Aug-16 0:12am    
The print is working on the machine in windows service on which the printer is installed. But when tried in another machine the print works as an application where I give the network printer name. But when hosted as windows service it is not working. I gave the service admin credentials on which printer is installed.
Garth J Lancaster 2-Aug-16 0:32am    
how are you specifying the network printer name ?

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