This article has been excerpted from book "Graphics Programming with GDI+".Before writing our next printing application, let's examine printer settings. Printer settings specify the properties of a print process, such as the paper size, print quality, number of copies, number of pages, and so on. In this section we will first discuss how to access and set printer settings using the PrinterSettings class properties. Then we will write an application that allows us to read and set printer settings programmatically.The PrinterSettings ClassThe PrinterSettings object is the gateway to reading and setting printer settings. PrinterSettings specifies how a document will be printed during a print process.After creating a PrinterSettings object instance, we usually use the PrintDocument.PrinterSettings or PageSettings.PrinterSettings property to access the PrinterSettings objects corresponding to the PrintDocument and PageSettings objects, respectively. We will discuss these in more detail in a moment.The following code snipped creates a PrinterSettings object: PrinterSetting prs = new PrinterSettings();The PrinterSettings class provides the following 22 properties: CanDuplex, Collate, Copies, DefaultPageSettings, Duplex, FromPage, InstalledPrinters, IsDefaultPrinter, IsPlotter, IsValid, LandScapeAngle, MaximumCopies, MaximumPage, MinimumPage, PaperSizes, PaperSources, PrinterName, PrinterResolutions, PrintRange, PrintToFile, SupportsColor, and ToPage. In the sections that follow, we will discuss each of these properties in turn.The InstalledPrinters PropertyThe InstalledPrinters static property returns the names of all available printers on a machine, including printers available on the network. This property returns all the printer names in a PrinterSettings.StringCollection object.Listing 11.5 iterates through all the available printers on a machine.LISTING 11.5: Getting all installed printers on a machine foreach (String printer in PrinterSettngs.InstalledPrinters) { string str = printer.ToString(); }The PaperSizes PropertyThe PaperSizes property returns the paper sizes supported by a printer. It returns all the paper sizes in a PrinterSettngs.PaperSizeCollection object.Listing 11.6 iterates through all the available paper sizes.LISTING 11.6: Reading all available paper sizes PrinterSetting prs = new PrinterSettings(); foreach (PaperSize ps in prs.PaperSizes) { string str = ps.ToString(); }The PrinterResolutions PropertyThe PrinterResolutions property returns all the resolutions supported by a printer. It returns all the printer resolutions in a PrinterSettings.PrinterResolutionsCollection object that contains PrinterResolution object.Listing 11.7 reads the printer resolution and adds them to a ListBox control. Here YourPrinterName is the name of the printer you want to use. If you do not set a printer name, the default printer will be used.LISTING 11.7: Getting printer resolution PrinterSettings ps = new PrinterSettings(); //Set the printer name ps.PrinterName = YourPrinterName; foreach (PrinterResolution pr in ps.PrinterResolutions) { listBox2.Items.Add(pr.ToString()); }The PrinterResolution class, which represents the resolution of a printer, is used by the PrinterResolutions and PrinterResolution properties of PrinterSettings to get and set printer resolutions. Using these two properties, we can get all the printer resolutions available on a printer. We can also use it to set the printing resolution for a page.The PrinterResolution class has three properties: Kind, X, and Y. The Kind property is used to determine whether the printer resolution is the PrinterResolutionKind enumeration type or Custom. If it's Custom, the X and Y properties are used to determine the printer resolution in the horizontal and vertical directions, respectively in dots per inch. If the Kind property is not Custom, the value of X and Y each is -1.The CanDuplex and Duplex PropertiesThe CanDuplex property is used to determine whether a printer can print on both sides of a page. If so, we can set the Duplex property to true to print on both sides of page.Listing 11.8 determines whether your printer can print on both sides of a page. If your program responds true, you have a very good printer.LISTING 11.8: Using the CanDuplex property PrinterSettings ps = new PrinterSettings(); MessageBox.Show("Supports Duplex"); MessageBox.Show("Answer = " + ps.CanDuplex.ToString());The Duplex enumeration specifies the printer's duplex settings, which are used by PrinterSettings. The members of the Duplex enumeration are described in Table 11.1.The Collate PropertyThe Collate property (both get and set) is used only if we choose to print more than one copy of a document. If the value of Collate is true, and entire copy of the document will be printed before the next copy is printed. If the value is false, all copies of page 1 will be printed, then all copies of page 2, and so on.The code snipped that follows sets the Collate property of PrinterSettings to true: PrinterSettings ps = new PrinterSettings(); ps.Collate = true;The Copies PropertyThe Copies property (both get and set) allows us to enter the number of copies of a document that we want to print. Not all printers support this feature (in which case this setting will be ignored). TABLE 11.1: Duplex members
Member
Description
Default
Default duplex setting
Horizontal
Double-sided,horizontal printing
Simplex
Single-sided printing
Vertical
Double-sided, vertical printing