如何在 C# 中設定列印邊距
列印邊距控制文件內容與實體頁面邊緣之間的空白區域。 正確處理這些內容可避免文字被截斷,確保不同印表機間的版面配置一致,並滿足發票、報告及法律文件的格式要求。
IronPrint 的 Margins 類別接受以毫米為單位的數值,並提供三種建構子重載 —— 均勻、水平/垂直,以及每邊 —— 因此我們能透過單一行程式碼滿足任何版面配置需求。以下我們將逐步說明每種方法,從安裝到使用自訂邊距進行列印。
快速入門:設定紙張邊距
- 透過 NuGet 安裝 IronPrint:
Install-Package IronPrint - 將
using IronPrint;加入檔案中 - 建立一個
PrintSettings物件 - 將
Margins的值賦予PaperMargins(單位為公釐) - 將設定連同檔案路徑傳遞給
Printer.Print()
-
使用NuGet套件管理器安裝https://www.nuget.org/packages/IronPrint
PM > Install-Package IronPrint -
複製並運行這段程式碼。
using IronPrint; // Set 15 mm margins on all sides and print PrintSettings settings = new PrintSettings(); settings.PaperMargins = new Margins(15); Printer.Print("report.pdf", settings); -
部署到您的生產環境進行測試
今天就在您的專案中開始使用免費試用IronPrint
最小工作流程(5 個步驟)
- 安裝 IronPrint C# 程式庫
- 建立一個 `PrintSettings` 物件
- 為 `PaperMargins` 設定 `Margins` 值
- 將設定傳遞給 `Printer.Print()`
- 執行專案以自訂邊距進行列印
如何設定所有邊距均等?
最簡單的建構函式接受一個整數,並將其均勻地套用至所有四個邊。 我們傳遞的數值單位為毫米:
:path=/static-assets/print/content-code-examples/how-to/set-paper-margins/set-paper-margins-uniform-margins.cs
using IronPrint;
// 20 mm margin on every side
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(20),
PaperSize = PaperSize.A4
};
Printer.Print("invoice.pdf", settings);
Imports IronPrint
' 20 mm margin on every side
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(20),
.PaperSize = PaperSize.A4
}
Printer.Print("invoice.pdf", settings)
Margins(20) 將 Right 及 Bottom 各自設定為 20 公釐。 這是標準商業文件的常見選擇,此類文件僅需確保每邊距的空白一致即可。
IronPrint 以毫米為單位測量邊距,這避免了 System.Drawing.Printing.Margins 類別使用百分之一英吋所造成的混淆。 在 IronPrint 中,25.4 公釐的邊距相當於 new System.Drawing.Printing.Margins(100) —— 我們這邊無需進行任何換算。
如何為每邊設定不同的邊距?
當文件需要在頂部預留額外空間放置信頭,或在底部預留空間放置頁尾時,我們會使用四參數建構函式:
:path=/static-assets/print/content-code-examples/how-to/set-paper-margins/set-paper-margins-per-side-margins.cs
using IronPrint;
// Left: 10 mm, Top: 25 mm, Right: 10 mm, Bottom: 20 mm
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(10, 25, 10, 20),
PaperOrientation = PaperOrientation.Portrait
};
Printer.Print("letterhead.pdf", settings);
Imports IronPrint
' Left: 10 mm, Top: 25 mm, Right: 10 mm, Bottom: 20 mm
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(10, 25, 10, 20),
.PaperOrientation = PaperOrientation.Portrait
}
Printer.Print("letterhead.pdf", settings)
參數順序為 bottom。 每個值皆為獨立存在,因此我們可建立非對稱版面配置,以適應頁首、頁尾、裝訂邊緣或打孔空間。 Margins 類別的 API 參考文件中詳述了每個欄位。
常見邊距佈局有哪些簡寫選項?
IronPrint 的 Margins 類別除了統一版和單面版之外,還提供了兩個額外的建構函式:
水平/垂直縮寫 — Margins(int horizontal, int vertical) 將 left+right 設為第一個值,top+bottom 設為第二個值:
:path=/static-assets/print/content-code-examples/how-to/set-paper-margins/set-paper-margins-shorthand-margins.cs
using IronPrint;
// 10 mm left & right, 20 mm top & bottom
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(10, 20)
};
Printer.Print("report-landscape.pdf", settings);
Imports IronPrint
' 10 mm left & right, 20 mm top & bottom
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(10, 20)
}
Printer.Print("report-landscape.pdf", settings)
零邊距 — Margins.Zero 會移除所有邊距以進行無邊框列印:
:path=/static-assets/print/content-code-examples/how-to/set-paper-margins/set-paper-margins-zero-margins.cs
PrintSettings borderless = new PrintSettings
{
PaperMargins = Margins.Zero
};
Printer.Print("poster.png", borderless);
Dim borderless As New PrintSettings With {
.PaperMargins = Margins.Zero
}
Printer.Print("poster.png", borderless)
請注意,大多數實體印表機皆設有硬體規定的最小可列印區域。 設定 Margins.Zero 會向印表機驅動程式傳送零邊距指令,但視印表機的實際功能而定,邊緣附近的內容仍可能被裁切。
如何將邊距與其他列印設定結合使用?
PaperMargins 是 PrintSettings 上的其中一個屬性。 我們可以將紙張尺寸、方向、DPI、份數、灰階模式及印表機選項整合至單一設定物件中:
:path=/static-assets/print/content-code-examples/how-to/set-paper-margins/set-paper-margins-combined-settings.cs
using IronPrint;
// Full print configuration for a quarterly report
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(15, 20, 15, 25),
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 300,
NumberOfCopies = 2,
Grayscale = false,
PrinterName = "HP LaserJet Pro MFP M428"
};
Printer.Print("Q4-report.pdf", settings);
Imports IronPrint
' Full print configuration for a quarterly report
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(15, 20, 15, 25),
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Portrait,
.Dpi = 300,
.NumberOfCopies = 2,
.Grayscale = False,
.PrinterName = "HP LaserJet Pro MFP M428"
}
Printer.Print("Q4-report.pdf", settings)
針對非同步工作流程(例如 WPF、MAUI 或 ASP.NET Web 應用程式),請將 Printer.Print() 替換為 await Printer.PrintAsync(),以避免阻塞 UI 執行緒。 同一個 PrintSettings 物件可與這兩種方法配合使用。
接下來我該怎麼做?
我們介紹了四種使用 IronPrint 設定列印邊距的方法:使用 Margins(int) 設定統一邊距、使用 Margins(int, int, int, int) 進行每頁獨立控制、使用 Margins(int, int) 的水平/垂直簡寫,以及使用 Margins.Zero 進行無邊框列印。 每種方法皆整合至 PrintSettings.PaperMargins,並可與 Printer.Print() 及 Printer.PrintAsync() 協同運作。
如需進一步閱讀,請參閱以下資源:
- IronPrint 教學 — 文件列印,提供端到端的列印操作指南。
- 列印設定操作指南,涵蓋 DPI、紙張方向、份數等設定。
- 請參閱 Margins Class API 參考文件,以獲取完整的建構子與屬性文件。
- 所有靜態列印方法的 Printer 類別 API 參考。
立即取得免費試用授權,在實際環境中測試所有功能;或於準備部署時查看授權選項。

