PageSettings.Margins 属性

定义

获取或设置该页的边距。

public:
 property System::Drawing::Printing::Margins ^ Margins { System::Drawing::Printing::Margins ^ get(); void set(System::Drawing::Printing::Margins ^ value); };
public System.Drawing.Printing.Margins Margins { get; set; }
member this.Margins : System.Drawing.Printing.Margins with get, set
Public Property Margins As Margins

属性值

Margins,表示页边距(以百分之一英寸为单位)。 默认情况下各边的边距都为 1 英寸。

例外

PrinterName 属性中命名的打印机不存在。

示例

下面的代码示例将文档的默认页面设置设置为每边距 1 英寸。 该示例有三个先决条件:

  • 名为 的 filePath 变量已设置为要打印的文件的路径。

  • 已定义一个名为 pd_PrintPage的方法,用于处理事件 PrintPage

  • 名为 的 printer 变量已设置为打印机的名称。

对于此示例, System.Drawing请使用 、 System.Drawing.PrintingSystem.IO 命名空间。

public:
   void Printing()
   {
      try
      {
         streamToPrint = gcnew StreamReader( filePath );
         try
         {
            printFont = gcnew Font( "Arial",10 );
            PrintDocument^ pd = gcnew PrintDocument;
            pd->PrintPage += gcnew PrintPageEventHandler(
               this, &Sample::pd_PrintPage );
            pd->PrinterSettings->PrinterName = printer;
            // Create a new instance of Margins with 1-inch margins.
            Margins^ margins = gcnew Margins( 100,100,100,100 );
            pd->DefaultPageSettings->Margins = margins;
            pd->Print();
         }
         finally
         {
            streamToPrint->Close();
         }
      }
      catch ( Exception^ ex ) 
      {
         MessageBox::Show( ex->Message );
      }
   }
public void Printing(){
   try{
     streamToPrint = new StreamReader (filePath);
     try{
       printFont = new Font("Arial", 10);
       PrintDocument pd = new PrintDocument(); 
       pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
       pd.PrinterSettings.PrinterName = printer;
       // Create a new instance of Margins with 1-inch margins.
       Margins margins = new Margins(100,100,100,100);
       pd.DefaultPageSettings.Margins = margins;
       pd.Print();
     } 
     finally{
       streamToPrint.Close() ;
     }
   } 
   catch(Exception ex){ 
     MessageBox.Show(ex.Message);
   }
 }
Public Sub Printing()
    Try
        streamToPrint = New StreamReader(filePath)
        Try
            printFont = New Font("Arial", 10)
            Dim pd As New PrintDocument()
            AddHandler pd.PrintPage, AddressOf pd_PrintPage
            pd.PrinterSettings.PrinterName = printer
            ' Create a new instance of Margins with 1-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(ex.Message)
    End Try
End Sub

注解

处理 PrintDocument.PrintPage 事件时,可以将此属性与 属性一起使用 Bounds ,以计算页面的打印区域。

适用于

另请参阅