Export (0) Print
Expand All

Margins Class

Specifies the dimensions of the margins of a printed page.

System.Object
  System.Drawing.Printing.Margins

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

[SerializableAttribute]
[TypeConverterAttribute(typeof(MarginsConverter))]
public class Margins : ICloneable

The Margins type exposes the following members.

  NameDescription
Public methodMargins()Initializes a new instance of the Margins class with 1-inch wide margins.
Public methodMargins(Int32, Int32, Int32, Int32)Initializes a new instance of the Margins class with the specified left, right, top, and bottom margins.
Top

  NameDescription
Public propertyBottomGets or sets the bottom margin, in hundredths of an inch.
Public propertyLeftGets or sets the left margin width, in hundredths of an inch.
Public propertyRightGets or sets the right margin width, in hundredths of an inch.
Public propertyTopGets or sets the top margin width, in hundredths of an inch.
Top

  NameDescription
Public methodCloneRetrieves a duplicate of this object, member by member.
Public methodEqualsCompares this Margins to the specified Object to determine whether they have the same dimensions. (Overrides Object.Equals(Object).)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeCalculates and retrieves a hash code based on the width of the left, right, top, and bottom margins. (Overrides Object.GetHashCode().)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringInfrastructure. Converts the Margins to a string. (Overrides Object.ToString().)
Top

  NameDescription
Public operatorStatic memberEqualityCompares two Margins to determine if they have the same dimensions.
Public operatorStatic memberInequalityCompares two Margins to determine whether they are of unequal width.
Top

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.

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.

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

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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