BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Input / Output
.NET 1.1+

Interrogating Printers with PrinterSettings

When creating an application that can print information, or send drawings to a plotter, it may be necessary to obtain information about all connected printing devices. Most of the information that is required can be read from the PrinterSettings class.

PrinterSettings Properties

Once configured, a PrinterSettings object contains a wealth of information about a printing device, all accessible through its properties. Some of these properties are simple Boolean values that specify whether or not the device supports particular functionality. These include:

  • CanDuplex. This property returns true if the printer supports duplex, or double-sided, printing. If false, you may only print on one side of the paper.
  • Collate. When set to true, printouts are collated. This means that if you request more than one copy of a multi-page document, the entire first copy of the document will be printed, followed by the entire second copy and so on. When false, every copy of the first page is printed, followed by every copy of the second page, etc. You can obtain the current setting by reading this property and change it by setting the property value.
  • IsDefaultPrinter. This read-only property returns true if the printer being examined is the default printer.
  • IsPlotter. The IsPlotter property allows you to differentiate between printers and other devices that output raster images, and plotters or devices that produce vector graphics. If the property is true, the device is vector based. If false, the device creates raster output.
  • PrintToFile. This property indicates whether information will be sent to a printer port or to a file. True indicates that output will be sent to a file.
  • SupportsColor. The final Boolean property is true if the device supports printing in colour and false if it only permits monochrome output.

The PrinterSettings class includes a number of numeric properties that allow you to determine how documents will be generated.

  • Copies. This sixteen bit integer property allows you to set or check the number of copies that will be produced when printing. The value defaults to one but can be increased for printers that support the generation of multiple copies.
  • FromPage / ToPage. These two properties allow you to control the range of pages of a document that will be printed. The actual printed pages depend upon this and other settings controlled at the time of printing.
  • LandscapeAngle. The LandscapeAngle property returns one of three values. If zero, the printer does not support printing with a landscape orientation. The other options are 90 or 270, indicating the number of degrees of rotation applied to landscape pages. This allows you to understand the direction of rotation.
  • MaximumCopies. This property returns the maximum number of copies that can be printed by the device in a single print job. For most printers this will be several hundred copies. However, some printers, particularly dummy printers such as those used for PDF output, only support producing a single copy.
  • MaximumPage / MinimumPage. These two properties allow you to obtain or set the widest possible range of pages that can be printed. These values can be used to prevent the user from selecting invalid pages in a printing dialog box.

The remaining properties are more complex, as they do not use the native types of C#.

  • DefaultPageSettings. This property returns a PageSettings object holding the default page settings for the printer. It includes properties that define the default page size, paper source, printing resolution and many other settings.
  • Duplex. This property returns the current duplex setting for printers that support duplex printing. If the CanDuplex property returns true, you can set this to a value from the Duplex enumeration. Four options are available:
    • Default. Use the printer's default duplex option.
    • Simplex. Sets the printer to create one-sided output only.
    • Horizontal. The printer prints double-sided flipping the page horizontally. This means that the tops of two opposing pages are at the same edge of the sheet. Most books use horizontal duplex, allowing you to flip pages and have all of the text appear the correct way up.
    • Vertical. The printer prints double-sided flipping the page vertically. This means that the tops of two opposing pages are at opposite edges of the paper. Many calendars use vertical duplex, allowing you to lift a page from the bottom of the calendar to the top when changing from one month to the next.
  • PaperSources. Returns a collection of PaperSource objects, each describing a paper source, such as a paper tray, cassette or manual feed. These provide limited information but are useful when selecting which source to use. Each PaperSource instance has a name in its SourceName property. It also includes details of the type of source in the Kind property. This holds a value from the PaperSourceKind enumeration, which has the following options:
    • AutomaticFeed
    • Cassette
    • Custom
    • Envelope
    • FormSource
    • LargeCapacity
    • LargeFormat
    • Lower
    • Manual
    • ManualFeed
    • Middle
    • SmallFormat
    • TractorFeed
    • Upper
  • PrinterResolutions. This property returns a collection of PrinterResolution objects, each describing a resolution supported by the device. Each includes a Kind property, which returns a value from the PrinterResolutionKind enumeration. These tell you whether the resolution in question is high, medium, low or draft for the printer. The property may also indicate a custom value, in which case the X and Y properties give the horizontal and vertical resolutions respectively, measured in dots per inch (DPI).

The following code brings a few of these properties together by printing some of the details of all of the known printers.

foreach (string printer in PrinterSettings.InstalledPrinters)
{
    PrinterSettings settings = new PrinterSettings();
    settings.PrinterName = printer;

    Console.Write(settings.PrinterName);
    Console.WriteLine(settings.IsDefaultPrinter ? " (Default)" : "");
    Console.WriteLine(settings.IsPlotter ? "Vector Images" : "Raster Images");
    Console.WriteLine(settings.SupportsColor ? "Colour" : "Mono");
    Console.WriteLine(settings.CanDuplex ? "Duplex" : "Single-Sided");

    foreach (PrinterResolution resolution in settings.PrinterResolutions)
    {
        Console.WriteLine(resolution);
    }

    Console.WriteLine(new string('-', 70));
}
24 April 2013