PageSettings.Margins 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定這個頁面的邊界。
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.Printing和 System.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 屬性來計算頁面的列印區域。