We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Printing the document throws DivideByZeroException using .NET

We sometimes run big printing batches (atm a batch of 4000+ documents) after a number of prints, like maybe 1000-2000? We get the following error:

System.DivideByZeroException: Attempted to divide by zero.

at System.Drawing.Printing.PageSettings.get_HardMarginX()

at Aspose.Words.Rendering.AsposeWordsPrintDocument.OnPrintPage(PrintPageEventArgs e)

at System.Drawing.Printing.PrintDocument._OnPrintPage(PrintPageEventArgs e)

at System.Drawing.Printing.PrintController.PrintLoop(PrintDocument document)

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)

at Aspose.Words.Document.Print(String printerName)

This error then keeps on repeating itself while the program is running, if i restart the printer the same documents print fine… Sadly it means i have to keep an eye on the printing software and restart it every now and then.

Untill i get the same error again after X printers (usually a high number, like 1000+) and then it will keep throwing the error on every .Print() call untill the software is restarted.

Hi Folkert,

Thanks for your query. It would be great, If you share some more detail about your issue, how are you printing the documents by using Aspose.Words? Please share your code for investigation purposes.

Sure, i understand my earlier description might be a little slow.

Basically we get data from the database in objects, we loop through these objects and mailmerge them through a custom data source. (this all goes fine so i don’t see a big need to post this code)

Code which might be related to the whole printing problem;

I’ve made this ALOT shorter and turned it into some psuedo code, i hope you don’t mind.

foreach (object record in results)

Document doc = new Document("");
Document subDoc = new Document(“Part1.doc”);
Bookmark bookmark = doc.Range.Bookmarks[“Eiser”];
bookmark.Text = “”;
Helper.InsertDocument(bookmark.BookmarkStart.ParentNode, subDoc);

var anonymousType =
Something = “Variables”,

doc.MailMerge.Execute(new ObjectDataSource(anonymousType));


//Change op Tray3
foreach (Section section in doc.Sections)
section.PageSetup.FirstPageTray = 3;
section.PageSetup.OtherPagesTray = 3;

// set the license for Aspose.BarCode for .NET and Aspose.Pdf.Kit for .NET components
Aspose.BarCodeRecognition.License licenceBarCodeRecognition = new Aspose.BarCodeRecognition.License();

Aspose.Pdf.License licensePdfKit = new Aspose.Pdf.License();

//Check Barcode
MemoryStream byteStream = new MemoryStream(Bytes, 0, verdictBytes.Length);
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(byteStream);

//Check and remove first page if scanpage
string barcode = Common.AsposeHelpers.PDF.Barcode.GetFirst(byteStream);
if (barcode.Length == 13)

//Convert to word doc.
Document convertedPdfDocument = Common.AsposeHelpers.PDF.Words.CreateDocumentFromImages(pdfDocument);

Document original = doc.Clone();

doc.AppendDocument(convertedPdfDocument, ImportFormatMode.KeepSourceFormatting);

//save files

//Add other documents to package
doc.Print(“Printername with spaces and 123 numbers”);
original.Print(“Printername with spaces and 123 numbers”);

Now, important here to note is that everything goes fine, the saving of the document they also look fine etc etc.

Just during the iteration after a big number, like 1000+ iterations we get this exception and it will keep on throwing it untill the application is restarted.

Hi Folkert,

Please accept my apology for late response and thanks for sharing more information. I have tried to reproduce the same problem at my end but not able to reproduce it. Please use the latest version of Aspose.Words for .NET 10.8.0.
It would be great, If you execute a sample program which contain only mail merge and print function at your end and share your finding with us for further investigation.

Hi Folkret,

Thanks for your inquiry.

I’ve taken a closer look into this by inspecting the native code executed when HardMarginX is called. I found that it does indeed perform division and the cause of the error is due to a call to GetDeviceCaps returning zero. This is a method used to find certain attributes of a device context (in this case a printer). This tells us that the error is occurring within native platform code and has nothing to do with Aspose.Words.

Most likely what is happening is that there is some problem that occurs during communication with Windows and the printer which causes this error to occur after so many prints. One thing that doesn’t help is that the native code in the HardMarginX property is wrapped only in a try-finally instead of a try-catch-finally which means the exception propagates up and halts the program instead of being ignored as you see here.

I can suggest a work around to hopefully avoid this problem all together. You can try implementing your own custom print class and making sure that a call to HardMargin properties are wrapped within a try-catch block. Please see the code on the following page which demonstrates creating your own Document printing class. You would instead use printDoc.Print instead of doc.Print in your new code.

You will need to modify the code in OnPrintPage like this:

// Hard code the
margins that will be used if anything goes wrong.<o:p></o:p>

float hardOffsetX = 20;

float hardOffsetY = 20;



hardOffsetX = e.PageSettings.HardMarginX;

hardOffsetY = e.PageSettings.HardMarginY;


catch (Exception) { }

int pageIndex = mCurrentPage - 1;

mDocument.RenderToScale(mCurrentPage, e.Graphics, -hardOffsetX, -hardOffsetY, 1.0f);

Hopefully this helps.


Sorry for my late reply (had a little new years vacation).

The problem i have with this work around is that it isn’t just for one print command that this exception is thrown, but for every print command after the exception first occures. Which means your solution wouldnt potentially create one document that has the wrong offset margins but would in fact create a potential problem for every document printed after the first exception occures.

I’d have to see the results to see if the resulting hardcopy’s would look different but ideally i’d only have the exception happen once every X prints, instead of for every print after X prints. That way we possibly have one in X hardcopy’s that look slightly different (or need to be reprinted) instead of possibly thousands. Since these are legal documents we’re printing this could cause problems.

Like said i’d need to look into the hardcopy results of this implementation and if they vary, if time allows for it i will do this today.

Hi Folkret,

Thanks for this additional information.

We can only hope that if you catch this exception once during printing then it won’t happen again for the next print job. Please let us know how your testing goes and if the exception keeps persisting after the first occurrence.

Otherwise, I’m afraid there isn’t much other choice, you may want to look into reinstalling the .NET framework, reinstall printer drivers etc or you may want to look into performing each print job in a separate application domain. Hopefully this means if the printing fails like this then the next print job in a separate domain does not suffer from the same exception. You can use this idea in conjunction with the work around code above.