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:
-
Create an instance of the PrintDocument class, or drop the control onto your
form
-
Set properties about how to print
-
Handle the PrintPage event where you specify the printing output
-
Call the Print method to start printing
This class has 4 events that you can handle.
-
BeginPrint - Occurs just before the document is about to be printed.
-
EndPrint - Occurs after the document has been printed.
-
PrintPage - Occurs for each page printed. Here is where you indicated what to
print.
-
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)
{
   printDocument1.Print();
}
private
void
printDocument1_PrintPage(object
sender,
System.Drawing.Printing.PrintPageEventArgs
e)
{
   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);
 
   e.HasMorePages
=
false;
}
private
void
printDocument1_BeginPrint(object
sender,
System.Drawing.Printing.PrintEventArgs
e)
{
   MessageBox.Show("Print Job starting...");
}
private
void
printDocument1_EndPrint(object
sender,
System.Drawing.Printing.PrintEventArgs
e)
{
   MessageBox.Show("Print Job complete!");
}"
private
void
printDocument1_QueryPageSettings(object
sender,
System.Drawing.Printing.QueryPageSettingsEventArgs
e)
{
 }
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)
{
   PrintDialog
printDialog1
=
new
PrintDialog();
 printDialog1.Document
=
printDocument1;
   DialogResult
result
=
printDialog1.ShowDialog();
   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: