.netCoders Contact Us
Search:

Printing

There are several controls that are involved in adding Print capability to your application.
  • PrintDocument
  • PrintDialog
  • PrintPreviewDialog
  • PrintPreviewControl
  • PageSetupDialog
The PrintDocument class is used to start the printing process. The PrintDialog allows the user to select the printer to print to, along with other printer options. The PageSetupDialog sets paper, orientation, and margins. The PrintPreviewDialog and PrintPreviewControl controls handle print previewing.

PrintDocument

As mentioned, the PrintDocument class drives the printing process. Typically, the steps you will go through are:
  1. Create an instance of the PrintDocument class, or drop the control onto your form
  2. Set properties about how to print
  3. Handle the PrintPage event where you specify the printing output
  4. Call the Print method to start printing
This class has 4 events that you can handle.
  1. BeginPrint - Occurs just before the document is about to be printed.
  2. EndPrint - Occurs after the document has been printed.
  3. PrintPage - Occurs for each page printed. Here is where you indicated what to print.
  4. QueryPageSettings - Occurs before each page is printed, allowing you to change PageSettings.
The following code sample shows a button click event that starts the printing process. The BeginPrint and EndPring event handlers display message boxes to the user to indicate the status of the print job. The Print event handler indicates what to print.
private void button1_Click(object sender, System.EventArgs e)
{
    //Start the Printing process
    printDocument1.Print();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    //Indicate what to print
    e.Graphics.DrawEllipse(new Pen(Color.Red), 90, 90, 100, 100);
    e.Graphics.DrawEllipse(new Pen(Color.Red), 310, 90, 100, 100);
    e.Graphics.DrawArc(new Pen(Color.Red), 90, 280, 320, 200, 0, 180);
    
    //Indicate no more pages
    e.HasMorePages = false;
}

private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
    //Alert user that print job is starting
    MessageBox.Show("Print Job starting...");
}

private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
    //Alert user that print job is complete
    MessageBox.Show("Print Job complete!");
}"

private void printDocument1_QueryPageSettings(object sender, System.Drawing.Printing.QueryPageSettingsEventArgs e)
{
    //Do nothing
}
Notice that we set the HasMorePages property of the PrintPageEventArgs argument to false. If you have additional pages to print, be sure to set this property to true. Otherwise, only page 1 will print, and then the EndPrint event will fire.

PrintDialog

The PrintDialog is the standard dialog that is launched when a user clicks print. This allows he/she to select the printer, and set any additional printer options.

The process for showing the print dialog, and then printing if the user clicks OK, is accomplished with the following code.
private void btnPrint_Click(object sender, System.EventArgs e)
{
    //Create a Print Dialog
    PrintDialog printDialog1 = new PrintDialog();
    printDialog1.Document = printDocument1;

    //Show the Dialog
    DialogResult result = printDialog1.ShowDialog();

    //If user clicked OK, print the document
    if (result == DialogResult.OK)
    {
        printDocument1.Print();
    }
}
Note that we set the PrintDialog's Document property to the PrintDocument object. This provides PrinterSettings to the dialog. If you fail to do this, or set the PrinterSettings directly, an exception will be thrown:

Additional Resources