
C# 程式化列印 PDF(程式碼範例教程)
IronPrint 是 Iron Software 全新的 .NET 列印程式庫,可相容於各種平台,包括 Windows、macOS、Android 和 iOS。立即開始使用 IronPrint!
在.NET中列印PDF 必須快速且可靠。 手動處理列印任務可能會很繁瑣,尤其是在文件量很大的情況下。 IronPrint,一個.NET列印程式庫,透過確保發票、報告和圖像順暢列印且不需要不必要的複雜性來消除這個麻煩。 它允許開發者在不需處理不可預測的印表機行為或耗時的配置的情況下,自動化他們的列印工作流程。
企業依賴自動列印來處理發票、報告和運送標籤。 一些使用者基於商業原因也希望列印特定頁面。 手動列印引入了降低工作流程速度的不必要步驟和錯誤。 透過整合IronPrint,開發者移除這些低效,實現自動化重複任務,並提高準確性。 這為需要及時、格式良好的文件的最終使用者帶來了更加順暢的體驗。
IronPrint提供了一種有效管理PDF和圖像的簡化列印方式。 開發者可以不必手動處理複雜的印表機設定,而將文件傳送至印表機,所需的設置最少。這為專注於核心應用程式功能騰出時間,同時確保文件每次都正確列印。下一步是將IronPrint設置在您的.NET專案中。 這個過程很簡單,而且一經整合,它確保每次都能持續且可靠地列印。
開始使用IronPrint

透過NuGet套件管理員進行安裝
安裝IronPrint很簡單。 將其新增到您的.NET專案的最簡單方法是透過NuGet套件管理員:
- 在Visual Studio中打開您的專案。
- 進入NuGet套件管理員。
- 搜尋**"IronPrint"**。
- 按下安裝,讓Visual Studio處理其餘部分。
對偏好使用套件管理員控制台的使用者,請使用下列命令:

