Free Support Forum - aspose.com

Printing PDF files from Windows Service

Hi.


Can i print PDF files from Windows Service with Aspose ?

I need to be able to control tray’s aswell as color options.

I have tried using the following code.

PdfViewer p = new PdfViewer();
p.BindPdf(this.FileName);
System.Drawing.Printing.PrinterSettings ps = p.GetDefaultPrinterSettings();
System.Drawing.Printing.PageSettings pgs = p.GetDefaultPageSettings();
ps.Copies = this.Copies;
pgs.Color = this.printColor;
ps.DefaultPageSettings.Color = this.printColor;
pgs.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0);
ps.PrinterName = this.PrinterName;
p.PrinterJobName = this.PrintName;
p.PrintDocumentWithSettings(pgs, ps);

This code only works aslong as my user is logged into the server with desktop. As soon i log out and the service should print by it self(its setup to run under my account).

I get the following error.

System.Drawing.Printing.InvalidPrinterException: Settings to access printer ‘\optdc02\HP LJ300-400 color MFP M375-M475 PCL 6’ are not valid.
at System.Drawing.Printing.PrinterSettings.GetHdevmodeInternal(String printer)
at System.Drawing.Printing.PrinterSettings.CreateDeviceContext(PageSettings pageSettings)
at System.Drawing.Printing.PageSettings.get_HardMarginX()
at Aspose.Pdf.Facades.PdfViewer.›(PageSettings pageSettings, Single& x, Single& y)
at Aspose.Pdf.Facades.PdfViewer.PrintDocumentWithSettings(PageSettings pageSettings, PrinterSettings printerSettings)
at OptimateDistributionEngine.PDFPrinter.print()

So is there anyway to print PDF from a windows service using Aspose ?

Hi Jes,


Thanks for using our API’s.

It appears that when a user is logged off from system, the access to printer drives is stopped by server executing the application and as our API cannot get hold of print drivers, such issues are generated. However please share some sample project, so that we can further look into this matter.

We are sorry for this inconvenience.

Hi Thanks for the reply.


It seems that this only happens if.

I log onto the server with the user who runs the service. Then when i log off, first and sometimes second try, if they appear very fast fails for each printer. I think maybe what could be happening is that when i log off with the user, the server removes users connection to the printers. So when the service tries to print i have to make new connection so this takes some time and it fails.

I’m not sure above is right this is only my speculations. I solved the issue with

try Print if fails sleep 1 second, then try again. I do a total number of 3 retries before giving up. This have solved my issue.

Hi Jes,


Thanks for sharing your feedback.

We are further looking into this matter and will get back to you soon.

Hi Jes,


Thanks for your patience.

We have further looked into this matter and as stated earlier, the issue does not seem to be related to our API. In fact when a user logs off, the service performing page print is stopped. In order to resolve this issue, you may consider following the instructions specified over following links.