如何在 C# 中設定列印文件的列印設定(IronPrint)
使用 IronPrint 的 PrintSettings 類別在 C# 中設定列印選項,以控制紙張尺寸、方向、DPI、邊距等設定。 只需實例化 PrintSettings,設定您的偏好設定,並將其傳遞給 Print 方法。
快速入門:設定列印選項
- 透過 NuGet 安裝 IronPrint:
Install-Package IronPrint - 將
using IronPrint;加入檔案中 - 建立一個
PrintSettings物件 - 設定諸如
NumberOfCopies及Grayscale等屬性 - 將設定傳遞至
Printer.Print()或Printer.ShowPrintDialog()
-
使用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()`
- 請確認列印出的文件與您的設定相符
如何設定列印設定?
若要設定列印設定,請實體化 PrintSettings class 並根據您的喜好進行設定。 在 Print 或 ShowPrintDialog 方法中,請將 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)
為什麼我需要設定列印設定?
列印設定是指決定如何列印文件或內容的配置或參數集。 這些設定包括紙張大小、方向(直印或橫印)、列印解析度(每英吋點數 - 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)
如果我沒有指定列印設定會如何?
如果沒有指定列印設定,IronPrint 會使用系統預設印表機的預設設定,這可能與您的預期輸出格式或品質要求不符。 若要發現系統上可用的印表機,請使用 GetPrinterNames 方法 以程式化的方式擷取所有連接的印表機。
有哪些列印設定可用?
探索以下所有可用的列印設定選項。 完整的 API 參考為每個屬性和方法提供詳細的說明文件:
| 設定 | 說明 | 預設值 | 備註 |
|---|---|---|---|
| 預設設定 | 以預設值初始化 IronPrint.PrintSettings 類的新實例 | 不適用 | 不適用 |
| 紙張大小 | 設定印表機使用的紙張尺寸 | IronPrint.PaperSize.PrinterDefault | 不適用 |
| 文件方向 | 指定紙張方向(例如:直印或橫印) | IronPrint.PaperOrientation.Portrait | 不適用 |
| Dpi | 代表預期的列印解析度(以每英吋點數為單位 | 300 | 實際用於列印的 DPI 可能會受到印表機功能的限制 |
| 份數 | 表示列印文件時產生的相同複本數量 | 1 | 在某些平台中,可能存在無法精確複製多份的限制。 在此類情況下,IronPrint.PrintSettings.NumberOfCopies 的指定值可能會被忽略,導致僅列印一份副本 |
| 印表機名稱 | 指定用於列印的印表機名稱 | 空(使用作業系統預設印表機) | 如果您在 PrintDialog 中選擇印表機,此設定將會被忽略。 若要取得可用的印表機名稱,您可以使用 IronPrint.Printer.GetPrinterNames 或 IronPrint.Printer.GetPrinterNamesAsync 來擷取印表機名稱清單 |
| 紙邊距 | 以毫米為單位設定列印時使用的邊距 | 空(使用印表機預設邊距) | 不適用 |
| 灰階 | 表示是否以灰階列印 | 錯誤(嘗試彩色列印) | 不適用 |
| 扁平化。 | 在列印前將 PDF 壓平,這對於顯示表格欄位值和圖片非常有用 | 虛假 | 不適用 |
| 托盤 | 用於列印工作的印表機托架。 這可讓使用者指定特定的紙盤,從該紙盤將紙張送入印表機 | 空(使用印表機預設托盤) | 如果您在 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)
如何處理特定平台的設定?
某些設定(如托盤選擇)僅在 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
}
如需更全面的範例和進階列印情境,請探索 列印文件教學,其中涵蓋從頭到尾的完整列印工作流程。
在生產環境中實施列印設定時,尤其是在使用 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 列印時將彩色文件轉換為灰階。

