PrintSettings: System.ComponentModel.Win32Exception (0x80004005): The operation completed successfully

Hi,

Our app is installed with 100+ customers who print all day long. Several of them are now reporting the error below which often occurs randomly but can end up becoming continuous.

System.ComponentModel.Win32Exception (0x80004005): The operation completed successfully
at System.Drawing.Printing.PrinterSettings.GetHdevmodeInternal(String printer)
at System.Drawing.Printing.PrinterSettings.GetHdevmodeInternal()
at System.Drawing.Printing.PrinterSettings.GetHdevmode(PageSettings pageSettings)
at System.Drawing.Printing.PrintController.OnStartPrint(PrintDocument document, PrintEventArgs e)
at System.Drawing.Printing.StandardPrintController.OnStartPrint(PrintDocument document, PrintEventArgs e)
at System.Drawing.Printing.PrintController.Print(PrintDocument document)
at System.Drawing.Printing.PrintDocument.Print()
at Aspose.Words.Document.Print(PrinterSettings printerSettings, String documentName)
at Aspose.Words.Document.Print(PrinterSettings printerSettings)

We have looked into this for a while and during our investigations we were able get the occasional random error such as this one in our own code.

System.ComponentModel.Win32Exception (0x80004005): The operation completed successfully
at System.Drawing.Printing.PrinterSettings.GetHdevmodeInternal(String printer)
at System.Drawing.Printing.PrinterSettings.GetModeField(ModeField field, Int16 defaultValue, IntPtr modeHandle)
at System.Drawing.Printing.PrinterSettings.get_Collate() 

This helped us to find this post:
http://stackoverflow.com/questions/1400783/printdocument-print-results-in-win32exception-the-operation-completed-successful

The post points out (among other things) a bug deep in .NET with:
DllImport("winspool.drv", CharSet=CharSet.Auto, SetLastError=true)
public static extern int DocumentProperties(...);

We have worked around the issue in our code by ignoring the error. Unfortunately the error still occurs in the Aspose Words code.

The post above also points to http://blogs.msdn.com/b/dsui_team/archive/2013/06/24/printing-from-a-windows-service.aspx. This post states “System.Drawing is not supported in a service or web application”. It adds that code should be using the Win32 GDI APIs when printing from a Windows Service.

Is there any alternative way in Aspose.Words to print without going through the System.Drawing code?

With kind regards,

Doug

Hi Doug,

Thanks
for your inquiry. Please use the code mentioned in the following
article to be able to print a Word document from an ASP.NET or Windows Service application
using Aspose.Words and the XpsPrint API:
https://docs.aspose.com/words/net/print-a-document-programmatically-or-using-dialogs/

Hope this helps you. Please let us know if you have any more queries.