2

我试图从我的 Windows 服务打印一个 pdf 文件。它没有工作。后来我写了一个控制台应用程序来打印一个 pdf 文件。控制台应用程序确实工作了!。Afterwords ii 从服务中调用该控制台应用程序来打印 pdf 文件它没有工作. 为什么“打印”不适用于 Windows 服务?以下是我尝试过的代码片段

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;
    }*/

做了很多google bing yahoo ..没用!!

4

1 回答 1

1

服务通常由不同的帐户运行。我会尝试以用户身份运行服务。可能是系统用户没有映射该打印机的问题。服务安装类如下所示:

[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);
    }
}
于 2013-11-15T10:37:24.293 回答