Export (0) Print
Expand All

Margins Class

Specifies the margins of a printed page.

For a list of all members of this type, see Margins Members.

System.Object
   System.Drawing.Printing.Margins

[Visual Basic]
Public Class Margins
   Implements ICloneable
[C#]
public class Margins : ICloneable
[C++]
public __gc class Margins : public ICloneable
[JScript]
public class Margins implements ICloneable

Thread Safety

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

Remarks

This class is used to manipulate 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 object. Equals determines if another object is equal to a Margins object.

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

Example

[Visual Basic, C#, C++] Use the System.Drawing, System.Drawing.Printing, and System.IO namespaces for this example.

[Visual Basic, C#, C++] The following example sets the default page settings for a document to margins of 1 inch on each side.

[Visual Basic] 
Public Sub 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)
            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
            pd.Print()
        Finally
            streamToPrint.Close()
        End Try
    Catch ex As Exception
        MessageBox.Show("An error occurred printing the file - " & ex.Message)
    End Try
End Sub
   

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


[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 System::Drawing::Font(S"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(this, &Sample::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(String::Concat( S"An error occurred printing the file - ", ex->Message ));
   }
 }
 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Drawing.Printing

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

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

See Also

Margins Members | System.Drawing.Printing Namespace | PageSettings | PrintController | MarginsConverter

Show:
© 2014 Microsoft