如何使用 IronPrint 在 C# 中設定列印設定
使用 IronPrint 的 PrintSettings 類別在 C# 中設定列印選項,以控制紙張尺寸、方向、DPI、邊距等設定。 只需實例化 PrintSettings,設定您的偏好設定,並將其傳遞給 Print 方法。
快速入門:設定PRINT設定
- 透過 NuGet 安裝 IronPrint:
Install-Package IronPrint - 將
using IronPrint;加入檔案中 - 建立一個
PrintSettings物件 - 設定諸如
NumberOfCopies及Grayscale等屬性 - 將設定傳遞至
Printer.Print()或Printer.ShowPrintDialog()
-
using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronPrint
PM > Install-Package IronPrint -
請複製並執行此程式碼片段。
using IronPrint; // Print with custom settings Printer.Print("document.pdf", new PrintSettings { PaperSize = PaperSize.A4, PaperOrientation = PaperOrientation.Landscape, Dpi = 300, NumberOfCopies = 2, Grayscale = true }); -
部署至您的生產環境進行測試
立即透過免費試用,在您的專案中開始使用 IronPrint
簡化工作流程(5 個步驟)
- 安裝 IronPrint C# 列印程式庫
- 建立一個
PrintSettings物件 - 設定紙張尺寸、方向、DPI、份數等
- 將設定傳遞給
Printer.PRINT()或Printer.ShowPrintDialog() - 請確認列印出的文件與您的設定相符
如何設定PRINT設定?
若要設定列印設定,請建立 PrintSettings 類別的實例,並根據您的偏好進行設定。 在 Print 或 ShowPrintDialog 方法中,將 PrintSettings 物件作為第二個參數傳入。 以下程式碼範例說明此用法。 如需更詳細的範例,請參閱PRINT設定程式碼範例頁面。
// 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)
為什麼需要設定列印設定?
列印設定是指決定文件或內容應如何列印的配置或參數集。 這些設定包含紙張尺寸、方向(直式或橫式)、列印解析度(每英吋點數 - 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);
// 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)
若未指定列印設定會發生什麼情況?
若未指定列印設定,IronPrint 將採用系統預設印表機的設定,這可能不符合您預期的輸出格式或品質要求。 若要偵測系統上可用的印表機,請使用 GetPrinterNames 方法以程式化方式擷取所有已連接的印表機。
有哪些可用的PRINT設定?
請參閱下方所有可用的PRINT設定選項。 完整的 API 參考文件針對每個屬性與方法提供了詳細說明:
| 設定 | 描述 | 預設值 | 備註 |
|---|---|---|---|
DefaultSettings |
使用預設值初始化 IronPrint.PrintSettings 類別的新實例 | 不適用 | 不適用 |
PaperSize |
設定印表機使用的紙張尺寸 | IronPrint.PaperSize.PrinterDefault | 不適用 |
PaperOrientation |
指定紙張方向(例如:直向或橫向) | IronPrint.PaperOrientation.Portrait | 不適用 |
Dpi |
表示預期的列印解析度(以每英吋點數計) | 300 | 實際用於列印的 DPI 可能會受限於印表機的規格 |
NumberOfCopies |
表示列印文件時將產生的相同副本數量 | 1 | 在某些平台上,可能存在限制,導致無法精確複製多份副本。 在此類情況下,IronPrint.PrintSettings.NumberOfCopies 的指定值可能會被忽略,導致僅列印一份副本 |
PrinterName |
指定用於列印的印表機名稱 | null(使用作業系統預設印表機) | 若您在 PrintDialog 中選擇印表機,此設定將被忽略。 若要取得可用的印表機名稱,您可以使用 IronPrint.Printer.GetPrinterNames 或 IronPrint.Printer.GetPrinterNamesAsync 來擷取印表機名稱清單 |
PaperMargins |
設定列印時使用的邊距(單位為公釐) | null(使用印表機預設邊距) | 不適用 |
Grayscale |
表示是否以灰階列印 | false(嘗試彩色列印) | 不適用 |
Flatten |
在列印前請先將 PDF 檔案轉換為平面格式,此舉有助於顯示表單欄位值與圖片 | false | 不適用 |
Tray |
用於該列印工作的印表機匣。 這讓使用者能夠指定特定紙匣,讓紙張從該處送入印表機 | null(使用印表機預設紙匣) | 若您在 PrintDialog 中選擇印表匣,此設定將被忽略。 若要取得可用托盤,您可以使用 IronPrint.Printer.GetPrinterTrays(System.String) 或 IronPrint.Printer.GetPrinterTraysAsync(System.String)。 此托盤選取功能僅在 Windows 系統中提供 |
我應該始終設定哪些列印設定?
對於大多數商業應用程式,請務必設定 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)
如何處理平台特定設定?
某些設定(例如"Tray"選項)僅在 Windows 系統上可用。 使用特定平台功能時,請務必檢查平台相容性,並為跨平台應用程式提供備用處理機制。 若需排除特定平台的疑難排解,請參閱工程支援指南。
常見的PRINT設定組合有哪些?
常見的設定組合包括:標準文件採用 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
};
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
}
如需更全面的範例及進階列印情境,請參閱"列印文件教學指南",該指南涵蓋從頭到尾完整的列印工作流程。
在生產環境中實作 PRINT 設定時,特別是在使用 Web.config 的 Web 應用程式中,請參閱 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 列印兩份文件副本。
如何設定自訂的列印邊距?
請透過 Margins 類別中的 PrintSettings 物件,使用 PaperMargins 屬性設定自訂邊距。例如:settings.PaperMargins = new Margins(10, 10, 10, 10) 將在使用 IronPrint 列印時,將四邊邊距設定為 10 公釐。
我可以選擇灰階列印而非彩色列印嗎?
是的,請在 PrintSettings 物件中將 Grayscale 屬性設定為 true,即可啟用灰階列印。這將使透過 IronPrint 列印的彩色文件轉換為灰階。

