如何在 C# 中配置 PDF 打印設置 | IronPrint

如何使用IronPrint在 C# 中配置列印設置

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

使用 IronPrint 的 PrintSettings 類別在 C# 中配置列印設置,以控制紙張尺寸、方向、DPI、邊距等。 只需實例化 PrintSettings,設定您的偏好,然後將其傳遞給 Print 方法。

快速入門:使用IronPrint設定進行設定和列印快速入門:使用 IronPrint 設定進行設定和列印

首先建立一個 PrintSettings 對象,設定紙張大小、方向、DPI、份數和灰階等屬性。 然後調用 Printer.Print(...) 即可立即套用這些設定——無需複雜的設定。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronPrint

    PM > Install-Package IronPrint
  2. 複製並運行這段程式碼。

    IronPrint.Printer.Print("document.pdf", new IronPrint.PrintSettings { PaperSize = IronPrint.PaperSize.A4, PaperOrientation = IronPrint.PaperOrientation.Landscape, Dpi = 300, NumberOfCopies = 2, Grayscale = true });
  3. 部署到您的生產環境進行測試

    今天就在您的專案中開始使用免費試用IronPrint

    arrow pointer

如何設定列印設定?

若要配置列印設置,請實例化PrintSettings 類別並根據您的喜好進行配置。 在 PrintShowPrintDialog 方法中,將 PrintSettings 物件作​​為第二個參數傳遞。 下面的程式碼範例說明了這種用法。 如需更詳細的範例,請查看列印設定程式碼範例頁面。

// Import the necessary namespace for IronPrint
using IronPrint;

// Initialize a new instance of the PrintSettings class
PrintSettings settings = new PrintSettings();

// Configure various print settings
settings.PaperSize = PaperSize.A4;                // Set paper size to A4
settings.PaperOrientation = PaperOrientation.Landscape; // Set paper orientation to Landscape
settings.Dpi = 300;                               // Set print resolution to 300 DPI
settings.NumberOfCopies = 2;                      // Set the number of copies to 2
settings.PrinterName = "MyPrinter";               // Set the name of the printer
settings.PaperMargins = new Margins(10, 10, 10, 10); // Set margins to 10mm on each side
settings.Grayscale = true;                        // Print in grayscale

// Use the PrintSettings in the Print method
IronPrint.Printer.Print(document, settings);
// Import the necessary namespace for IronPrint
using IronPrint;

// Initialize a new instance of the PrintSettings class
PrintSettings settings = new PrintSettings();

// Configure various print settings
settings.PaperSize = PaperSize.A4;                // Set paper size to A4
settings.PaperOrientation = PaperOrientation.Landscape; // Set paper orientation to Landscape
settings.Dpi = 300;                               // Set print resolution to 300 DPI
settings.NumberOfCopies = 2;                      // Set the number of copies to 2
settings.PrinterName = "MyPrinter";               // Set the name of the printer
settings.PaperMargins = new Margins(10, 10, 10, 10); // Set margins to 10mm on each side
settings.Grayscale = true;                        // Print in grayscale

// Use the PrintSettings in the Print method
IronPrint.Printer.Print(document, settings);
$vbLabelText   $csharpLabel

為什麼需要配置列印設定?

列印設定是指決定如何列印文件或內容的配置或參數集。 這些設定包括紙張尺寸、方向(縱向或橫向)、列印解析度(每英吋點數 - DPI)、份數、印表機選擇、邊距以及灰階列印等選項等詳細資訊。 自訂這些設定以滿足特定的列印偏好和要求。

IronPrint 的全面列印設定功能為開發人員提供了對列印過程各個方面的精細控制。 無論是建立桌面應用程式還是ASP.NET Web 應用程序,正確的配置都能確保在不同環境下獲得一致的結果。

何時應該使用自訂列印設定?

當需要精確控制列印輸出時,自訂列印設定至關重要,例如列印具有特定邊距的報表、產生文件的多份副本,或確保文件以正確的方向列印以滿足業務需求。

以下是列印符合特定要求的發票的實用範例:

// Example: Printing invoices with business requirements
using IronPrint;

// Invoice printing with specific business settings
var invoiceSettings = new PrintSettings
{
    PaperSize = PaperSize.Letter,        // US Letter size for business documents
    PaperOrientation = PaperOrientation.Portrait,
    Dpi = 600,                           // High quality for professional output
    NumberOfCopies = 3,                  // Original + customer copy + file copy
    PaperMargins = new Margins(15, 15, 15, 25), // Extra bottom margin for footer
    Grayscale = false,                   // Keep company logo in color
    PrinterName = "Office Color Printer" // Specific high-quality printer
};

