Margins Constructor (Int32, Int32, Int32, Int32)
Collapse the table of content
Expand the table of content

Margins Constructor (Int32, Int32, Int32, Int32)

 

Initializes a new instance of the Margins class with the specified left, right, top, and bottom margins.

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

public Margins(
	int left,
	int right,
	int top,
	int bottom
)

Parameters

left
Type: System.Int32

The left margin, in hundredths of an inch.

right
Type: System.Int32

The right margin, in hundredths of an inch.

top
Type: System.Int32

The top margin, in hundredths of an inch.

bottom
Type: System.Int32

The bottom margin, in hundredths of an inch.

Exception Condition
ArgumentException

The left parameter value is less than 0.

-or-

The right parameter value is less than 0.

-or-

The top parameter value is less than 0.

-or-

The bottom parameter value is less than 0.

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

The following code example sets the default page settings for a document to margins of 1 inch width on each side.

public void Printing()
{
  try 
  {
    /* This assumes that a variable of type string, named filePath,
       has been set to the path of the file to print. */
    streamToPrint = new StreamReader (filePath);
    try 
    {
      printFont = new Font("Arial", 10);
      PrintDocument pd = new PrintDocument(); 
      /* This assumes that a method, named pd_PrintPage, has been
         defined. pd_PrintPage handles the PrintPage event. */
      pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
      /* This assumes that a variable of type string, named 
         printer, has been set to the printer's name. */
      pd.PrinterSettings.PrinterName = printer;
      // Create a new instance of Margins with one inch margins.
      Margins margins = new Margins(100,100,100,100);
      pd.DefaultPageSettings.Margins = margins;
      pd.Print();
    } 
    finally 
    {
      streamToPrint.Close() ;
    }
  } 
  catch(Exception ex) 
  { 
    MessageBox.Show("An error occurred printing the file - " + ex.Message);
  }
}

.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft