DekGenius.com
[ Team LiB ] Previous Section Next Section

PrintDocumentmarshal by reference, disposable

System.Drawing.Printing (system.drawing.dll)class

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 happens, EndPrint is raised and you can clean up. All these events support the Cancel property to abandon printing.

Immediately before the PrintPage event, the framework raises the QueryPageSettings event. You can use this to modify the settings on a page-by-page basis (by modifying the QueryPageSettingsEventArgs.PageSettings property), without having to inject code into your existing PrintPage handler.

public class PrintDocument : System.ComponentModel.Component {
// Public Constructors
   public PrintDocument();
// Public Instance Properties
   public PageSettings DefaultPageSettings{set; get; }
   public string DocumentName{set; get; }
   public PrintController PrintController{set; get; }
   public PrinterSettings PrinterSettings{set; get; }
// Public Instance Methods
   public void Print();
   public override string ToString();  // overrides System.ComponentModel.Component
// Protected Instance Methods
   protected virtual void OnBeginPrint(PrintEventArgs e);
   protected virtual void OnEndPrint(PrintEventArgs e);
   protected virtual void OnPrintPage(PrintPageEventArgs e);
   protected virtual void OnQueryPageSettings(QueryPageSettingsEventArgs e);
// Events
   public event PrintEventHandler BeginPrint;
   public event PrintEventHandler EndPrint;
   public event PrintPageEventHandler PrintPage;
   public event QueryPageSettingsEventHandler QueryPageSettings;
}

Hierarchy

System.Object System.MarshalByRefObject System.ComponentModel.Component(System.ComponentModel.IComponen, System.IDisposable) PrintDocument

Returned By

System.Windows.Forms.PageSetupDialog.Document, System.Windows.Forms.PrintDialog.Document, System.Windows.Forms.PrintPreviewControl.Document, System.Windows.Forms.PrintPreviewDialog.Document

Passed To

PrintController.{OnEndPage(), OnEndPrint(), OnStartPage(), OnStartPrint()}, System.Windows.Forms.PageSetupDialog.Document, System.Windows.Forms.PrintDialog.Document, System.Windows.Forms.PrintPreviewControl.Document, System.Windows.Forms.PrintPreviewDialog.Document

    [ Team LiB ] Previous Section Next Section