Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/5608bf3a30d8ca686e5b to your computer and use it in GitHub Desktop.
Save bjoerntx/5608bf3a30d8ca686e5b to your computer and use it in GitHub Desktop.
/*------------------------------------------------------
** OnPrintPage
** description: This method overrides the standard
** OnPrintPage method and implements the
** rendering of more pages on a single
** sheet
**
** parameters: standard PrintPageEventArgs
**----------------------------------------------------*/
protected override void OnPrintPage(PrintPageEventArgs e)
{
base.OnPrintPage(e);
// set the internal unit to point to be compatible with Text Control's units
e.Graphics.PageUnit = GraphicsUnit.Point;
// retrieve the thumbnail rows and columns for selected number of pages
ThumbnailGrid szThumbnailValuePair = GetThumbnailValuePair(m_pagesPerSheet);
// set the thumbnail size (page size divided by number of cols / rows)
SizeF szfThumbnailSize = new SizeF(
HundredthsOfAnInchToPoint(m_paperSize.Width) / szThumbnailValuePair.Columns,
HundredthsOfAnInchToPoint(m_paperSize.Height) / szThumbnailValuePair.Rows);
// get the last thumbnail page that is printed on the current page
int iLastPage = Math.Min(m_currentPage + (int)m_pagesPerSheet - 1, m_toPage);
// iterate through the selected pages
for (int iCurrentPage = m_currentPage; iCurrentPage <= iLastPage; iCurrentPage++) {
// get the page from TextControl
TXTextControl.Page txPage = m_textControl.GetPages()[iCurrentPage + 1];
// get the current row and column
int iCurrentColumn;
int iCurrentRow = Math.DivRem(iCurrentPage - m_currentPage,
szThumbnailValuePair.Columns, out iCurrentColumn);
PointF pfThumbnailLocation = new PointF(
iCurrentColumn * szfThumbnailSize.Width,
iCurrentRow * szfThumbnailSize.Height);
// create the Metafile image from current page in TextControl
// using the Pages collection
Metafile mfPageImage = txPage.GetImage(TXTextControl.Page.PageContent.All);
// calculate the scale factor
float fScaleFactor =
(m_textControl.Sections[txPage.Section].Format.Landscape == true) ?
(szfThumbnailSize.Width / txPage.Bounds.Width) :
(szfThumbnailSize.Height / txPage.Bounds.Height);
// draw the Text Control page onto the existing PrintDocument Graphics
// at the calculated location and size
e.Graphics.DrawImage(mfPageImage, new RectangleF(pfThumbnailLocation.X,
pfThumbnailLocation.Y,
txPage.Bounds.Width * fScaleFactor,
txPage.Bounds.Height * fScaleFactor));
// print page borders
if (m_drawPageBorders)
e.Graphics.DrawRectangle(Pens.LightGray,
pfThumbnailLocation.X,
pfThumbnailLocation.Y,
szfThumbnailSize.Width,
szfThumbnailSize.Height);
}
// more pages?
m_currentPage = m_currentPage + (int)m_pagesPerSheet;
e.HasMorePages = (m_currentPage <= m_toPage);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment