The PageSettings Class Printing

177 Protected Overrides Sub OnPrintPageByVal e As PrintPageEventArgs MyBase.OnPrintPagee Draw text to the printer graphics device. Dim rect As Rectangle = e.MarginBounds e.Graphics.DrawStringHello, Printer, m_fnt, Brushes.Black, 0, 0 Indicate that there are no more pages. e.HasMorePages = False End Sub End Class

4.7.4 Choosing a Printer

The code given in Examples Ex am ple 4- 13 and Ex am ple 4- 14 merely prints to the default printer. To allow the user to select a specific printer and set other printer options, pass the PrintDocument object to a PrintDialog object and call the PrintDialog objects ShowDialog method. The ShowDialog method displays a PrintDialog dialog box shown in Figur e 5- 19 in Chapt er 5 . When the user clicks OK in the PrintDialog dialog box, the ShowDialog method sets the appropriate values in the given PrintDocument object. The PrintDocument objects Print method can then be called to print the document to the selected printer. Here is the code: Create the PrintDocument object and the dialog box object. Dim pd As New HelloPrintDocument Dim dlg As New PrintDialog Pass the PrintDocument object to the dialog box object. dlg.Document = pd Show the dialog box. Be sure to test the result so that printing occurs only if the user clicks OK. If dlg.ShowDialog = DialogResult.OK Then Print the document. pd.Print End If This code assumes the presence of the HelloPrintDocument class defined in Ex am ple 4- 13 or Ex am ple 4- 14 . Note that the HelloPrintDocument class itself does not need to be modified to support choosing a printer.

4.7.5 The PageSettings Class

As mentioned earlier, the PrintPageEventArgs object passed to the OnPrintPage method has a PageSettings property that holds a PageSettings object. This object holds the settings applicable to printing a single page. The properties of the PageSettings class are: Bounds Represents a rectangle that specifies the full area of the page, including the area outside the margins. This is the same value found in the PageBounds property of the PrintPageEventArgs class. The syntax of the Bounds property is: Public ReadOnly Property Bounds As System.Drawing.Rectangle Color Indicates whether the page should be printed in color. The syntax of the Color property is: Public Property Color As Boolean 178 Landscape Indicates whether the page is being printed in landscape orientation. The syntax of the Landscape property is: Public Property Landscape As Boolean Margins Indicates the size of the margins. The syntax of the Margins property is: Public Property Margins As System.Drawing.Printing.Margins The Margins class has four properties, Left, Top, Right, and Bottom, each of which is an Integer expressing the size of the respective margin. PaperSize Indicates the size of the paper. The syntax of the PaperSize property is: Public Property PaperSize As System.Drawing.Printing.PaperSize The PaperSize class has four properties: Width An Integer expressing the width of the paper. This is the same value found in the Width member of the Bounds property of the PageSettings object. Height An Integer expressing the height of the paper. This is the same value found in the Height member of the Bounds property of the PageSettings object. Kind An enumeration of type PaperKind expressing the size of the paper in terms of standard named sizes, such as Letter and Legal . PaperName A string giving the name of the paper size, such as Letter and Legal . PaperSource Indicates the paper tray from which the page will be printed. The syntax of the PaperSource property is: Public Property PaperSource As System.Drawing.Printing.PaperSource The PaperSource class has two properties: Kind 179 An enumeration of type PaperSourceKind expressing the paper source in terms of standard names, such as Lower and Upper . SourceName A string giving the name of the paper source, such as Lower and Upper . PrinterResolution Indicates the resolution capability of the printer. The syntax of the PrinterResolution property is: Public Property PrinterResolution As _ System.Drawing.Printing.PrinterResolution The PrinterResolution class has three properties: X An Integer expressing the horizontal resolution of the printer in dots per inch. Y An Integer expressing the vertical resolution of the printer in dots per inch. Kind An enumeration of type PrinterResolutionKind expressing the resolution mode. The values of this enumeration are Draft , Low , Medium , High , and Custom . PrinterSettings Indicates the settings applicable to the printer being used. The syntax of the PrinterSettings property is: Public Property PrinterSettings As _ System.Drawing.Printing.PrinterSettings The PrinterSettings class is described in the next section.

4.7.6 The PrinterSettings Class