PageSettings.Margins 属性
定义
获取或设置该页的边距。Gets or sets the margins for this page.
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,表示页边距(以百分之一英寸为单位)。A Margins that represents the margins, in hundredths of an inch, for the page. 默认情况下各边的边距都为 1 英寸。The default is 1-inch margins on all sides.
例外
PrinterName 属性中命名的打印机不存在。The printer named in the PrinterName property does not exist.
示例
下面的代码示例将文档的默认页面设置设置为每一端的边距为1英寸。The following code example sets the default page settings for a document to margins to 1 inch on each side. 该示例有三个先决条件:The example has three prerequisites:
已将名为的变量
filePath设置为要打印的文件的路径。A variable namedfilePathhas been set to the path of the file to print.已经定义了一个名为的方法
pd_PrintPage,该方法处理 PrintPage 事件。A method namedpd_PrintPage, which handles the PrintPage event, has been defined.已将名为的变量
printer设置为打印机的名称。A variable namedprinterhas been set to the printer's name.
在 System.Drawing 此示例中使用、 System.Drawing.Printing 和 System.IO 命名空间。Use the System.Drawing, System.Drawing.Printing, and System.IO namespaces for this example.
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 来计算页面的打印区域。When handling the PrintDocument.PrintPage event, you can use this property along with the Bounds property to calculate the printing area for the page.