如何在 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 設定進行設定和列印

透過建立 PrintSettings 物件開始,設定紙張大小、方向、DPI、複本和灰階等屬性。 然後呼叫Printer.Print(...)即可立即套用這些設定-無需複雜的設定。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 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 class 並根據您的喜好進行設定。 在 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);
' Import the necessary namespace for IronPrint
Imports IronPrint

' Initialize a new instance of the PrintSettings class
Private settings As 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);
Imports IronPrint

' Example: Printing invoices with business requirements

' Invoice printing with specific business settings
Dim invoiceSettings As New PrintSettings With {
    .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.PrintSettings 類的新實例 不適用 不適用
紙張大小 設定印表機使用的紙張尺寸 IronPrint.PaperSize.PrinterDefault 不適用
文件方向 指定紙張方向(例如:直印或橫印) IronPrint.PaperOrientation.Portrait 不適用
Dpi 代表預期的列印解析度(以每英吋點數為單位 300 實際用於列印的 DPI 可能會受到印表機功能的限制
份數 表示列印文件時產生的相同複本數量 1 在某些平台中,可能存在無法精確複製多份的限制。 在這種情況下,IronPrint.PrintSettings.NumberOfCopies 的指定值可能會被忽略,導致只列印一份。
印表機名稱 指定用於列印的印表機名稱 空(使用作業系統預設印表機) 如果您在 PrintDialog 中選擇印表機,此設定將會被忽略。 若要取得可用的印表機名稱,您可以使用 IronPrint.Printer.GetPrinterNamesIronPrint.Printer.GetPrinterNamesAsync 取得印表機名稱清單
紙邊距 以毫米為單位設定列印時使用的邊距 空(使用印表機預設邊距) 不適用
灰階 表示是否以灰階列印 錯誤(嘗試彩色列印) 不適用
扁平化 在列印前將 PDF 壓平,這對於顯示表格欄位值和圖片非常有用 虛假 不適用
托盤 用於列印工作的印表機托架。 這可讓使用者指定特定的紙盤,從該紙盤將紙張送入印表機 空(使用印表機預設托盤) 如果您在 PrintDialog 中選擇托盤,此設定將會被忽略。 若要取得可用紙盒,可以使用IronPrint.Printer.GetPrinterTrays(System.String)IronPrint.Printer.GetPrinterTraysAsync(System.String) 。 此托盤選擇屬性僅適用於 Windows

哪些列印設定應該經常設定?

對於大多數的商業應用程式,請務必設定 PaperSize, PaperOrientation, 和 Dpi 以確保不同印表機和系統的輸出一致。 這三種設定對文件的外觀和可讀性影響最大。

在使用基於對話方塊的列印時,請使用 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);
Imports System

' Pre-configure settings but allow user to modify
Dim presetSettings As New PrintSettings With {
    .PaperSize = PaperSize.A4,
    .PaperOrientation = PaperOrientation.Portrait,
    .Dpi = 300
}

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

如何處理特定平台的設定?

某些設定(如托盤選擇)僅在 Windows 上可用。 使用特定平台功能時,務必檢查平台相容性,並為跨平台應用程式提供備用行為。 有關特定平台問題的疑難排解,請參閱 工程支援指南

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

常見的組合包括用於標準文件的 A4/Portrait/300 DPI、用於詳細報告的 A3/Landscape/600 DPI,以及用於草稿列印以節省油墨的 Letter/Portrait/300 DPI/Grayscale。

以下是一個展示不同情境的範例:

// 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
};
Imports System

' Standard office document
Dim standardDocument As New PrintSettings With {
    .PaperSize = PaperSize.A4,
    .PaperOrientation = PaperOrientation.Portrait,
    .Dpi = 300
}

' Detailed engineering drawing
Dim technicalDrawing As New PrintSettings With {
    .PaperSize = PaperSize.A3,
    .PaperOrientation = PaperOrientation.Landscape,
    .Dpi = 600,
    .Grayscale = False
}

' Draft mode for review
Dim draftMode As New PrintSettings With {
    .PaperSize = PaperSize.Letter,
    .PaperOrientation = PaperOrientation.Portrait,
    .Dpi = 150,
    .Grayscale = True,
    .NumberOfCopies = 5
}

' High-volume batch printing
Dim batchPrint As New PrintSettings With {
    .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 bots,將他對技術的熱愛與創意結合。

準備好開始了嗎?
Nuget 下載 37,046 | 版本: 2026.2 剛剛發布