Margins Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Specifies the dimensions of the margins of a printed page.

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


Public Class Margins
	Implements ICloneable


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

System_CAPS_pubmethodMargins(Int32, Int32, Int32, Int32)

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


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


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


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


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


Retrieves a duplicate of this object, member by member.


Compares this Margins to the specified Object to determine whether they have the same dimensions.(Overrides Object.Equals(Object).)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Calculates and retrieves a hash code based on the width of the left, right, top, and bottom margins.(Overrides Object.GetHashCode().)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


This API supports the product infrastructure and is not intended to be used directly from your code. Converts the Margins to a string.(Overrides Object.ToString().)

System_CAPS_puboperatorSystem_CAPS_staticEquality(Margins, Margins)

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

System_CAPS_puboperatorSystem_CAPS_staticInequality(Margins, Margins)

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

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 Sub Printing()
        ' 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)
            printFont = New Font("Arial", 10)
            Dim pd As New PrintDocument()
            ' This assumes that a method, named pd_PrintPage, has been
            ' defined. pd_PrintPage handles the PrintPage event. 
            AddHandler pd.PrintPage, AddressOf 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.
            Dim margins As New Margins(100, 100, 100, 100)
            pd.DefaultPageSettings.Margins = margins
        End Try
    Catch ex As Exception
        MessageBox.Show("An error occurred printing the file - " & ex.Message)
    End Try
End Sub

.NET Framework
Available since 1.1

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

Return to top