To print a document, you add a PrintDocument and a PrintDialog object to a form (see the previous two topics), as I've done in the Printing example on the CD-ROM. Before displaying the Print dialog, you assign the PrintDocument object to the PrintDialog object's Document property. In the Printing example, I'm storing all the printer settings (such as what printer to use and what pages to print) in the print document's PrinterSettings property, so I assign the PrinterSettings object returned by that property to the PrinterSettings property of the PrintDialog object before displaying that dialog. I also set the print dialog's AllowSomePages property to True to allow the user to select a range of pages to print, and use the ShowDialog method to show the dialog box. You can see what the Print dialog looks like for this example in Figure 9.7.
If the user clicked the OK button in the Print dialog, I copy the settings the user specified in that dialog to the document, and then print the document with the Print method:
Private Sub MenuItem5_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MenuItem5.Click PrintDialog1.Document = PrintDocument1 PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings PrintDialog1.AllowSomePages = True If PrintDialog1.ShowDialog = DialogResult.OK Then PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings PrintDocument1.Print() End If End Sub
When you call the PrintDocument object's Print method, this object's BeginPrint event occurs to start the print job, followed by a PrintPage event for each page to print, followed by a EndPrint event at the end of the printing job. You're responsible for keeping track of the pages and printing them. In the PrintPage event, you're passed an object of the PrintPageEventArgs class, which has these members:
Cancel— Gets/sets a value indicating whether the print job should be canceled. Setting this value to True cancels the print job.
Graphics— The Graphics object used to draw the page.
HasMorePages— Gets/sets a value indicating whether an additional page should be printed.
MarginBounds— The rectangular area that represents the portion of the page inside the margins.
PageBounds— The rectangular area that represents the total area of the page.
PageSettings— The page settings for the current page (see "Creating Page Setup Dialogs" later in this chapter for more information on the PageSetting class).
All these properties are very useful—for example, when you're done printing one page, you can set the HasMorePages property to True to indicate that there are more pages yet to print (which means another PrintPage event will occur). To print the two pages of rectangles in the Printing example on the CD-ROM, we'll need to keep track of the current page number, which I do by setting an integer, PageNumber, to 0 when the BeginPrint event happens. Then, in the PrintPage event handler, I increment the page number, use the FillRectangle method of the Graphics object passed to us to draw the rectangles, and set HasMorePages to True if there are more pages to print:
Dim PageNumber As Integer Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, _ ByVal e As System.Drawing.Printing.PrintEventArgs) Handles _ PrintDocument1.BeginPrint PageNumber = 0 End Sub Private Sub PrintDocument1_PrintPage(ByVal sender As _ System.Object, ByVal e As _ System.Drawing.Printing.PrintPageEventArgs) _ Handles PrintDocument1.PrintPage PageNumber += 1 Select Case PageNumber Case 1 e.Graphics.FillRectangle(Brushes.Red, _ New Rectangle(200, 200, 500, 500)) e.HasMorePages = True Case 2 e.Graphics.FillRectangle(Brushes.Blue, _ New Rectangle(200, 200, 500, 500)) e.HasMorePages = False End Select End Sub
This example is not set up to print selected ranges of pages—it just prints the whole document. If you want to handle print ranges, take a look at the PrintDocument PrintRange property, which holds the range of pages to print.
And that's it—our output is sent to the printer. If you want to print text, you can use the use the Graphics object's DrawString method. We'll discuss the Graphics class later in the book; for example, if the font you want to print in is represented by the Font object myFont, you could determine the number of text lines per page this way:
numberLinesPerPage = e.MarginBounds.Height / myFont.GetHeight(e.Graphics)