protected override void OnPrintPage(PrintPageEventArgs e)
{
base.OnPrintPage(e);
// Transfer to the point units.
e.Graphics.PageUnit = GraphicsUnit.Point;
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
// Get the number of the thumbnail placeholders across and down the paper sheet.
Size thumbCount = GetThumbCount(mPagesPerSheet);
// Calculate the size of each thumbnail placeholder in points.
// Papersize in .NET is represented in hundreds of an inch. We need to convert this value to points first.
SizeF thumbSize = new SizeF(
HundredthsInchToPoint(mPaperSize.Width) / thumbCount.Width,
HundredthsInchToPoint(mPaperSize.Height) / thumbCount.Height);
// Select the number of the last page to be printed on this sheet of paper.
int pageTo = System.Math.Min(mCurrentPage + mPagesPerSheet - 1, mPageTo);
// Loop through the selected pages from the stored current page to calculated last page.
for (int pageIndex = mCurrentPage; pageIndex <= pageTo; pageIndex++)
{
// Calculate the column and row indices.
int columnIdx;
int rowIdx = System.Math.DivRem(pageIndex - mCurrentPage, thumbCount.Width, out columnIdx);
// Define the thumbnail location in world coordinates (points in this case).
float thumbLeft = columnIdx * thumbSize.Width;
float thumbTop = rowIdx * thumbSize.Height;
// Render the document page to the Graphics object using calculated coordinates and thumbnail placeholder size.
// The useful return value is the scale at which the page was rendered.
float scale = mDocument.RenderToSize(pageIndex, e.Graphics, thumbLeft, thumbTop, thumbSize.Width, thumbSize.Height);
// Draw the page borders (the page thumbnail could be smaller than the thumbnail placeholder size).
if (mPrintPageBorders)
{
// Get the real 100% size of the page in points.
SizeF pageSize = mDocument.GetPageInfo(pageIndex).SizeInPoints;
// Draw the border around the scaled page using the known scale factor.
e.Graphics.DrawRectangle(Pens.Black, thumbLeft, thumbTop, pageSize.Width * scale, pageSize.Height * scale);
// Draws the border around the thumbnail placeholder.
e.Graphics.DrawRectangle(Pens.Red, thumbLeft, thumbTop, thumbSize.Width, thumbSize.Height);
}
}
// Re-calculate next current page and continue with printing if such page resides within the print range.
mCurrentPage = mCurrentPage + mPagesPerSheet;
e.HasMorePages = (mCurrentPage <= mPageTo);
}
// ExEnd:OnPrintPage