I’m following MSDN’s "how to" document on printing multiple pages: How to: Print a Multi-Page Text File in Windows Forms
I’ve turned the example on that page into a project (tried Visual Studio 2010 and 2012) and found that it works as expected when printing a small amount of pages, but when printing a large amount (9 or so pages) it starts rendering the beginning pages as blank (page one and two are blank, the next 15 are correct, etc.).
Can anyone confirm this behaviour? I don’t see what could be causing this and there are no exceptions thrown.
Here is the section of code that I believe contains the issue:
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
int charactersOnPage = 0;
int linesPerPage = 0;
// Sets the value of charactersOnPage to the number of characters
// of stringToPrint that will fit within the bounds of the page.
e.Graphics.MeasureString(stringToPrint, this.Font,
e.MarginBounds.Size, StringFormat.GenericTypographic,
out charactersOnPage, out linesPerPage);
// Draws the string within the bounds of the page
e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
e.MarginBounds, StringFormat.GenericTypographic);
// Remove the portion of the string that has been printed.
stringToPrint = stringToPrint.Substring(charactersOnPage);
// Check to see if more pages are to be printed.
e.HasMorePages = (stringToPrint.Length > 0);
}
I don’t believe any of the datatypes are being overflowed. I’ve tried this with different printers but each have the same result.
Note: I’ve tried .NET Framework 4 and 4.5 with the same result.
It seems that the MSDN example does not work as it should. It may be due to the fact that the MarginBounds Rectangle is integer-based as opposed to float-based. Keeping track of the Y-position as a float and using this value within the MeasureString and DrawString methods resolves the issue. I found this by examining a different MSDN printing example.
Here is the relevant code:
This does not account for word wrapping like the previous example but that can be implemented with relative ease.