Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
4.67/5 (2 votes)
See more:
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();
Posted
Updated 23-Jun-16 5:59am
Comments
Member 12642169 3-Aug-16 8:07am    
How print on defalut print By this article

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();
       }
 
Share this answer
 
Comments
Member 12642169 3-Aug-16 8:06am    
If i want to print on default printer. how i will do?
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.
 
Share this answer
 
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?
 
Share this answer
 
v2
Comments
Pavan_N 6-Jul-12 11:51am    
I too facing similar kind of issue. Can u help me out please?

Regards,
Pavan
Hey Pavan,

To be honest it's been a while since I've messed with this but I'll see what I can do. How far along did you get? Where is it bombing out?
 
Share this answer
 
Comments
Pavan_N 9-Jul-12 2:44am    
Hi,
i tried with Ghpostscript, rundll, foxit, acrobat,clprint, etc etc... All were working perfectly for my server side printing if i run my code under debug mode. i.e., from visual studio. But if i deploy it in my IIS. obviusly it runs under a network service. so i implemented impersonation. even though the service is getting started but just hangs . i can see it in my task manager.

Regards,
pavan
compninja25 9-Jul-12 8:30am    
Hi Pavin,

Instead of running the service as network service have you tried 'local service' or defining a user with administrative privileges? It sounds like it might be an authentication issue.

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