G e t A c q u a i n t e d w i t h t h e P r i n t e r S e t t i n g s C l a s s
By Michael McIntyre
The purpose of the System.Drawing.Printing.PrinterSettings class is obtain information about the printers installed on a computer and change printer settings. This article provides an introduction to the PrinterSettings class.
The PrinterSettings class can be used to determine supported paper sizes, paper sources, and resolutions. Use it to check for the ability to print color or double-sided pages. Retrieve and change default page settings such as margins and page orientation. These are just a few samples of what the PrinterSettings class encapsulates.
Key to using the PrinterSettings class is the use of its InstalledPrinters collection. This collection contains the name of every printer installed on a computer. Installed printers are accessed in code through this collection.
Here are some examples that show how to use the PrinterSettings class and its InstalledPrinters collection.
Example 1:
’ Declare a variable of type PrinterSettings
’ named aPrinterSettings.
Dim aPrinterSettings As PrinterSettings
’ Declare a variable of type String named aPrinterName.
Dim aPrinterName As String
’ Loop through the installed printers.
For Each aPrinterName In PrinterSettings.InstalledPrinters
' Report the name of the printer.
Console.WriteLine(aPrinterName)
Next
Result:
HP DeskJet 930C/932C/935C
Fax
Note: Your result will vary, it will be a list of the printers installed on your computer.
Example 2: Get Printer Information
’ Declare and instantiate a PrinterSettings object.
Dim aPrinterSettings As New PrinterSettings()
’ Inspect one of the installed printers.
’ Using one of the printers found in Example 1.
aPrinterSettings.PrinterName = CType(“HP DeskJet 930C/932C/935C”, String)
‘’ Report information about the printer.
’ Printer Name
Console.WriteLine("Printer Name: " & aPrinterSettings.PrinterName)
’ Supported Printer Resolutions
Console.WriteLine(“Supported Printer Resolutions:”)
Dim aPrinterResolution As PrinterResolution
For Each aPrinterResolution In aPrinterSettings.PrinterResolutions
Console.WriteLine(" " & aPrinterResolution.ToString & vbCrLf)
Next
’ Supported Paper Sizes
Console.WriteLine(“Supported Paper Sizes:”)
Dim aPaperSize As PaperSize
For Each aPaperSize In aPrinterSettings.PaperSizes
Console.WriteLine(" " & aPaperSize.ToString & vbCrLf)
Next
’ Printer’s DefaultPageSettings
Console.WriteLine(vbCrLf & "Default Printer Settings: " & vbCrLf)
Console.WriteLine(aPrinterSettings.DefaultPageSettings.ToString & vbCrLf)
Result:
Printer Name: HP DeskJet 930C/932C/935C
Supported Printer Resolutions:
[PrinterResolution High]
[PrinterResolution Medium]
…
etc.
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]
…
etc.
Default Printer Settings:
[PageSettings: Color=True, Landscape=False,
Margins=[Margins Left=100 Right=100 Top=100 Bottom=100],
PaperSize=[PaperSize Letter Kind=Letter Height=1100 Width=850],
PaperSource=[PaperSource Automatically Select Kind=FormSource], PrinterResolution=[PrinterResolution X=300 Y=300]]
Example 3: Change the Print Range
’ Declare and instantiate a PrinterSettings object.
Dim aPrinterSettings As New PrinterSettings()
’ Using one of the printers found in Example 1.
aPrinterSettings.PrinterName = CType(“HP DeskJet 930C/932C/935C”, String)
’ Change Page Range to be Printed
aPrinterSettings.FromPage = 2
aPrinterSettings.ToPage = 3
To learn more about the PrinterSettings class click here -> PrinterSettings Class