IRONSOFTWAREHOME

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

Curtis Chau
Curtis Chau
Updated: 2026年6月29日

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

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

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

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

參數順序為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);
C#

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

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

PaperMarginsPrintSettings上的一個屬性。 我們可以將其與紙張大小、方向、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應用—替換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應用程式整合,讓您將自訂列印邊距設置納入各種專案和工作流程中。

How do I print documents asynchronously with IronPrint?

For asynchronous printing, replace `Printer.Print()` with `await Printer.PrintAsync()` in your IronPrint-based workflow to prevent blocking the UI thread.

Where can I find further resources and tutorials for IronPrint?

Further resources for IronPrint can be found on the IronPrint tutorials page, API references for the `Margins` and `Printer` classes, and the Print Settings How-To guide on their website.

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

...
閱讀更多

準備好開始了嗎?

Nuget Downloads 46,090版本:2026.9剛剛發布

立即獲取您的免費30天試用金鑰
不需要信用卡或帳戶建立
C# NuGet PDF程式庫
安裝方法NuGet

版本: 2026.9

PM > Install-Package IronPrint
nuget.org/packages/IronPrint/
  1. 在解決方案資源管理器中,右鍵點擊引用,管理NuGet套件
  2. 選擇瀏覽並搜尋"IronPrint"
  3. 選擇套件並安裝
C# PDF DLL
下載DLL

版本: 2026.9

  1. 下載並解壓IronPrint到比如~/Libs這樣的位置在您的Solution目錄下
  2. 在Visual Studio解決方案資源管理器中,右鍵點擊引用。選擇瀏覽,"IronPrint.dll"

授權從$999

有問題嗎?聯繫我們的開發團隊。

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預約您的免費
現場演示
Booking Badge

全球數百萬工程師信賴

Iron Software的客戶標誌
獲得非義務性諮詢
完成下列表單或發郵件到sales@ironsoftware.com
您的資訊將始終保密。
全球數百萬工程師信賴
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立