Margins Class

Definition

Specifies the dimensions of the margins of a printed page.

C#
[System.ComponentModel.TypeConverter(typeof(System.Drawing.Printing.MarginsConverter))]
public class Margins : ICloneable
C#
[System.ComponentModel.TypeConverter(typeof(System.Drawing.Printing.MarginsConverter))]
[System.Serializable]
public class Margins : ICloneable
C#
[System.ComponentModel.TypeConverter("System.Drawing.Printing.MarginsConverter, System.Windows.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51")]
public class Margins : ICloneable
Inheritance
Margins
Attributes
Implements

Examples

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 on each side.

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

Remarks

The Margins class is used to manipulate the width of margins in PageSettings and PrintController. MarginsConverter is the type descriptor for this class.

Left, Right, Top, and Bottom are properties that define the margins. Clone creates an identical Margins. Equals determines if another object has the same dimensions as a Margins.

For more information about printing, see the System.Drawing.Printing namespace overview.

Constructors

Margins()

Initializes a new instance of the Margins class with 1-inch wide margins.

Margins(Int32, Int32, Int32, Int32)

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

Properties

Bottom

Gets or sets the bottom margin, in hundredths of an inch.

Left

Gets or sets the left margin width, in hundredths of an inch.

Right

Gets or sets the right margin width, in hundredths of an inch.

Top

Gets or sets the top margin width, in hundredths of an inch.

Methods

Clone()

Retrieves a duplicate of this object, member by member.

Equals(Object)

Compares this Margins to the specified Object to determine whether they have the same dimensions.

GetHashCode()

Calculates and retrieves a hash code based on the width of the left, right, top, and bottom margins.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Converts the Margins to a string.

Operators

Equality(Margins, Margins)

Compares two Margins to determine if they have the same dimensions.

Inequality(Margins, Margins)

Compares two Margins to determine whether they are of unequal width.

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