// Print the invoice
Printer.Print("invoice_2024_001.pdf", invoiceSettings);
// Example: Printing invoices with business requirements
using IronPrint;

// Invoice printing with specific business settings
var invoiceSettings = new PrintSettings
{
    PaperSize = PaperSize.Letter,        // US Letter size for business documents
    PaperOrientation = PaperOrientation.Portrait,
    Dpi = 600,                           // High quality for professional output
    NumberOfCopies = 3,                  // Original + customer copy + file copy
    PaperMargins = new Margins(15, 15, 15, 25), // Extra bottom margin for footer
    Grayscale = false,                   // Keep company logo in color
    PrinterName = "Office Color Printer" // Specific high-quality printer
};

// Print the invoice
Printer.Print("invoice_2024_001.pdf", invoiceSettings);
$vbLabelText   $csharpLabel

如果我不指定列印設定會發生什麼?

如果未指定列印設置, IronPrint將使用系統預設印表機的預設設置,這可能與您預期的輸出格式或品質要求不符。 若要發現系統上可用的印表機,請使用GetPrinterNames 方法以程式設計方式擷取所有已連接的印表機。

有哪些列印設定可用?

請查看以下所有可用的列印設定選項。 完整的API 參考文件提供了每個屬性和方法的詳細文件:

環境 描述 預設值 評論
預設設定 使用預設值初始化IronPrint類別的新實例 不適用 不適用
紙張尺寸 設定印表機使用的紙張尺寸 IronPrint.PaperSize.PrinterDefault 不適用
紙張方向 指定紙張方向(例如,縱向或橫向) IronPrint.PaperOrientation.Portrait 不適用
dpi 表示預期的列印分辨率,單位為每英吋點數。 300 實際列印所用的DPI可能受限於印表機的效能。
份數 表示列印文件時要產生的相同副本的數量。 1 在某些平台上,可能存在一些限制,導致無法準確地複製多個副本。 在這種情況下,指定的 IronPrint.PrintSettings.NumberOfCopies 值可能會被忽略,導致只列印一份。
印表機名稱 指定用於列印的印表機的名稱 空(使用作業系統預設印表機) 如果在列印對話方塊中選擇印表機,則此設定將被忽略。 若要取得可用的印表機名稱,您可以使用 IronPrint.Printer.GetPrinterNamesIronPrint.Printer.GetPrinterNamesAsync 來取得印表機名稱清單。
紙張邊緣 設定列印時所使用的頁邊距(單位:毫米) 空白(使用印表機預設邊距) 不適用
灰階 指示是否以灰階列印 錯誤(嘗試彩色列印) 不適用
壓平 列印前將 PDF 檔案展平,這有助於顯示表單欄位值和影像。 錯誤的 不適用
托盤 用於列印作業的印表機紙匣。 這樣,使用者可以指定從哪個紙匣傳送到印表機送紙。 空(使用印表機預設紙匣) 如果在列印對話方塊中選擇紙盒,則此設定將被忽略。 若要取得可用托盤,您可以使用 IronPrint.Printer.GetPrinterTrays(System.String)IronPrint.Printer.GetPrinterTraysAsync(System.String)。 此托盤選擇屬性僅在 Windows 系統中可用。

哪些列印設定必須始終配置?

對於大多數商業應用,請始終配置 PaperOrientationDpi,以確保在不同的印表機和系統上輸出一致。 這三項設定對文件的外觀和可讀性影響最大。

在使用基於對話方塊的列印時,可以使用ShowPrintDialog 方法將自訂設定與使用者互動結合:

// Pre-configure settings but allow user to modify
var presetSettings = new PrintSettings
{
    PaperSize = PaperSize.A4,
    PaperOrientation = PaperOrientation.Portrait,
    Dpi = 300
};

// Show dialog with preset values
Printer.ShowPrintDialog("report.pdf", presetSettings);
// Pre-configure settings but allow user to modify
var presetSettings = new PrintSettings
{
    PaperSize = PaperSize.A4,
    PaperOrientation = PaperOrientation.Portrait,
    Dpi = 300
};

// Show dialog with preset values
Printer.ShowPrintDialog("report.pdf", presetSettings);
$vbLabelText   $csharpLabel

如何處理平台特定設定?

某些設定(例如托盤選擇)僅在 Windows 系統中可用。 使用特定於平台的功能時,請務必檢查平台相容性,並為跨平台應用程式提供回退行為。 如需排查平台特定問題,請參閱工程支援指南

