0 out of 2 rated this helpful - Rate this topic

PrinterSettings Class

Specifies information about how a document is printed, including the printer that prints it, when printing from a Windows Forms application.

System.Object
  System.Drawing.Printing.PrinterSettings

Namespace:  System.Drawing.Printing
Assembly:  System.Drawing (in System.Drawing.dll)
[SerializableAttribute]
public class PrinterSettings : ICloneable

The PrinterSettings type exposes the following members.

  Name Description
Public method PrinterSettings Initializes a new instance of the PrinterSettings class.
Top
  Name Description
Public property CanDuplex Gets a value indicating whether the printer supports double-sided printing.
Public property Collate Gets or sets a value indicating whether the printed document is collated.
Public property Copies Gets or sets the number of copies of the document to print.
Public property DefaultPageSettings Gets the default page settings for this printer.
Public property Duplex Gets or sets the printer setting for double-sided printing.
Public property FromPage Gets or sets the page number of the first page to print.
Public property Static member InstalledPrinters Gets the names of all printers installed on the computer.
Public property IsDefaultPrinter Gets a value indicating whether the PrinterName property designates the default printer, except when the user explicitly sets PrinterName.
Public property IsPlotter Gets a value indicating whether the printer is a plotter.
Public property IsValid Gets a value indicating whether the PrinterName property designates a valid printer.
Public property LandscapeAngle Gets the angle, in degrees, that the portrait orientation is rotated to produce the landscape orientation.
Public property MaximumCopies Gets the maximum number of copies that the printer enables the user to print at a time.
Public property MaximumPage Gets or sets the maximum FromPage or ToPage that can be selected in a PrintDialog.
Public property MinimumPage Gets or sets the minimum FromPage or ToPage that can be selected in a PrintDialog.
Public property PaperSizes Gets the paper sizes that are supported by this printer.
Public property PaperSources Gets the paper source trays that are available on the printer.
Public property PrinterName Gets or sets the name of the printer to use.
Public property PrinterResolutions Gets all the resolutions that are supported by this printer.
Public property PrintFileName Gets or sets the file name, when printing to a file.
Public property PrintRange Gets or sets the page numbers that the user has specified to be printed.
Public property PrintToFile Gets or sets a value indicating whether the printing output is sent to a file instead of a port.
Public property SupportsColor Gets a value indicating whether this printer supports color printing.
Public property ToPage Gets or sets the number of the last page to print.
Top
  Name Description
Public method Clone Creates a copy of this PrinterSettings.
Public method CreateMeasurementGraphics() Returns a Graphics that contains printer information that is useful when creating a PrintDocument.
Public method CreateMeasurementGraphics(Boolean) Returns a Graphics that contains printer information, optionally specifying the origin at the margins.
Public method CreateMeasurementGraphics(PageSettings) Returns a Graphics that contains printer information associated with the specified PageSettings.
Public method CreateMeasurementGraphics(PageSettings, Boolean) Creates a Graphics associated with the specified page settings and optionally specifying the origin at the margins.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetHdevmode() Creates a handle to a DEVMODE structure that corresponds to the printer settings.
Public method GetHdevmode(PageSettings) Creates a handle to a DEVMODE structure that corresponds to the printer and the page settings specified through the pageSettings parameter.
Public method GetHdevnames Creates a handle to a DEVNAMES structure that corresponds to the printer settings.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method IsDirectPrintingSupported(Image) Gets a value indicating whether the printer supports printing the specified image file.
Public method IsDirectPrintingSupported(ImageFormat) Returns a value indicating whether the printer supports printing the specified image format.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method SetHdevmode Copies the relevant information out of the given handle and into the PrinterSettings.
Public method SetHdevnames Copies the relevant information out of the given handle and into the PrinterSettings.
Public method ToString Infrastructure. Provides information about the PrinterSettings in string form. (Overrides Object.ToString().)
Top

Typically, you access a PrinterSettings through PrintDocument.PrinterSettings or PageSettings.PrinterSettings properties to modify printer settings. The most common printer setting is PrinterName, which specifies the printer to print to.

For more information about printing with Windows Forms, see the System.Drawing.Printing namespace overview. If you wish to print from a Windows Presentation Foundation application, see the System.Printing namespace.

The following code example prints a document on the specified printer. The example has three prerequisites:

  • A variable named filePath has been set to the path of the file to print.

  • A method named pd_PrintPage, which handles the PrintPage event, has been defined.

  • A variable named printer has been set to the printer's name.

Use the System.Drawing, System.Drawing.Printing, and System.IO namespaces for this example.


public void Printing(string printer) {
  try {
    streamToPrint = new StreamReader (filePath);
    try {
      printFont = new Font("Arial", 10);
      PrintDocument pd = new PrintDocument(); 
      pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
      // Specify the printer to use.
      pd.PrinterSettings.PrinterName = printer;

      if (pd.PrinterSettings.IsValid) {
         pd.Print();
      } 
      else {	
         MessageBox.Show("Printer is invalid.");
      }
    } 
    finally {
      streamToPrint.Close();
    }
  } 
  catch(Exception ex) {
    MessageBox.Show(ex.Message);
  }
}



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Enumerating all printers
I wrote the following code for enumerating all installed printers and listing some of their properties:

StringBuilder sb = new StringBuilder();
foreach (string printer in PrinterSettings.InstalledPrinters)
{
        sb.AppendLine("Installed printer : " + printer);
        PrinterSettings s = new PrinterSettings();
        s.PrinterName = printer;
        sb.AppendLine("  default? " + s.IsDefaultPrinter);
        sb.AppendLine("  valid? " + s.IsValid);

        if (!s.IsValid)
        {
            sb.AppendLine();
            continue;
        }

        sb.AppendLine("  plotter? " + s.IsPlotter);
        PageSettings ps = s.DefaultPageSettings;
        sb.AppendLine("  default landscape? " + ps.Landscape);
        sb.AppendLine("  default page bounds: " + ps.Bounds.ToString());
        sb.AppendLine("  default paper size: " + ps.PaperSize);
        sb.AppendLine("  default printable area: " + ps.PrintableArea.ToString());
        sb.AppendLine();
 }

Note: the check for IsValid was written because I encountered an exception on the call to IsPlotter for a printer that had returned false for IsValid.