跳過到頁腳內容
使用 IRONPRINT

C# 程式化打印 PDF(代碼示例教程)

在 .NET 中列印PDF 文件必須快速可靠。 手動處理列印任務可能很繁瑣,尤其是在處理大量文件時。 IronPrint是一個 .NET 列印庫,它透過確保發票、報告和圖像能夠流暢且無不必要的複雜性來消除這種麻煩。 它允許開發人員自動化列印工作流程,而無需處理不可預測的印表機行為或耗時的配置。

企業依靠自動化列印來製作發票、報告和運輸標籤。 部分使用者也出於業務原因需要列印特定頁面。 手動列印會引入不必要的步驟和錯誤,從而減慢工作流程。 透過整合 IronPrint,開發人員可以消除這些低效之處,自動執行重複性任務,並提高準確性。 這樣一來,需要及時取得格式良好的文件的最終使用者就能獲得更流暢的體驗。

IronPrint 提供了一種高效的方式來管理 PDF 和影像,從而簡化了列印流程。 開發人員無需手動處理複雜的印表機設置,只需進行最少的設置即可將文件傳送到印表機。這節省了時間,使他們能夠專注於應用程式的核心功能,同時確保文件每次都能正確列印。下一步是在您的 .NET 專案中設定 IronPrint。 該過程很簡單,一旦集成,即可確保每次列印都一致且可靠。

IronPrint 入門指南

Csharp Print Pdf Programatically 1 related to IronPrint 入門指南

透過 NuGet 套件管理器進行安裝流程

安裝 IronPrint 非常簡單。 將其新增至 .NET 專案中最簡單的方法是透過NuGet 套件管理器

  • 在 Visual Studio 中開啟您的專案。
  • 導航至NuGet 套件管理器
  • 搜尋"IronPrint" 。 點選"安裝" ,剩下的就交給 Visual Studio 處理。

如果您喜歡使用軟體包管理器控制台,請使用以下命令:

Install-Package IronPrint

Csharp Print Pdf Programatically 2 related to 透過 NuGet 套件管理器進行安裝流程

安裝完成後,請確保所有相依性都已正確復原。

.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")
$vbLabelText   $csharpLabel

此基本設定使應用程式能夠立即將 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")
$vbLabelText   $csharpLabel

列印影像

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")
$vbLabelText   $csharpLabel

依序處理單一文件中的多張影像。 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")
$vbLabelText   $csharpLabel

或者,如果使用者需要在列印前調整設置,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")
$vbLabelText   $csharpLabel

靜默列印最適合不需要使用者介入的自動化工作流程,而列印對話方塊則是需要自訂時的理想選擇。 兩種方法都能根據應用需求確保靈活性和效率。 透過利用這些功能,開發人員可以在其 .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)
$vbLabelText   $csharpLabel

列印品質和影印份數

為了更好地控制列印質量,開發人員可以調整 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)
$vbLabelText   $csharpLabel

印表機選擇

開發人員可以指定要使用的印表機,而不是依賴系統的預設印表機。 這在配備多台印表機的辦公環境中尤其有用:

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)
$vbLabelText   $csharpLabel

頁邊距和灰階列印

自訂邊距有助於確保文件正確對齊,而灰階列印可以在不需要顏色時節省墨水。 為了精確調整,裕量以毫米為單位定義:

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)
$vbLabelText   $csharpLabel

利用這些高級列印設置,開發人員可以微調列印過程,以滿足對一致且專業品質輸出的特定要求。

取得印表機資訊

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
$vbLabelText   $csharpLabel

此實作會取得可用的印表機名稱並將其列印到控制台。 開發者可以使用這些資料來填入下拉式選單,或在應用程式中動態分配印表機首選項。

結論

Csharp Print Pdf Programatically 3 related to 結論

IronPrint 透過提供強大且靈活的解決方案來處理 PDF 和影像,徹底革新了 .NET 應用程式中的程式化列印。 IronPrint 的進階列印設定使用戶能夠精確控制紙張尺寸、列印品質、邊距和灰階選項。 此外,它動態檢索印表機資訊的能力增強了可用性,使應用程式能夠適應不同的環境而無需人工幹預。

IronPrint 能夠有效率且準確地管理列印相關任務,開發人員可以將精力集中在完善核心應用程式功能和提供無縫功能上。 IronPrint 為開發者提供免費試用版,以便他們在正式購買前探索其全部功能。 其授權價格從$liteLicense起,為各種規模的企業提供經濟高效且可擴展的解決方案。

常見問題解答

如何使用 C# 程式化列印 PDF?

您可以使用 IronPrint 以 C# 程式化方式列印 PDF。透過 NuGet Package Manager 將 IronPrint 整合到您的 .NET 專案中,您只需最少的設定即可自動執行列印程序。

使用 .NET 列印函式庫處理 PDF 有什麼好處?

使用 IronPrint for .NET 之類的 .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 for .NET 可以無縫集成到現有的 .NET 應用程式中,通過自動化 PDF 和圖像的打印提高工作流程效率。

在 C# 中使用 IronPrint 進行自動列印有哪些優勢?

IronPrint 透過提供靜音列印、進階列印設定客製化和簡易整合等功能,簡化了 C# 語言的自動化列印,提升了精確度和使用者體驗。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。