从Windows服务打印pdf [英] Printing a pdf from a windows service

查看:235
本文介绍了从Windows服务打印pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在过去的几天里一直在与这个问题作斗争,无法弄清楚如何解决它。我有一个Windows服务,从网络套接字接收信息,然后生成一个PDF文件,我需要在仓库中自动打印。我正在使用pdfsharp来创建文件,并尝试使用print方法,但我不能让它作为服务工作。它在Windows窗体程序中工作正常,但服务中的相同代码不起作用。据我所知,这个过程似乎没有启动acrobat reader程序...虽然我从未收到任何错误....



我已经尝试过看adobe SDK,但似乎无法让它工作。有没有人能够成功地从Windows服务打印pdf之前可能能给我一些指示?我已经尝试了以下所有但没有运气:

Hi Everyone,

I've been fighting with this problem for the past couple of day's now and cannot figure out how to solve it. I have a windows service that receives information from a network socket and then generates a PDF file that I need to automatically print in the warehouse. I'm using pdfsharp to create the files, and have tried using the print method, but I can't get it to work as a service. It works fine in a windows form program, but the same code in a service doesn't work. From what I can tell, it doesn't appear that the process is ever starting the acrobat reader program...although I never receive any errors....

I've then tried looking at the adobe SDK but cannot seem to get that to work either. Has anyone been able to successfully print a pdf from a windows service before that might be able to give me some pointers? I've tried all of the following with no luck:

AcroPDF pdfPrinter = new AcroPDF();

      pdfPrinter.LoadFile(filePath);
      pdfPrinter.Print();




//print the document
     PdfFilePrinter.AdobeReaderPath = @"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe";
      PdfFilePrinter makeOutput = new PdfFilePrinter(_orderNumber + ".pdf");

//    makeOutput.PrinterName = "mySharp";
           makeOutput.WorkingDirectory = SavePath;

           makeOutput.Print();




string pdfArguments = string.Format(" /t " + "\"" + SavePath + "\\" + _orderNumber + ".pdf \" " + "\"Microsoft XPS Document Writer\"");
       string pdfPrinterLocation =  @"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe";

       ProcessStartInfo newProcess = new ProcessStartInfo(pdfPrinterLocation, pdfArguments);
       newProcess.CreateNoWindow = true;
       newProcess.RedirectStandardOutput = true;
       newProcess.UseShellExecute = false;

       Process pdfProcess = new Process();
       pdfProcess.StartInfo = newProcess;
       pdfProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
       pdfProcess.Start();
       pdfProcess.WaitForExit();

推荐答案

好吧,我改变了过程以启动福昕阅读器,它就像一个冠军!我想这只是Adobe不喜欢的东西?



哦......好吧......至少它正在工作!哇!



对于其他可能遇到此问题的人来说,这是一个与我的本地用户帐户一起安装的Windows服务,无法访问与本地桌面的交互。



Well, I changed the process over to fire up Foxit Reader and it worked like a champ! I guess it was just something that Adobe doesn't like?

Oh well...at least it's working! whew!

For anyone else that might run into this issue, it's a windows service that is installed with my local user account w/o access to interaction with the local desktop.

string pdfArguments = string.Format(" /t " + "\"" + SavePath + "\\" + _orderNumber + ".pdf\" " + "\"mySharp\"");
      //string pdfPrinterLocation = @"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe";
      string pdfPrinterLocation = @"C:\Program Files (x86)\Foxit Software\Foxit Reader\Foxit Reader.exe";

      ProcessStartInfo newProcess = new ProcessStartInfo(pdfPrinterLocation, pdfArguments);
      newProcess.CreateNoWindow = true;
      newProcess.RedirectStandardOutput = true;
      newProcess.UseShellExecute = false;

      Process pdfProcess = new Process();
      pdfProcess.StartInfo = newProcess;
      pdfProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
      pdfProcess.Start();
      pdfProcess.WaitForExit();
       }


检查您的服务管理单元。它可能就像打开允许服务与桌面交互属性一样简单。您可以通过在特定服务的上下文菜单中选择属性菜单,然后导航到登录选项卡来实现此目的。我当然已经对此进行了测试,但这是一个预感我基于几年前我做过的另一个项目,我必须与运行表单应用程序进行交互。
Check your services snap-in. It may be as simple as turning on the "Allow service to interact with desktop" property. You can get to it by selecting the "Properties" menu on the context menu for you particular service and then navigating to the Log On tab. I certainly have tested this, but it is a hunch I have based on a different project I did a couple years ago where I had to interact with running forms applications.


Hey Marcus,



不幸的是,没有骰子:/我认为它确实与在无桌面中运行的服务有关?环境,但将帐户更改为本地服务并检查允许服务与桌面交互似乎没有做任何不同的事情。我正在使用procmon来查看acrord32.exe是否曾经启动过,它看起来甚至没有尝试。



更新

好​​的,如果我使用第三块代码(我自己定义并启动进程)Acrord32.exe正在启动并正在传递看似正确的命令行参数。我可以从procmon中取出该字符串,将其粘贴到命令窗口中,然后打印就好了。尽管如此,即使使用复选标记以允许与桌面交互,它也无法正常工作。它确实看起来像其他人一样幸运,但作为解决方案...我想知道它是否与windows 7 x64有关?
Hey Marcus,

Unfortunately, no dice :/ I believe it does have something to do with the service running in a "no desktop?" environment, but changing the account to the local service and checking allow service to interact with desktop didn't appear to do anything different. I'm using procmon to see if acrord32.exe ever starts and it doesn't look like it's even trying.

UPDATE
Ok, if I use the third block of code (where I'm defining and starting the process myself) Acrord32.exe is starting and is being passed what appears to be the correct command line parameters. I can take that string out of procmon, paste it into a command window and it prints just fine. Still, even with the check mark to allow interaction with the desktop it's not working though. It does look like other have had luck with that as a solution though...I wonder if it's something with windows 7 x64?


这篇关于从Windows服务打印pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