PageSettings.Margins Property

Definition

Gets or sets the margins for this page.

C#
public System.Drawing.Printing.Margins Margins { get; set; }

Property Value

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

Exceptions

The printer named in the PrinterName property does not exist.

Examples

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.

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

Remarks

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

Applies to

Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also