如何在 C# 中設定列印的紙張邊界
PRINT邊界控制文件內容與實體頁面邊緣之間的空白區域。 正確處理這些內容可避免文字被截斷,確保不同印表機間的版面配置一致,並滿足發票、報告及法律文件的格式要求。
IronPrint 的 Margins 類別接受以毫米為單位的數值,並提供三種建構子重載——均勻、水平/垂直,以及每邊——因此我們能透過單一行程式碼滿足任何版面配置需求。以下我們將逐步說明每種方法,從安裝到使用自訂邊距進行列印。
快速入門:設定紙張邊界
- 透過 NuGet 安裝 IronPrint:
Install-Package IronPrint - 將
using IronPrint;加入檔案中 - 建立一個
PrintSettings物件 - 將
Margins的值賦予PaperMargins(單位為公釐) - 將設定連同檔案路徑一併傳遞給
Printer.Print()
-
using 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設定邊距值 - 將設定傳遞給
Printer.PRINT() - 執行專案以自訂邊距進行PRINT
如何設定所有邊距均等?
最簡單的建構函式接受一個整數參數,並將其均勻地套用至所有四個邊。 我們以毫米為單位傳遞數值:
:path=/static-assets/print/content-code-examples/how-to/set-paper-margins/set-paper-margins-uniform-margins.cs
using IronPrint;
// Configure a uniform 20 mm margin on all sides
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(20),
PaperSize = PaperSize.A4
};
// Print the invoice
Printer.Print("invoice.pdf", settings);
Imports IronPrint
' Configure a uniform 20 mm margin on all sides
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(20),
.PaperSize = PaperSize.A4
}
' Print the invoice
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;
// Configure per-side margins (left, top, right, bottom)
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(10, 25, 10, 20),
PaperOrientation = PaperOrientation.Portrait
};
// Print the letterhead
Printer.Print("letterhead.pdf", settings);
Imports IronPrint
' Configure per-side margins (left, top, right, bottom)
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(10, 25, 10, 20),
.PaperOrientation = PaperOrientation.Portrait
}
' Print the letterhead
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;
// Configure horizontal and vertical margin shorthand
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(10, 20)
};
// Print the landscape report
Printer.Print("report-landscape.pdf", settings);
Imports IronPrint
' Configure horizontal and vertical margin shorthand
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(10, 20)
}
' Print the landscape report
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
using IronPrint;
// Configure zero margins for edge-to-edge printing
PrintSettings borderless = new PrintSettings
{
PaperMargins = new Margins(0)
};
// Print the poster
Printer.Print("poster.png", borderless);
Imports IronPrint
' Configure zero margins for edge-to-edge printing
Dim borderless As New PrintSettings With {
.PaperMargins = New Margins(0)
}
' Print the poster
Printer.Print("poster.png", borderless)
請注意,大多數實體印表機皆設有硬體規定的最小可列印區域。 設定 Margins.Zero 會向印表機驅動程式傳送零邊距指令,但視印表機的實際功能而定,邊緣附近的內容仍可能被裁切。
如何將邊距與其他PRINT設定結合使用?
PaperMargins 是 PrintSettings 上的其中一個屬性。 我們可以將紙張尺寸、方向、DPI、份數、灰階模式及印表機選項整合至單一設定物件中:
:path=/static-assets/print/content-code-examples/how-to/set-paper-margins/set-paper-margins-combined-settings.cs
using IronPrint;
// Configure full print settings with asymmetric margins
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"
};
// Print the Q4 report to the named printer
Printer.Print("Q4-report.pdf", settings);
Imports IronPrint
' Configure full print settings with asymmetric margins
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"
}
' Print the Q4 report to the named printer
Printer.Print("Q4-report.pdf", settings)
針對非同步工作流程(例如 WPF、MAUI 或 ASP.NET 網頁應用程式),請將 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 教學 — 文件列印,提供端到端的列印操作指南。
- PRINT 設定操作指南:包含 DPI、紙張方向、份數等設定說明。
Margins類別 API 參考文件,內含完整的建構函式與欄位說明。Printer所有靜態列印方法的類別 API 參考。
立即取得免費試用授權,在實際環境中測試所有功能;或於準備部署時查看授權方案。
常見問題
什麼是 IronPrint,它如何協助在 C# 中設定列印邊界?
IronPrint 是一個 .NET 程式庫,可簡化 C# 中的列印邊界設定。它提供 Margins 類別,讓開發人員只需一行程式碼,即可輕鬆自訂列印邊界,並提供「統一」、「每頁」及「無邊框」等選項。
如何使用 IronPrint 在 C# 中設定統一的列印邊距?
若要在 C# 中使用 IronPrint 設定統一的列印邊距,您可以使用 Margins 類別。此類別讓您能透過單一行程式碼,為頁面的所有邊緣指定相同的邊距大小。
在 C# 中是否可以為頁面的每一邊設定不同的邊距?
是的,IronPrint 允許您在 C# 中為頁面的每一邊設定不同的邊距。Margins 類別提供了自訂上、下、左、右邊距的選項。
我可以使用 IronPrint 製作無邊框列印嗎?
IronPrint 支援製作無邊框列印。透過使用 Margins 類別調整邊距,您可以將邊距設為零,從而有效產生無邊框列印。
使用 IronPrint 設定紙張邊界有何優勢?
IronPrint 透過提供直觀且高效的 API,簡化了在 C# 中設定紙張邊距的流程。其 Margins 類別能簡化程式碼並提升生產力,使實作自訂列印需求變得更加容易。
使用 IronPrint 設定列印邊界是否需要具備廣泛的程式設計知識?
不,您無需具備深厚的程式設計知識即可使用 IronPrint 設定列印邊界。此程式庫設計上極為友善,即使僅具備基礎 C# 技能的使用者,也能輕鬆實作自訂邊界。
IronPrint 在設定邊界時,如何處理不同的紙張尺寸?
IronPrint 支援多種紙張尺寸,讓您能根據文件的具體尺寸設定邊界。這種靈活性確保您的列印輸出能滿足特定的版面配置需求。
IronPrint 能否與其他 .NET 應用程式整合?
是的,IronPrint 可與其他 .NET 應用程式無縫整合,讓您能在各式各樣的專案和工作流程中,加入自訂的列印邊界設定。

