Name

PrintDocument

Synopsis

This class encapsulates a document to be printed.

On construction, the DefaultPageSettings and PrinterSettings represent the default system printer, but you could create a set of PageSettings and assign them to the DefaultPageSettings property, and/or a set of PrinterSettings, which you assign to the PrinterSettings property if you want to customize the printer output. You can also modify individual properties in the existing settings (such as the PrinterSettings.Copies to change the number of copies to print). You can also set the DocumentName to a display name for the print job. You’d see this in the printer queue or a status dialog.

Once the settings are to your satisfaction, you can set a PrintController. The default is a PrintControllerWithStatusDialog, but you can choose a StandardPrintController or a PreviewPrintController. Call Print() to send the document to the printer.

You’re not done yet. To determine what to print, you have to handle several events raised by the document. BeginPrint is raised when printing is started for the document. Then, the PrintPage event provides the System.Drawing.Graphics object on which to draw your page, along with a bunch of other settings that allow you to format the output for the page dimensions and page settings (see the PrintPageEventArgs for details). If you have more pages to print, you can set the HasMorePages property to true; otherwise, set it to false and the document printing can come to an end. When this ...

Get .NET Windows Forms in a Nutshell 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.