Search code examples
c#visual-studio-2008printingwindows-servicesprocessstartinfo

How to print pdf file using windows service


I tried to print a pdf file from my windows service.It didnt work.Later i wrote a console app to print a pdf file.Console app did work!.Afterwords i i called that console app from service to print pdf file it didn'work. Why is that "print" doesnt work with windows service? following are the code snippets i tried

1.Used adobe reader:
                PdfReportGeneration.Log logs = new PdfReportGeneration.Log();
                logs.writeLog("PrintDocument filepath:-" + filepath);

                Process process = new Process();
                process.StartInfo.FileName = filepath;
                process.StartInfo.UseShellExecute = true;
                process.StartInfo.Verb = "printTo";
                process.StartInfo.Arguments = "HP LaserJet P1005";
                process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                process.Start();
                process.WaitForInputIdle();
                process.Kill();

2. Used foxit reader/adobe reader both didnt work
    string sArgs = " /t \"" + filepath + "\" \"" + "HP LaserJet P1005" + "\"";
    System.Diagnostics.ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = @"C:\Program Files\Foxit Software\Foxit Reader\Foxit Reader.exe";
    startInfo.Verb = "printTo";
    startInfo.Arguments = sArgs;
    startInfo.CreateNoWindow = true;
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    System.Diagnostics.Process proc = Process.Start(startInfo);
    proc.WaitForExit(100000); // Wait a maximum of 10 sec for the process to finish
    if (!proc.HasExited)
    {
        proc.Kill();
        proc.Dispose();
       // return false;
    }*/

Done a lots of google bing yahoo.. no use!!


Solution

  • Service is usually run by different account. I would try to run service as user. Could be a problem that system user doesn't have mapped that printer. Service install class would look like this:

    [RunInstaller(true)]
    public class ServiceInstall : Installer
    {
        public ServiceInstall()
        {
            ServiceInstaller serviceInstaller = new ServiceInstaller();
            ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
    
            serviceProcessInstaller.Account = ServiceAccount.User;
            serviceProcessInstaller.Username = "User";
            serviceProcessInstaller.Password = "Password";
    
            serviceInstaller.DisplayName = "Some Service";
            serviceInstaller.StartType = ServiceStartMode.Automatic;
            serviceInstaller.ServiceName = "Some Service";
    
            this.Installers.Add(serviceProcessInstaller);
            this.Installers.Add(serviceInstaller);
        }
    }