PageSettings.Margins 属性

定义

获取或设置该页的边距。

public System.Drawing.Printing.Margins Margins { get; set; }

属性值

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

例外

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

示例

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

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

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

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

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

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

注解

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

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

另请参阅