Export (0) Print
Expand All

PrinterSettings Class

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

Namespace: System.Drawing.Printing
Assembly: System.Drawing (in system.drawing.dll)

[SerializableAttribute] 
public class PrinterSettings : ICloneable
/** @attribute SerializableAttribute() */ 
public class PrinterSettings implements ICloneable
SerializableAttribute 
public class PrinterSettings implements ICloneable
Not applicable.

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);
  }
}


public void Printing(String printer)
{
    try {
        streamToPrint = new StreamReader(filePath);
        try {
            printFont = new Font("Arial", 10);
            PrintDocument pd = new PrintDocument();
            pd.add_PrintPage(new PrintPageEventHandler(pd_PrintPage));

            // Specify the printer to use.
            pd.get_PrinterSettings().set_PrinterName(printer);
            if (pd.get_PrinterSettings().get_IsValid()) {
                pd.Print();
            }
            else {
                MessageBox.Show("Printer is invalid.");
            }
        }
        finally {
            streamToPrint.Close();
        }
    }
    catch (System.Exception ex) {
        MessageBox.Show(ex.get_Message());
    }
} //Printing

System.Object
  System.Drawing.Printing.PrinterSettings

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft