Basic Print Events

All print controller implementations rely on the print document's print events to gather the drawing commands into the graphics object, either to spool to the printer or to show on the screen:

void printDocument1_PrintPage(object sender, PrintPageEventArgs e) {
  // Draw to the e.Graphics object

  Graphics g = e.Graphics;
  using( Font font = new Font("Lucida Console", 72) ) {
    g.DrawString("Hello,\nPrinter", font, ...);
  }
}

Notice that this sample PrintPage event handler creates a font only for printing. For a single page, this code is fine, because it creates the font and then reclaims the font resources when the printing is complete. However, if we're printing more than one page, it's wasteful to create the font anew on each ...

Get Windows Forms Programming in C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.