IRONSOFTWAREHOME

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

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

在C#中使用IronPrint的PrintSettings類別配置列印設定,以控制紙張大小、方向、DPI、邊距等。 只需實例化Print方法。

快速開始:配置列印設定
  1. 通過NuGet安裝IronPrint:Install-Package IronPrint
  2. using IronPrint;新增到文件中
  3. 建立一個PrintSettings物件
  4. 設置屬性如Grayscale
  5. 將設置傳入Printer.ShowPrintDialog()
  1. 1Install IronPrint with NuGet Package Manager

    PM > Install-Package IronPrint

  2. 2複製並運行這段程式碼片段。

    using IronPrint;
    
    // Print with custom settings
    Printer.Print("document.pdf", new PrintSettings
    {
        PaperSize = PaperSize.A4,
        PaperOrientation = PaperOrientation.Landscape,
        Dpi = 300,
        NumberOfCopies = 2,
        Grayscale = true
    });
    C#
  3. 3部署以在您的實時環境中測試

    今天就開始在您的專案中使用IronPrint,透過免費試用
    arrow pointer

如何設置列印設定?

要配置列印設定,實例化PrintSettings類,並根據您的偏好進行配置。 在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);

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

列印設定指的是一個配置或參數集,指導如何列印文件或內容。 這些設置包括諸如紙張大小、方向(縱向或橫向)、列印解析度(每英寸點數 - DPI)、副本數量、列印機選擇、邊距及灰階列印選項等詳情。 自定義這些設置可達到特殊列印偏好和需求。

IronPrint的綜合列印設定功能提供了開發者對列印過程每個方面的精細控制。 無論是構建桌面應用還是ASP.NET網頁應用,適當的配置確保了不同環境中的一致結果。

什麼時候應該使用自定義列印設定?

當需要對列印輸出進行精確控制時,例如列印有特定邊距的報告、生成多份文件或確保文件以正確方向列印以滿足商業需求,自定義列印設定是必需的。

以下是一個滿足特定需求的發票列印實例:

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

如果我不指定列印設定會怎麼樣?

如果沒有指定列印設定,IronPrint會使用系統預設列印機的預設設置,這可能與您期望的輸出格式或質量要求不符。 要發現系統上可用的列印機,使用GetPrinterNames方法來程式化檢索所有連接的列印機。

有哪些可用的列印設定?

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

設定描述預設值備註
DefaultSettings初始化IronPrint.PrintSettings類的新實例,使用預設值不適用不適用
PaperSize設定列印機使用的紙張大小IronPrint.PaperSize.PrinterDefault不適用
PaperOrientation指定紙張方向(例如,縱向或橫向)IronPrint.PaperOrientation.Portrait不適用
Dpi代表預期的每英寸點數列印解析度300實際列印使用的DPI可能受限於列印機的能力
NumberOfCopies指示列印文件時要生成的相同副本數1在某些平台上,可能存在無法準確再現多份副本的限制。 在這些情況下,可能忽略指定的IronPrint.PrintSettings.NumberOfCopies值,結果只列印一份
PrinterName指定用於列印的列印機名稱空值(使用作業系統的預設列印機)如果您在PrintDialog中選擇列印機,則此設置將被忽略。 若要獲取可用列印機名稱,您可以使用IronPrint.Printer.GetPrinterNamesAsync來取得列印機名稱清單
PaperMargins設定列印時使用的邊距,單位是毫米空值(使用列印機的預設邊距)不適用
Grayscale指示是否採用灰階列印false(嘗試彩色列印)不適用
Flatten在列印前將PDF扁平化,這對於顯示表單欄位數值和圖像很有用false不適用
Tray用於列印任務的列印機匣。 這使得使用者可以指定將紙張送入列印機的特定匣空值(使用列印機的預設匣)如果您在PrintDialog中選擇匣,則此設置將被忽略。 若要獲取可用的匣,您可以使用IronPrint.Printer.GetPrinterTraysAsync(System.String)。 此匣選擇屬性僅在Windows中可用

哪些列印設定我應該始終配置?

對於大多數商業應用,始終配置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);

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

某些設置如匣選擇僅在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
};

欲獲取更全面的範例及高級列印場景,探索列印文件教學,其中涵蓋了從開始到結束的完整列印工作流程。

在生產環境中實施列印設置時,特別是在使用Web.config的網頁應用中,查看在Web.config中設定授權金鑰的指南以確保正確配置。

常見問題

如何在C#中配置列印設置?

要在C#中配置列印設置,需從IronPrint實例化PrintSettings類並設置屬性,如PaperSize, PaperOrientation, Dpi, NumberOfCopies, 和 Grayscale。然後將此PrintSettings物件作為第二個參數傳遞給Print或ShowPrintDialog方法。

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

IronPrint的PrintSettings類允許您自訂紙張尺寸(A4,Letter等)、方向(直式/橫式)、DPI解析度、份數、列印機選擇、紙張邊距和灰階列印選項。

我如何設置紙張尺寸和方向?

在調用Print方法之前,使用IronPrint PrintSettings物件的PaperSize屬性(例如:PaperSize.A4)設置紙張尺寸,使用PaperOrientation屬性(例如:PaperOrientation.Landscape)設置方向。

我可以列印多份文件嗎?

可以,通過在PrintSettings類中設置NumberOfCopies屬性來列印多份。例如,settings.NumberOfCopies = 2將使用IronPrint列印您的文件兩份。

如何設置自訂的列印邊距?

在PrintSettings中使用Margins類通過PaperMargins屬性設置自訂邊距。例如:settings.PaperMargins = new Margins(10, 10, 10, 10)在使用IronPrint列印時,在所有邊設置10mm邊距。

我可以使用灰階列印而非彩色列印嗎?

可以,通過在PrintSettings物件中將Grayscale屬性設為true啟用灰階列印。這將在通過IronPrint列印時,將彩色文件轉為灰階。

How can I handle platform-specific print settings with IronPrint?

Check platform compatibility when using settings like Tray selection, which is only available on Windows. Provide fallback options for cross-platform applications.

Why is it important to configure print settings?

Configuring print settings is crucial to ensure documents print as intended with specific parameters like paper size, margins, and quality, maintaining consistency across different environments.

Can I allow users to modify print settings during the print process?

Yes, you can use the `ShowPrintDialog` method to pre-configure settings and allow users to modify them before printing.

What options are available for printing multiple copies of a document?

You can print multiple copies by setting the `NumberOfCopies` property in the `PrintSettings` class. Note that some platforms may have limitations on reproducing multiple copies.

Curtis Chau
技術作家

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

...
閱讀更多

準備好開始了嗎?

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

立即獲取免費

立即獲取 30天試用金鑰

bullet_checked無需信用卡或註冊帳號
bullet_test在生產
環境中進行測試,且不顯示浮水印
bullet_calendar30 天全
功能產品
bullet_support試用期間提供 24/5 技術
支援
立即獲取您的免費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天試用金鑰
無需信用卡或帳戶建立