8.12 Retrieve Information About the Installed Printers


Problem

You need to retrieve a list of available printers.

Solution

Read the names in the InstalledPrinters collection of the System.Drawing.Printing.PrinterSettings class.

Discussion

The PrinterSettings class encapsulates the settings for a printer and information about the printer. For example, you can use the PrinterSettings class to determine supported paper sizes, paper sources, and resolutions and check for the ability to print color or double-sided (or duplexed ) pages. In addition, you can retrieve default page settings for margins, page orientation, and so on.

The PrinterSettings class provides a static InstalledPrinters collection, which includes the name of every printer that's installed on the computer. If you want to find out more information about the settings for a specific printer, you simply need to create a PrinterSettings instance and set the PrinterName property accordingly .

The following code shows a Console application that finds all the printers that are installed on a computer and displays information about the paper sizes and the resolutions supported by each one.

 using System; using System.Drawing.Printing; public class ListPrinters {     private static void Main(string[] args) {              foreach (string printerName in PrinterSettings.InstalledPrinters) {                      // Display the printer name.             Console.WriteLine("Printer: {0}", printerName);             // Retrieve the printer settings.             PrinterSettings printer = new PrinterSettings();             printer.PrinterName = printerName;             // Check that this is a valid printer.             // (This step might be required if you read the printer name             // from a user-supplied value or a registry or configuration file             // setting.)             if (printer.IsValid) {                              // Display the list of valid resolutions.                 Console.WriteLine("Supported Resolutions:");                 foreach (PrinterResolution resolution in                   printer.PrinterResolutions) {                     Console.WriteLine("  {0}", resolution);                 }                  Console.WriteLine();                 // Display the list of valid paper sizes.                 Console.WriteLine("Supported Paper Sizes:");                                  foreach (PaperSize size in printer.PaperSizes) {                                      if (Enum.IsDefined(size.Kind.GetType(), size.Kind)) {                         Console.WriteLine("  {0}", size);                     }                 }                 Console.WriteLine();             }         }         Console.ReadLine();     } } 

Here's the type of output this utility displays:

 Printer: HP LaserJet 5L Supported Resolutions:   [PrinterResolution High]   [PrinterResolution Medium]   [PrinterResolution Low]   [PrinterResolution Draft]   [PrinterResolution X=600 Y=600]   [PrinterResolution X=300 Y=300] Supported Paper Sizes:   [PaperSize Letter Kind=Letter Height=1100 Width=850]   [PaperSize Legal Kind=Legal Height=1400 Width=850]   [PaperSize Executive Kind=Executive Height=1050 Width=725]   [PaperSize A4 Kind=A4 Height=1169 Width=827]   [PaperSize Envelope #10 Kind=Number10Envelope Height=950 Width=412]   [PaperSize Envelope DL Kind=DLEnvelope Height=866 Width=433]   [PaperSize Envelope C5 Kind=C5Envelope Height=902 Width=638]   [PaperSize Envelope B5 Kind=B5Envelope Height=984 Width=693]   [PaperSize Envelope Monarch Kind=MonarchEnvelope Height=750 Width=387] Printer: Generic PostScript Printer . . . 

You don't need to take this approach when creating an application that provides printing features. As you'll see in recipe 8.13, you can use PrintDialog to prompt the user to choose a printer and its settings. The PrintDialog class can automatically apply its settings to the appropriate PrintDocument without any additional code.

Note  

You can print a document in almost any type of application. However, your application must include a reference to the System.Drawing.dll assembly. If you are using a project type in Visual Studio .NET that wouldn't normally have this reference (such as a Console application), you must add it.




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net