如何在 C# 中設定列印的紙張邊界

This article was translated from English: Does it need improvement?
Translated
View the article in English

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

IronPrint 的 Margins 類別接受以毫米為單位的數值,並提供三種建構子重載——均勻、水平/垂直,以及每邊——因此我們能透過單一行程式碼滿足任何版面配置需求。以下我們將逐步說明每種方法,從安裝到使用自訂邊距進行列印。

快速入門:設定紙張邊界

  1. 透過 NuGet 安裝 IronPrint:Install-Package IronPrint
  2. using IronPrint; 加入檔案中
  3. 建立一個 PrintSettings 物件
  4. Margins 的值賦予 PaperMargins(單位為公釐)
  5. 將設定連同檔案路徑一併傳遞給 Printer.Print()
  1. using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronPrint

    PM > Install-Package IronPrint
  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. 部署至您的生產環境進行測試

    立即透過免費試用,在您的專案中開始使用 IronPrint

    arrow pointer

如何設定所有邊距均等?

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

: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)
$vbLabelText   $csharpLabel

Margins(20)RightBottom 各自設定為 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)
$vbLabelText   $csharpLabel

參數順序為 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)
$vbLabelText   $csharpLabel

零邊距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)
$vbLabelText   $csharpLabel

請注意,大多數實體印表機皆設有硬體規定的最小可列印區域。 設定 Margins.Zero 會向印表機驅動程式傳送零邊距指令,但視印表機的實際功能而定,邊緣附近的內容仍可能被裁切。

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

PaperMarginsPrintSettings 上的其中一個屬性。 我們可以將紙張尺寸、方向、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)
$vbLabelText   $csharpLabel

針對非同步工作流程(例如 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,它如何協助在 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 應用程式無縫整合,讓您能在各式各樣的專案和工作流程中,加入自訂的列印邊界設定。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 41,154 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronPrint
執行範例程式,親眼見證您的文件送印。