安裝完成後,確保所有依賴項都正確還原。
在.NET專案中進行基本設置和配置
安裝後,IronPrint只需最少的配置即可開始運行。 首先,確保您的應用程式能存取所需的印表機驅動程式。 然後,透過引用該程式庫並設置基本參數來初始化IronPrint。 一個簡單的實施可能看起來像這樣:
// Import the IronPrint namespace to access its functionalities.
using IronPrint;
// Use the Printer object to send a PDF file to the default printer.
Printer.Print("sample-document.pdf");' Import the IronPrint namespace to access its functionalities.
Imports IronPrint
' Use the Printer object to send a PDF file to the default printer.
Printer.Print("sample-document.pdf")此基本設定使應用程式能夠立即將PDF文件發送到預設印表機。 開發者可以透過指定不同的印表機、調整列印設置或根據需求整合其他配置來自訂行為。 隨著安裝和設置的完成,下一步是探討IronPrint的核心功能,以有效地管理不同的列印場景。
核心列印功能
程式化列印PDF
IronPrint使列印PDF和圖像變得簡單。 Print 方法高效處理PDF文件,將它們直接發送到印表機,而無需不必要的步驟。 這在您需要快速而高效地列印PDF文件時特別有用。 一個簡單的實現看起來像這樣:
using IronPrint;
// Print a PDF document using the default settings.
Printer.Print("invoice.pdf");Imports IronPrint
' Print a PDF document using the default settings.
Printer.Print("invoice.pdf")列印圖像
對於圖像文件,IronPrint支持PNG、JPEG和BMP等常見格式。Print 方法能自動偵測和處理圖像,確保它們在列印時保留清晰度和解析度。
using IronPrint;
// Print an image file, ensuring clarity and resolution are maintained during the process.
Printer.Print("logo.png");Imports IronPrint
' Print an image file, ensuring clarity and resolution are maintained during the process.
Printer.Print("logo.png")單一文件中的多個圖片將按順序處理。 IronPrint確保格式保持完好,防止失真或質量下降。 這使得它非常適合在PDF文件旁列印複雜的報告或圖形文件。
靜默列印與列印對話框
IronPrint讓開發者控制文件的列印方式。 靜默列印直接將PDF文件發送到印表機而不需要使用者輸入。 這對於自動批次列印的應用程式很有用,減少中斷並加快工作流程。 您也可以高效列印多個PDF文件。
using IronPrint;
// Silent printing of a PDF without requiring user input.
Printer.Print("report.pdf");Imports IronPrint
' Silent printing of a PDF without requiring user input.
Printer.Print("report.pdf")或者,如果使用者需要在列印之前調整設置,IronPrint可以觸發列印對話框。 這讓使用者可以選擇印表機、設置頁面方向和調整列印質量,然後再最終列印。
using IronPrint;
// Display a print dialog for user configuration before printing the document.
Printer.ShowPrintDialog("document.pdf");Imports IronPrint
' Display a print dialog for user configuration before printing the document.
Printer.ShowPrintDialog("document.pdf")靜默列印最適合不需要使用者干預的自動化工作流程,而列印對話框在需要自定義時很理想。 根據應用程式的需求,這兩種方法都能確保靈活性和效率。 通過利用這些功能,開發者可以在其 .NET 應用程式中整合自動化和人性化的列印功能,確保順利處理列印PDF文件。
高級列印設置
紙張配置
使用IronPrint,開發者可以自訂紙張尺寸和方向以符合應用程式的需求。 這對於列印於非標準紙張格式或需要特定佈局時特別有用。 當前列印文件物件允許開發者有效管理這些配置。 要設置紙張大小和方向,只需初始化PrintSettings 類別並指定所需屬性:
using IronPrint;
// Configure paper settings such as size and orientation.
PrintSettings printSettings = new PrintSettings
{
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Landscape
};
// Print the document using the specified settings.
Printer.Print("document.pdf", printSettings);Imports IronPrint
' Configure paper settings such as size and orientation.
Private printSettings As New PrintSettings With {
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Landscape
}
' Print the document using the specified settings.
Printer.Print("document.pdf", printSettings)列印質量和副本數量
為了更好地控制列印質量,可調整DPI(每英寸點數)設置以確保文件以正確的細節列印。 較高的DPI設定能提高清晰度,但可能會增加列印時間。此外,您可以指定要列印的副本數量:
using IronPrint;
// Configure print settings for quality and number of copies.
PrintSettings printSettings = new PrintSettings
{
Dpi = 300,
NumberOfCopies = 2
};
// Print the document with the specified settings.
Printer.Print("report.pdf", printSettings);Imports IronPrint
' Configure print settings for quality and number of copies.
Private printSettings As New PrintSettings With {
.Dpi = 300,
.NumberOfCopies = 2
}
' Print the document with the specified settings.
Printer.Print("report.pdf", printSettings)印表機選擇
開發者可以指定使用哪台印表機,而不是依賴系統的預設印表機。 這在辦公環境中特別有用,當有多台印表機可用時:
using IronPrint;
// Specify the printer to use for printing the document.
PrintSettings printSettings = new PrintSettings
{
PrinterName = "Your Printer Name"
};
// Print the document using the specified printer.
Printer.Print("invoice.pdf", printSettings);Imports IronPrint
' Specify the printer to use for printing the document.
Private printSettings As New PrintSettings With {.PrinterName = "Your Printer Name"}
' Print the document using the specified printer.
Printer.Print("invoice.pdf", printSettings)頁邊距和灰階列印
自訂頁邊距有助於確保文件正確對齊,而灰階列印可在不需要顏色時節省墨水。 頁邊距以毫米為單位定義,以便進行精確調整:
using IronPrint;
// Configure print settings for margins and grayscale printing.
PrintSettings printSettings = new PrintSettings
{
PaperMargins = new Margins
{
Top = 10,
Right = 10,
Bottom = 10,
Left = 10
},
Grayscale = true
};
// Print the document using the specified settings.
Printer.Print("draft.pdf", printSettings);Imports IronPrint
' Configure print settings for margins and grayscale printing.
Private printSettings As New PrintSettings With {
.PaperMargins = New Margins With {
.Top = 10,
.Right = 10,
.Bottom = 10,
.Left = 10
},
.Grayscale = True
}
' Print the document using the specified settings.
Printer.Print("draft.pdf", printSettings)藉由利用這些高級列印設置,開發者可以對列印過程進行微調,以滿足對一致性和專業品質輸出的具體要求。
檢索印表機資訊
IronPrint允許開發者檢索系統上所有可用印表機的列表。 這在需要使用者在啟動列印工作之前選擇特定印表機的應用程式中很有用。 GetPrinterNames 方法返回安裝的印表機名稱陣列。
using IronPrint;
// Retrieve and display the names of all available printers.
var printers = Printer.GetPrinterNames();
foreach (var printer in printers)
{
Console.WriteLine(printer); // Outputs: OneNote (Desktop), Microsoft Print to PDF
}Imports IronPrint
' Retrieve and display the names of all available printers.
Private printers = Printer.GetPrinterNames()
For Each printer In printers
Console.WriteLine(printer) ' Outputs: OneNote (Desktop), Microsoft Print to PDF
Next printer此實施不僅能取得可用的印表機名稱,還能將它們列印到控制台上。 開發者可以使用這些資料來填充下拉選單或在應用程式中動態指派印表機偏好設定。
結論
IronPrint通過提供一個強大而靈活的解決方案來處理PDF和圖像,徹底改變了.NET應用程式中的程式化列印。 IronPrint的高級列印設定賦予使用者對紙張尺寸、列印質量、頁邊距和灰階選項的精確控制。 此外,其動態檢索印表機資訊的能力提高了使用性,使應用程式不需要手動干預就能適應不同環境。
隨著IronPrint高效而準確地管理列印相關的任務,開發者可以專注於完善核心應用程式的功能,提供無縫的功能性。 IronPrint提供免費試用,讓開發者在承諾之前探索其完整功能。 其授權起價為**$999**,為各類規模的企業提供具成本效益且可擴展的解決方案。

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

