You want the user to indicate some basic paper-related settings for a printed document.
Use the PageSetupDialog class to prompt the user for these basic settings. The following code displays the Page Setup dialog for a basic print document:
Dim pageSetup As New PageSetupDialog pageSetup.Document = New Printing.PrintDocument pageSetup. ShowDialog()
The PageSetupDialog's ShowDialog() method presents the user with the basic Page Setup dialog shown in Figure 11-3. Its initial settings are based on the default printer, or the printer you have specified as the active printer.
The PageSetupDialog class encapsulates a complete form that lets the user set the page size, margins, source, and orientation for an upcoming print job. Normally, you prompt for these settings for a specific document by setting the Document property to a valid PrintDocument object. However, you can also call this form generically by setting its PrinterSettings and PageSettings properties to valid PrinterSettings and PageSettings objects, and setting the printer name to your intended target (if different from the default):
Figure 11-3. The Page Setup dialog
Dim pageSetup As New Forms.PageSetupDialog pageSetup.PageSettings = New Printing.PageSettings pageSetup. PrinterSettings = New Printing.PrinterSettings pageSetup.PrinterSettings.PrinterName = "\\MySystem\MyPrinter" pageSetup.ShowDialog()
Once set, you can assign these PrinterSettings and PageSettings objects to the matching properties in your PrintDocument object:
' ----- Assumes a Printing.PrintDocument object named ' targetDocument. targetDocument.PrinterSettings = pageSetup.PrinterSettings targetDocument.DefaultPageSettings = pageSetup.PageSettings
The PageSetupDialog class also comes in a Windows Formsbased control variation (see Figure 11-4). You can add this control and a related PrintDocument control to your form and display the page settings that way, but it works just the same. You assign the PrintDocument control to the PageSetupDialog's Document property, and then call the PageSetupDialog's ShowDialog() method. It's the exact same code that appears in this recipe's solution; only the declarations of the PageSetupDialog and PrintDocument objects have moved from your source code to the form's surface.
Figure 11-4. The control version of the PageSetupDialog class