如何在C#中設置列印紙張邊距

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

列印邊距控制著文件內容與紙張邊緣之間的空白區域。 正確的邊距設置可以防止文字被截斷,確保跨列印機布局一致,滿足發票、報告和法律文件的格式要求。

IronPrint的Margins類接受以毫米為單位的值,並提供三種建構函式重載—統一、水平/垂直和每側,因此我們可以用一行程式碼滿足任何布局要求。我們在下面介紹每種方法,從安裝到使用自定義邊距列印。

快速入門:設置紙張邊距

  1. 通過NuGet安裝IronPrint:Install-Package IronPrint
  2. using IronPrint;新增到文件
  3. 建立一個PrintSettings物件
  4. PaperMargins(以毫米為單位)
  5. 通過文件路徑將設置傳遞給Printer.Print()
  1. 使用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

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

參數順序為bottom。 每個值都是獨立的,因此我們可以建立非對稱布局來滿足頁首、頁腳、裝訂邊或打孔空間的需求。 Margins類API參考記錄了每個字段。

有哪些常用邊距布局的簡寫選項?

IronPrint的Margins類提供了兩個超越統一和每側版本的建構函式:

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

: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 = Margins.Zero
};

// 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 = Margins.Zero
}

' Print the poster
Printer.Print("poster.png", borderless)
$vbLabelText   $csharpLabel

請注意,大多數物理列印機會強制執行硬體最小可列印區域。 設置Margins.Zero將零邊距指令傳送給驅動程式,但根據列印機的功能,它仍可能會剪裁接近邊緣的內容。

如何將邊距與其他列印設置結合?

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 web應用—替換Printer.Print()以避免阻塞UI執行緒。 同一個PrintSettings物件適用於兩種方法。

我的下一步是什麼?

我們介紹了四種使用IronPrint配置列印邊距的方法:使用Margins.Zero的無邊框列印。 每種方法都輸入到Printer.PrintAsync()一起使用。

進一步閱讀,請瀏覽這些資源:

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

常見問題

什麼是IronPrint,它如何幫助在C#中設置列印邊距?

IronPrint是一個簡化了C#中列印邊距設置的.NET程式庫。它提供了一個Margins類別,允許開發者輕鬆使用一行程式碼自訂列印邊距,支持統一、單獨側邊和無邊框選項。

我如何使用IronPrint在C#中設置統一邊距進行列印?

要使用IronPrint在C#中設置統一邊距進行列印,可以使用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大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備好開始了嗎?
Nuget 下載 44,051 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

仍在滾動?

想要快速證明嗎? PM > Install-Package IronPrint
運行範例 看看您的文件如何到達印表機。