The PrintPageEventArgs Class Printing

175 PrintDocument objects PrintPage event. If this is not done, any code that has registered for this event will not receive it. This is not an issue in this small example, but its nevertheless good programming practice. The next three lines of code are responsible for handling the printing: Draw text to the printer graphics device. Dim fnt As New FontArial, 10, FontStyle.Regular, _ GraphicsUnit.Point e.Graphics.DrawStringHello, Printer, fnt, Brushes.Black, 0, 0 fnt.Dispose This code draws some text to the Graphics object provided through the PrintPageEventArgs parameter. Everything you learned in the previous section about the Graphics object is applicable here. Finally, since were printing just one page in our example program, PrintPageEventArgs.HasMorePages is set to False : Indicate that there are no more pages. e.HasMorePages = False This line indicates to the Print method that the end of the document has been reached. If more pages need to be printed, the OnPrintPage method should set the HasMorePages property to True .

4.7.2 The PrintPageEventArgs Class

The PrintPageEventArgs class is declared in the System.Drawing.Printing namespace as: Public Class PrintPageEventArgs Inherits System.EventArgs Its properties are: Cancel An indication of whether the print job is being canceled. This property is set to True by the printing system if the user cancels the print job. Code in the OnPrintPage method can read this value and take any appropriate action. However, programmatically setting this property back to False does not resume the print job. On the other hand, programmatically setting it to True does cancel the print job, even if the user has not clicked the Cancel button. The syntax of the Cancel property is: Public Property Cancel As Boolean Graphics The Graphics object that represents the page surface. The syntax of the Graphics property is: Public ReadOnly Property Graphics As System.Drawing.Graphics HasMorePages The mechanism for the OnPrintPage method to indicate to the printing system whether there are more pages to be printed after the current page. The OnPrintPage method should set this property to True when there are more pages to print and to False when there are no more pages to print. The syntax of the HasMorePages property is: Public Property HasMorePages As Boolean 176 MarginBounds A rectangle that specifies the area of the page that is within the document margins i.e., the area of the page on which rendering should occur. The syntax of the MarginBounds property is: Public ReadOnly Property MarginBounds As System.Drawing.Rectangle PageBounds A rectangle that specifies the full area of the page, including the area outside the margins. The syntax of the PageBounds property is: Public ReadOnly Property PageBounds As System.Drawing.Rectangle PageSettings The page settings that apply to the page currently being printed. The syntax of the PageSettings property is: Public ReadOnly Property PageSettings As _ System.Drawing.Printing.PageSettings The PageSettings class is described later in this section.

4.7.3 The OnBeginPrint and OnEndPrint Methods