PageSettings.Margins Property

 

Gets or sets the margins for this page.

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

public Margins Margins { get; set; }

Property Value

Type: System.Drawing.Printing.Margins

A Margins that represents the margins, in hundredths of an inch, for the page. The default is 1-inch margins on all sides.

Exception Condition
InvalidPrinterException

The printer named in the PrinterSettings.PrinterName property does not exist.

When handling the PrintDocument.PrintPage event, you can use this property along with the Bounds property to calculate the printing area for the page.

The following code example sets the default page settings for a document to margins to 1 inch on each side. 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(){
   try{
     streamToPrint = new StreamReader (filePath);
     try{
       printFont = new Font("Arial", 10);
       PrintDocument pd = new PrintDocument(); 
       pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
       pd.PrinterSettings.PrinterName = printer;
       // Create a new instance of Margins with 1-inch margins.
       Margins margins = new Margins(100,100,100,100);
       pd.DefaultPageSettings.Margins = margins;
       pd.Print();
     } 
     finally{
       streamToPrint.Close() ;
     }
   } 
   catch(Exception ex){ 
     MessageBox.Show(ex.Message);
   }
 }

.NET Framework
Available since 1.1
Return to top
Show: