private void PrintDocPrintPage(object sender, PrintPageEventArgs e) { this.pageNumber++; Graphics g = e.Graphics; var sourceRect = new Rectangle(this.lastPrintPosition, e.MarginBounds.Size); Rectangle destRect = e.MarginBounds; if ((sourceRect.Height % this.nodeHeight) > 0) { sourceRect.Height -= (sourceRect.Height % this.nodeHeight); } g.DrawImage(this.controlImage, destRect, sourceRect, GraphicsUnit.Pixel); //check to see if we need more pages if ((this.controlImage.Height - this.scrollBarHeight) > sourceRect.Bottom || (this.controlImage.Width - this.scrollBarWidth) > sourceRect.Right) { //need more pages e.HasMorePages = true; } if (this.currentDir == PrintDirection.Horizontal) { if (sourceRect.Right < (this.controlImage.Width - this.scrollBarWidth)) { //still need to print horizontally this.lastPrintPosition.X += (sourceRect.Width + 1); } else { this.lastPrintPosition.X = 0; this.lastPrintPosition.Y += (sourceRect.Height + 1); this.currentDir = PrintDirection.Vertical; } } else if (this.currentDir == PrintDirection.Vertical && sourceRect.Right < (this.controlImage.Width - this.scrollBarWidth)) { this.currentDir = PrintDirection.Horizontal; this.lastPrintPosition.X += (sourceRect.Width + 1); } else { this.lastPrintPosition.Y += (sourceRect.Height + 1); } //print footer Brush brush = new SolidBrush(Color.Black); string footer = this.pageNumber.ToString(NumberFormatInfo.CurrentInfo); var f = new Font(FontFamily.GenericSansSerif, 10f); SizeF footerSize = g.MeasureString(footer, f); var pageBottomCenter = new PointF(x: e.PageBounds.Width / 2, y: e.MarginBounds.Bottom + ((e.PageBounds.Bottom - e.MarginBounds.Bottom) / 2)); var footerLocation = new PointF( pageBottomCenter.X - (footerSize.Width / 2), pageBottomCenter.Y - (footerSize.Height / 2)); g.DrawString(footer, f, brush, footerLocation); //print header if (this.pageNumber == 1 && this.title.Length > 0) { var headerFont = new Font(FontFamily.GenericSansSerif, 24f, FontStyle.Bold, GraphicsUnit.Point); SizeF headerSize = g.MeasureString(this.title, headerFont); var headerLocation = new PointF(x: e.MarginBounds.Left, y: ((e.MarginBounds.Top - e.PageBounds.Top) / 2) - (headerSize.Height / 2)); g.DrawString(this.title, headerFont, brush, headerLocation); } }
private void PrintDocBeginPrint(object sender, PrintEventArgs e) { this.lastPrintPosition = new Point(0, 0); this.currentDir = PrintDirection.Horizontal; this.pageNumber = 0; }