如何在C#中使用IronPrint配置列印設定
在C#中使用IronPrint的PrintSettings類別配置列印設定,以控制紙張大小、方向、DPI、邊距等。 只需實例化Print方法。
- 通過NuGet安裝IronPrint:
Install-Package IronPrint - 將
using IronPrint;新增到文件中 - 建立一個
PrintSettings物件 - 設置屬性如
Grayscale - 將設置傳入
Printer.ShowPrintDialog()
-
1Install IronPrint with NuGet Package Manager
-
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部署以在您的實時環境中測試
今天就開始在您的專案中使用IronPrint,透過免費試用
最小化工作流程(5 步驟)
- 安裝 IronPrint C# 列印程式庫
- 建立一個
PrintSettings物件 - 配置紙張大小、方向、DPI、份數等等
- 將設置傳入
Printer.Print()或Printer.ShowPrintDialog() - 確認列印的文件與您配置的設置匹配
如何設置列印設定?
要配置列印設定,實例化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);' 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)為什麼我需要配置列印設定?
列印設定指的是一個配置或參數集,指導如何列印文件或內容。 這些設置包括諸如紙張大小、方向(縱向或橫向)、列印解析度(每英寸點數 - 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);Imports IronPrint
' 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)如果我不指定列印設定會怎麼樣?
如果沒有指定列印設定,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);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)如何處理特定平台的設置?
某些設置如匣選擇僅在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
};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
}欲獲取更全面的範例及高級列印場景,探索列印文件教學,其中涵蓋了從開始到結束的完整列印工作流程。
在生產環境中實施列印設置時,特別是在使用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擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。