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

If you are developing software that allows printing, it is often important to modify the output depending upon the selected printer and its capabilities. In many cases it is acceptable to leave this to the Windows operating system to handle. However, occasionally you may need to obtain information about the available printers and change the printing functionality accordingly. For example, you might want to implement your own shading algorithm when outputting images to a device that only supports monochrome printing.

One option is to use the PrinterSettings class. This type allows you to access lots of information about any of the connected printers, plotters or some other output devices. These other devices include those that allow you to print to a PDF or XPS file or to send information to other programs, such as Microsoft OneNote. As well as reading information about printers, you can also use this class to modify your application's printed output.

Getting a List of Installed Printers

One of the most common tasks relating to printer information is obtaining a list of all of the available printing devices. To get such a list you can use the InstalledPrinters property. This is a static property that returns a StringCollection holding the names of the devices. You can use a foreach loop to enumerate the printer names.

To demonstrate, create a new console application. The PrinterSettings class is found in the System.Drawing assembly, so add a reference to System.Drawing.dll to your project. Also add the following using directive to simplify the use of the class:

using System.Drawing.Printing;

Try running the following code, which reads the property and outputs all of the printer names.

foreach (string printer in PrinterSettings.InstalledPrinters)
{
    Console.WriteLine(printer);
}

/* EXAMPLE OUTPUT

Send To OneNote 2010
Samsung CLP-300 Series
Microsoft XPS Document Writer
Fax

*/

Obtaining a PrinterSettings Instance

When you want more information than just the printer names, you must create a PrinterSettings object and link it to a printer. The simplest way to do this is to use the default constructor, which is actually the only available constructor. Once instantiated, the new object holds the details for the current default printer.

PrinterSettings settings = new PrinterSettings();

To interrogate any other printer you can set the PrinterName property to the name of the device that you are interested in. This must be one of the names from the InstalledPrinters collection. For example, to investigate the "Samsung CLP-300" seen in the previous example you would set up an object as follows:

PrinterSettings settings = new PrinterSettings();
settings.PrinterName = "Samsung CLP-300 Series";

This unusual way of configuring the PrinterSettings instance is prone to errors. If you provide the name of a non-existent printer, the properties of the PrinterSettings object will be meaningless. To ensure that this does not happen, you should always check the IsValid property after changing the name. If the printer details are valid, the property returns true.

PrinterSettings settings = new PrinterSettings();
settings.PrinterName = "Samsung CLP-300 Series";
Console.WriteLine(settings.IsValid);    // True

settings.PrinterName = "Not a real printer";
Console.WriteLine(settings.IsValid);    // False
24 April 2013