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

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

安裝完成後,確保所有依賴項都正確還原。
在.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.
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");
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");
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");
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");
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);
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);
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);
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);
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
}
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提供免費試用,讓開發者在承諾之前探索其完整功能。 其授權起價為$liteLicense,為各類規模的企業提供具成本效益且可擴展的解決方案。
常見問題
我如何在C#中以程式方式列印PDF?
您可以使用IronPrint來在C#中以程式方式列印PDF。透過在您的.NET專案中整合IronPrint(通過NuGet套件管理員),您可以用最少的設定來自動化列印過程。
.NET列印程式庫使用PDF的優勢是什麼?
使用像IronPrint這樣的.NET列印程式庫有助於自動化列印過程,減少人工錯誤並提高效率。它支持靜默列印、列印對話框選項、高級自定義功能,並能無縫整合到現有的應用中。
我如何為PDF設置靜默列印?
使用IronPrint,您可以啟用靜默列印,將文件直接發送到印表機而不需使用者的互動。這是C#應用中自動批次列印的理想選擇。
我能否使用.NET程式庫自訂例如紙張大小和方向等列印設置?
是的,IronPrint允許擴展列印設置的自訂,包括紙張大小、方向、DPI列印品質等,以滿足特定的應用需求。
我如何在C#應用中檢索可用的印表機列表?
您可以使用IronPrint的GetPrinterNames方法來獲取環境中可用印表機名稱的陣列,這有助於動態選擇和配置印表機。
.NET列印程式庫可以列印哪種影像格式?
IronPrint支援列印各種影像格式,如PNG、JPEG和BMP。您可以使用Printer.Print方法確保高品質影像列印。
購買前有沒有辦法試用.NET列印程式庫?
是的,IronPrint為開發者提供免費試用,讓您在做出購買決定前探索其功能。完整授權從$749開始。
.NET列印程式庫支持哪些平台?
IronPrint支持多個平台,包括Windows、macOS、Android和iOS,適合多樣化的開發環境。
我如何將.NET列印程式庫整合到現有應用中?
IronPrint可以無縫整合到現有.NET應用中,通過自動化PDF和影像的列印過程提高工作流效率。
在C#中使用IronPrint進行自動列印的優點是什麼?
IronPrint通過提供像靜默列印、高級列印設定自訂和易於整合的功能來簡化C#中的自動列印,提升精確度和使用者體驗。


