如何在 C# 中設定列印邊距

Curtis Chau
Curtis Chau
已更新: 2026年8月2日

列印邊距控制文件內容與實體頁面邊緣之間的空白區域。 正確處理這些內容可避免文字被截斷,確保不同印表機間的版面配置一致,並滿足發票、報告及法律文件的格式要求。

IronPrint 的 Margins 類別接受 毫米的值,並提供三種構造函數重載 - 統一的、水平/垂直的和逐邊的,因此我們可以在一行內滿足任何佈局要求。我們將從安裝到使用自訂邊距打印的每種方法一一解析。

快速入門:設定紙張邊距
  1. 通過 NuGet 安裝 IronPrint: Install-Package IronPrint
  2. 將 using IronPrint; 添加到檔案中
  3. 創建一個 PrintSettings 物件
  4. 給 PaperMargins 賦予 Margins 值(值以毫米為單位)
  5. 將設置和檔案路徑一同傳遞給 Printer.Print()
  1. 1使用 IronPrint 安裝 NuGet Package Manager

    PM > Install-Package IronPrint

  2. 2複製並運行這段程式碼。

    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);
  3. 3部署到您的生產環境進行測試

    立即在您的項目中開始使用 IronPrint,並使用 免費試用
    arrow pointer

如何設定所有邊距均等?

最簡單的建構函式接受一個整數,並將其均勻地套用至所有四個邊。 我們以 毫米為單位傳遞值:

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

Margins(20) 將 Left, Top, Right 和 Bottom 分別設置為 20 毫米。 這是標準商業文件的常見選擇,此類文件僅需確保每邊距的空白一致即可。

IronPrint 以毫米為單位測量邊距,避免了 System.Drawing.Printing.Margins 類別使用百分之英寸的混淆。 IronPrint 中 25.4 毫米的邊距相當於 new System.Drawing.Printing.Margins(100) - 無需在我們這端進行轉換數學計算。

如何為每邊設定不同的邊距?

當文件需要在頂部預留額外空間放置信頭,或在底部預留空間放置頁尾時,我們會使用四參數建構函式:

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

參數順序為 left, top, right, bottom。 每個值皆為獨立存在,因此我們可建立非對稱版面配置,以適應頁首、頁尾、裝訂邊緣或打孔空間。 《 Margins 類別 API 參考手冊》記錄了每個欄位。

常見邊距佈局有哪些簡寫選項?

IronPrint 的 Margins 類別除了統一和逐邊版本外,還提供了一個額外的構造函數和一個靜態的簡寫字段:

水平/垂直簡寫 - Margins(int horizontal, int vertical) 將左+右設置為第一個值,頂+底設置為第二個值:

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

零邊距 - Margins.Zero 移除所有邊距以進行無邊框打印:

using IronPrint;

// Configure zero margins for edge-to-edge printing
PrintSettings borderless = new PrintSettings
{
    PaperMargins = Margins.Zero
};

// Print the poster
Printer.Print("poster.png", borderless);

請注意,大多數實體印表機皆設有硬體規定的最小可列印區域。 設置 Margins.Zero 發送零邊距指令給驅動程式,但打印機可能仍會根據其能力在邊緣附近截斷內容。

如何將邊距與其他列印設定結合使用?

PaperMargins 是 PrintSettings 的一個屬性。 我們可以將紙張尺寸、方向、DPI、份數、灰階模式及印表機選項整合至單一設定物件中:

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

對於異步工作流程 - WPF、MAUI 或 ASP.NET web 應用 - 使用 await Printer.PrintAsync() 替代 Printer.Print() 以避免阻塞 UI 執行緒。 同一個 PrintSettings 物件可與兩種方法一起使用。

接下來我該怎麼做?

我們介紹了使用 IronPrint 配置打印邊距的四種方法:使用 Margins(int) 的統一邊距、使用 Margins(int, int, int, int) 的逐邊控制、Margins(int, int) 的水平/垂直簡寫,和使用 Margins.Zero 的無邊框打印。 每一種方法都輸入 PrintSettings.PaperMargins,並可與 Printer.Print() 和 Printer.PrintAsync() 一起使用。

如需進一步閱讀,請參閱以下資源:

獲取免費試用授權以在實時環境中測試所有功能,或者查看授權選項當您準備部署時。

常見問題

在IronPrint中,什麼是列印邊界?

IronPrint中的列印邊界控制文件內容與實體頁面邊緣之間的空白區域。它們防止文字被截斷,保持佈局一致,並滿足各種文件的格式要求。

如何在C#中使用IronPrint設置統一印邊界?

您可以通過使用Margins類別建構函數與單一整數值來設置統一印邊界。例如,new Margins(20)在所有邊設定20毫米的邊界。

如何使用IronPrint為文檔的每個側面指定不同的邊界?

要設置不同的邊界,您可以使用Margins建構函數與四個參數:左、上、右和下。例如,new Margins(10, 25, 10, 20)。

我可以使用IronPrint設定零印邊界嗎?

可以,IronPrint允許您使用Margins.Zero設置零邊界。但是要注意,實體打印機仍可能強制執行最小可印刷區域。

如何用簡寫方法設置水平和垂直邊界?

IronPrint提供了一種簡寫方法,使用Margins(int horizontal, int vertical),其中第一個值設置左右邊界,第二個值設置上下邊界。

IronPrint中的印設置還可以與哪些邊界結合使用?

在IronPrint中,您可以將邊界與其他設置如紙張大小、方向、DPI、份數和打印機選擇等,結合在一個PrintSettings對象中。

如何在我的 C# 專案中安裝 IronPrint?

使用NuGet指令Install-Package IronPrint安裝IronPrint接著在您的專案檔中加入using IronPrint;。

在IronPrint中使用毫米來設置邊界的好處是什麼?

使用毫米可以避免與System.Drawing.Printing.Margins類別相關的轉換錯誤,該類別使用的是百分之一英寸,使其更簡單直觀。

如何使用IronPrint異步列印文件?

為非同步列印,將Printer.Print()替換為await Printer.PrintAsync(),以防止在您的IronPrint工作流程中阻塞UI線程。

我在哪裡可以找到更多有關IronPrint的資源和教程?

更多IronPrint資源可以在IronPrint教程頁面找到,包括Margins和Printer類別的API參考,以及如何在其網站上的印設置指南。

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

閱讀更多

準備好開始了嗎?

NuGet 下載次數 52,427版本:2026.11剛剛發布