如何在 C# 中設定列印 DPI
DPI (每英寸點數) 控制列印解析度 - 打印機每英寸紙張上放置的墨點數。 較高的 DPI 可呈現更清晰的文字與更平滑的圖像,但會增加處理時間及碳粉用量。 較低的 DPI 可加快列印速度,且適用於內部草稿。 透過程式設計設定 DPI,可確保每項列印工作皆符合應用程式所需之品質標準,無論使用者預設的印表機設定為何。
IronPrint 在 PrintSettings 類上公開一個 Dpi 屬性。 我們將其設置為整數值,將設定傳遞給 Printer.Print(),並且文件將以指定的解析度列印。 預設解析度為 300 DPI,符合商業印刷的標準。
- 通過NuGet安裝IronPrint:
Install-Package IronPrint - 添加
using IronPrint;到檔案 - 創建一個
PrintSettings物件 - 將
Dpi設置為所需的解析度 (例如,300, 600, 1200) - 將設置傳遞給
Printer.Print()或Printer.ShowPrintDialog()
-
1使用 IronPrint 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
using IronPrint; // Print a PDF at 600 DPI for high-quality output Printer.Print("report.pdf", new PrintSettings { Dpi = 600 });Imports IronPrint ' Print a PDF at 600 DPI for high-quality output Printer.Print("report.pdf", New PrintSettings With { .Dpi = 600 })Imports IronPrint ' Print a PDF at 600 DPI for high-quality output Printer.Print("report.pdf", New PrintSettings With { .Dpi = 600 }) -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronPrint,並使用 免費試用
最小工作流程(5 個步驟)
- 安裝IronPrint C#列印程式庫
- 建立一個
PrintSettings物件 - 將
Dpi設定為所需解析度 - 將設定傳遞給
Printer.Print() - 執行專案以設定的 DPI 進行列印
如何在C#中設定打印DPI?
PrintSettings 上的 Dpi 屬性接受任何正整數。 預設值為 300,這是商務文件的標準解析度。 列印使用的實際DPI可能會受物理打印機的能力限制 - 如果我們在一個最高支援600DPI的打印機上設置了1200DPI,打印機驅動程序可能會替換為其最接近的支持解析度。
using IronPrint;
// Configure 300 DPI for standard office quality
var officeSettings = new PrintSettings
{
Dpi = 300
};
// Print the invoice
Printer.Print("invoice.pdf", officeSettings);using IronPrint;
// Configure 300 DPI for standard office quality
var officeSettings = new PrintSettings
{
Dpi = 300
};
// Print the invoice
Printer.Print("invoice.pdf", officeSettings);Imports IronPrint
' Configure 300 DPI for standard office quality
Dim officeSettings As New PrintSettings With {
.Dpi = 300
}
' Print the invoice
Printer.Print("invoice.pdf", officeSettings)Imports IronPrint
' Configure 300 DPI for standard office quality
Dim officeSettings As New PrintSettings With {
.Dpi = 300
}
' Print the invoice
Printer.Print("invoice.pdf", officeSettings)使用本地 .NET 控制列印解析度需要創建一個 PrintDocument,存取 DefaultPageSettings.PrinterResolution,處理 PrintPage 事件,並手動使用 Graphics.DrawImage() 渲染內容。 這涉及明顯更多的樣板代碼。 IronPrint 將此簡化為設定物件上的單一整數屬性。
不同列印工作應使用哪種 DPI?
選擇合適的 DPI 取決於內容類型與用途。 更高的解析度並非總是更好 - 它會增加假脫機大小和列印時間,對於以文本為主的文檔沒有明顯的益處。
| DPI | 最適合 | 筆記 |
|---|---|---|
| 72-150 | 內部草稿、校樣、測試列印 | 輸出速度快,碳粉用量低 |
| 300 | 商業文件、發票、報告 | IronPrint 預設; 標準商業品質 |
| 600 | 行銷資料、圖形、圖表 | 圖像明顯更清晰,細線更精細 |
| 1200+ | 攝影、檔案保存、美術 | 需搭配相容的印表機; 大型暫存檔 |
對於大多數應用場景而言,300 DPI 能在畫質與效能之間取得最佳平衡。 我們建議先採用預設設定,僅在輸出效果需要明顯提升圖像清晰度或細部細節時才進行調整。
如何將 DPI 與其他列印設定結合使用?
DPI 與其他 PrintSettings 屬性一起工作,以定義完整的列印作業。 我們可以在同一個物件中設定紙張尺寸、方向、邊距、份數以及灰階模式。
using IronPrint;
// Combine 600 DPI with landscape A4 and grayscale output
var settings = new PrintSettings
{
Dpi = 600,
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Landscape,
PaperMargins = new Margins(10, 10, 10, 10),
NumberOfCopies = 2,
Grayscale = true
};
// Print the dashboard
Printer.Print("quarterly-dashboard.pdf", settings);using IronPrint;
// Combine 600 DPI with landscape A4 and grayscale output
var settings = new PrintSettings
{
Dpi = 600,
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Landscape,
PaperMargins = new Margins(10, 10, 10, 10),
NumberOfCopies = 2,
Grayscale = true
};
// Print the dashboard
Printer.Print("quarterly-dashboard.pdf", settings);Imports IronPrint
' Combine 600 DPI with landscape A4 and grayscale output
Dim settings As New PrintSettings With {
.Dpi = 600,
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Landscape,
.PaperMargins = New Margins(10, 10, 10, 10),
.NumberOfCopies = 2,
.Grayscale = True
}
' Print the dashboard
Printer.Print("quarterly-dashboard.pdf", settings)Imports IronPrint
' Combine 600 DPI with landscape A4 and grayscale output
Dim settings As New PrintSettings With {
.Dpi = 600,
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Landscape,
.PaperMargins = New Margins(10, 10, 10, 10),
.NumberOfCopies = 2,
.Grayscale = True
}
' Print the dashboard
Printer.Print("quarterly-dashboard.pdf", settings)將 Grayscale = true 設置為600DPI會生成清晰的單色輸出,這非常適合用於圖表和數據表。 PaperMargins 的值以毫米為單位。
如何讓使用者在列印對話方塊中調整 DPI?
當我們將 PrintSettings 傳遞給 Printer.ShowPrintDialog() 時,對話框以我們預設的DPI打開。 使用者可在列印前接受設定或調整解析度。
using IronPrint;
// Pre-configure 600 DPI for the dialog
var settings = new PrintSettings
{
Dpi = 600,
PaperSize = PaperSize.Letter
};
// Open the dialog with pre-selected DPI
Printer.ShowPrintDialog("design-proof.pdf", settings);using IronPrint;
// Pre-configure 600 DPI for the dialog
var settings = new PrintSettings
{
Dpi = 600,
PaperSize = PaperSize.Letter
};
// Open the dialog with pre-selected DPI
Printer.ShowPrintDialog("design-proof.pdf", settings);Imports IronPrint
' Pre-configure 600 DPI for the dialog
Dim settings As New PrintSettings With {
.Dpi = 600,
.PaperSize = PaperSize.Letter
}
' Open the dialog with pre-selected DPI
Printer.ShowPrintDialog("design-proof.pdf", settings)Imports IronPrint
' Pre-configure 600 DPI for the dialog
Dim settings As New PrintSettings With {
.Dpi = 600,
.PaperSize = PaperSize.Letter
}
' Open the dialog with pre-selected DPI
Printer.ShowPrintDialog("design-proof.pdf", settings)對於非阻塞UI場景,Printer.ShowPrintDialogAsync() 接受相同的參數並保持應用程式的響應。對話框讓用戶在列印前確認與其打印機支持的解析度相比的DPI - 這在切換辦公室使用的600 DPI雷射打印機和1200 DPI相片打印機之間特別有用。 對於完全自動化的工作流程,不需要用戶互動,請改用 Printer.Print() 進行 靜默列印。
後續步驟
DPI 是 PrintSettings 物件上的一個整數 - 設置它以符合每個列印作業的質量要求。 商業文件起始字數為 300 字,若為圖像密集型內容則增加至 600 字或以上。
探索 列印設置指南中的所有可用屬性,查閱 Printer類API參考以獲取完整的方法訊息,並參考 程式碼示例頁面以獲得即用的程式碼片段。 IronPrint 教學 全面介紹列印生命週期,並且 更新日誌 跟蹤最近的更新。
開始免費30天試用 以在實際項目中測試DPI設置。 準備好後,查看授權選項,起價 $999。
常見問題
如何在C#中使用IronPrint程式設置列印DPI?
您可以透過在PrintSettings類別上的Dpi屬性配置,使用IronPrint在C#中設定列印DPI。為Dpi分配一個整數值,並將此設置提供給Printer.Print()或Printer.ShowPrintDialog()。預設DPI為300,適合大多數商業文件列印。
使用IronPrint時的預設列印DPI是多少?
使用IronPrint時的預設列印DPI為300。此設定符合商業文件的標準解析度,確保列印質量與速度之間達到平衡。
為什麼以程式方式在IronPrint設定列印DPI很重要?
通過IronPrint以程式方式設定列印DPI,確保在不同的作業和印表機之間維持一致的列印質量,無論使用者的預設印表機設定如何。這允許對列印文件的解析度進行精確控制。
高質量圖像或行銷材料應選擇什麼DPI?
對於高質量圖像或行銷材料,您應設定DPI為600或更高,使用IronPrint。這提供了明顯更銳利的圖像和更精細的細節,儘管它需要更多的處理能力和可能增加列印時間。
如何在IronPrint中調整DPI設置以及其他列印設置?
在IronPrint中,您可以透過配置單個PrintSettings物件上的屬性來調整DPI設置以及其他列印設置,例如紙張尺寸、方向和灰階模式。將此配置傳遞給Printer.Print()以使用所有首選設置執行列印作業。
在使用IronPrint時,使用者可以在列印對話框中修改DPI設置嗎?
可以,當PrintSettings被傳遞給Printer.ShowPrintDialog()時,使用者可以在對話框中調整DPI設定。這使得使用者可以在列印之前根據印表機的能力修改解析度。
如果我在IronPrint中設定了超過我的印表機所支持的DPI會發生什麼?
如果您在IronPrint中設定了超過印表機支持的DPI,印表機驅動程式通常會調整為最接近的支持解析度。例如,一台最高支持600 DPI的印表機將不會以1200 DPI列印。
IronPrint是否與靜默列印功能相容以支持自動化工作流程?
是的,IronPrint透過Printer.Print()方法支援靜默列印功能以支持自動化工作流程,這處理列印作業無需用戶互動,確保快速且高效的文件處理。
在IronPrint中選擇DPI設定時應考慮什麼?
選擇IronPrint中的DPI設定時,應考慮內容類型及其預定用途。雖然300 DPI適用於大多數商業文件,但600 DPI等更高解析度則更適合詳細的圖形或圖像。較高的DPI也意味著更大的文件大小和更長的列印時間。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。