Printer Settings in GDI+ using VB.NET

In this article I will explain about Printer Settings in GDI+.
  • 5953

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 Class

The 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:

Dim prs As PrinterSetting = 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 Property

The 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

        For Each printer As [String] In PrinterSettngs.InstalledPrinters
            Dim str As String = printer.ToString()

The PaperSizes Property

The 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

        Dim prs As PrinterSetting = New PrinterSettings()
        For Each ps As PaperSize In prs.PaperSizes
            Dim str As String = ps.ToString()

The PrinterResolutions Property

The 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

        Dim ps As New PrinterSettings()
        'Set the printer name
        ps.PrinterName = YourPrinterName

        For Each pr As PrinterResolution In ps.PrinterResolutions

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 Properties

The 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

        Dim ps As 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 Property

The 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:

        Dim ps As New PrinterSettings()
        ps.Collate = True

The Copies Property

The 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




Default duplex setting




Single-sided printing


Double-sided, vertical printing

Duplex Printing: A Problem

Duplex printing (the ability to print on both sides of a page) is a feature usually found on higher-end laser and inkjet printers. It is generally found only on more expensive printers because either the printer needs to be able to print on both sides of a sheet, or it must have an internal mechanism to turn the page over and print on the other side.

Let's assume you have a low-end printer and need to print on both sides of the pages. To do this, you would need to create a custom software solution. Let's also assume that you application is printing a 100-page text document. Because the document consists of text alone, this is not too difficult to achieve. You would simply read from a text stream and keep track of whether you have the space to print the next line. If not, you would tell the printer to go to another page. In this scenario you would end up with 100 single-sided pages.

So how do you get double-sided printing? In the tradition of good programming, you cheat, of course! The solution to this problem is to track the page number, and on the first pass print only odd-numbered pages (1, 3, 5, and so on). Once you have done this, display a dialog box that tells you to take all the sheets of paper just printed and reload them into the printer so they will be fed into the printer upside down. Now you can print the even-numbered pages (2,4,6, and so on). Voila! The user gets duplex printing functionality from a cheap printer.

The following code sets the Copies property of PrinterSettings:

        Dim ps As PrinterSetting = New PrinterSettngs()
        'We want 10 copies of our document
        ps.Copies = 10

The IsPlotter Property

The IsPlotter property tells us if the printer we're using is actually a plotter that can accept plotter commands.

The following code snipped indicates whether the printer is a plotter:

        Dim ps As PrinterSetting = New PrinterSettings()

The PrinterName and IsValid Properties

If we print without setting the PrinterName property, our printout will be sent to the default printer. The PrinterName property allows us to specify a printer to use. The IsValid property tells us whether the PrinterName value we have selected represents a valid printer on our system.

Listing 11.9 check if the printer is valid.

LISTING 11.9: Using the IsValid property

        Dim ps As New PrinterSettings()
        ps.PrinterName = ("Invalid Printer Name")
        MessageBox.Show("Is this a Valid printer name?")

The MaximumCopies Property

The MaximumCopies property determines how many copies the printer can print. Some printers do not allow us to print more than one copy at a time.

Listing 11.10 reads the maximum number of copies that a printer can print.

Listing 11.10: Reading the maximum number of copies

        Dim ps As New PrinterSettings()
        MessageBox.Show("Maximum number of copies: ")

The SupportsColor Property

The SupportsColor property tells us whether the current printer supports printing in color. It will return true if the printer supports color printing and false otherwise.

Listing 11.11 reads the value of the SupportsColors property to find out whether a printer supports colors.

LISTING 11.11: Using the SupportsColor property

        Dim ps As New PrinterSettings()
        MessageBox.Show("Does this printer support color:")


Hope the article would have helped you in understanding Printer Settings in GDI+. Read other articles on GDI+ on the website.


More Articles

© 2020 DotNetHeaven. All rights reserved.