常見的列印設定組合有哪些?

常用的組合包括:A4/縱向/300 DPI 用於標準文檔,A3/橫向/600 DPI 用於詳細報告,Letter/縱向/300 DPI/灰度 用於草稿列印以節省墨水。

以下範例展示了不同的場景:

// Standard office document
var standardDocument = new PrintSettings
{
    PaperSize = PaperSize.A4,
    PaperOrientation = PaperOrientation.Portrait,
    Dpi = 300
};

// Detailed engineering drawing
var technicalDrawing = new PrintSettings
{
    PaperSize = PaperSize.A3,
    PaperOrientation = PaperOrientation.Landscape,
    Dpi = 600,
    Grayscale = false
};

// Draft mode for review
var draftMode = new PrintSettings
{
    PaperSize = PaperSize.Letter,
    PaperOrientation = PaperOrientation.Portrait,
    Dpi = 150,
    Grayscale = true,
    NumberOfCopies = 5
};

// High-volume batch printing
var batchPrint = new PrintSettings
{
    PaperSize = PaperSize.A4,
    PaperOrientation = PaperOrientation.Portrait,
    Dpi = 300,
    NumberOfCopies = 100,
    Tray = "Tray 2" // Large capacity tray on Windows
};
// Standard office document
var standardDocument = new PrintSettings
{
    PaperSize = PaperSize.A4,
    PaperOrientation = PaperOrientation.Portrait,
    Dpi = 300
};

// Detailed engineering drawing
var technicalDrawing = new PrintSettings
{
    PaperSize = PaperSize.A3,
    PaperOrientation = PaperOrientation.Landscape,
    Dpi = 600,
    Grayscale = false
};

// Draft mode for review
var draftMode = new PrintSettings
{
    PaperSize = PaperSize.Letter,
    PaperOrientation = PaperOrientation.Portrait,
    Dpi = 150,
    Grayscale = true,
    NumberOfCopies = 5
};

// High-volume batch printing
var batchPrint = new PrintSettings
{
    PaperSize = PaperSize.A4,
    PaperOrientation = PaperOrientation.Portrait,
    Dpi = 300,
    NumberOfCopies = 100,
    Tray = "Tray 2" // Large capacity tray on Windows
};
$vbLabelText   $csharpLabel

如需更全面的範例和進階列印場景,請瀏覽列印文件教學課程,其中涵蓋了從開始到結束的完整列印工作流程。

在生產環境中實作列印設定時,尤其是在使用 Web.config 的 Web 應用程式中,請查看有關在 Web.config 中設定許可證金鑰的指南,以確保設定正確。

常見問題解答

如何在 C# 中設定列印設定?

若要在 C# 中設定列印設定,請實體化 IronPrint 的 PrintSettings 類別,並設定 PaperSize、PaperOrientation、Dpi、NumberOfCopies 和 Grayscale 等屬性。然後將此 PrintSettings 物件作為第二參數傳給 Print 或 ShowPrintDialog 方法。

我可以自訂哪些列印設定?

IronPrint 的 PrintSettings 類別可讓您自訂紙張大小 (A4、Letter 等)、方向 (Portrait/Landscape)、DPI 解析度、份數、印表機選擇、紙張邊界以及灰階列印選項。

如何設定紙張大小和方向?

在呼叫 Print 方法之前,請使用 IronPrint PrintSettings 物件中的 PaperSize 屬性(例如 PaperSize.A4)設定紙張尺寸,並使用 PaperOrientation 屬性(例如 PaperOrientation.Landscape)設定紙張方向。

我可以列印多份文件嗎?

是的,您可以透過設定 PrintSettings 類別中的 NumberOfCopies 屬性來列印多份副本。例如,settings.NumberOfCopies = 2 將會使用 IronPrint 列印兩份您的文件。

如何設定自訂列印邊距?

使用 PrintSettings 中的 PaperMargins 屬性與 Margins 類別設定自訂頁邊空白。例如:settings.PaperMargins = new Margins(10,10,10,10)可在使用 IronPrint 列印時設定各邊距為 10mm。

我可以用灰階來代替彩色列印嗎?

是的,透過將 PrintSettings 物件中的 Grayscale 屬性設定為 true 來啟用灰階列印。這將會在透過 IronPrint 列印時將彩色文件轉換為灰階。

Curtis Chau
技術作家

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

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 38,093 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package IronPrint
執行範例 觀看您的文件打到印表機上。