Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Margins Class

Specifies the dimensions of the margins of a printed page.

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

[SerializableAttribute] 
[TypeConverterAttribute(typeof(MarginsConverter))] 
public class Margins : ICloneable
/** @attribute SerializableAttribute() */ 
/** @attribute TypeConverterAttribute(System.Drawing.Printing.MarginsConverter) */ 
public class Margins implements ICloneable
SerializableAttribute 
TypeConverterAttribute(System.Drawing.Printing.MarginsConverter) 
public class Margins implements ICloneable
Not applicable.

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


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

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.