跳至頁尾內容
使用 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");
$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");
$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");
$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");
$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");
$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);
$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);
$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);
$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);
$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
}
$vbLabelText   $csharpLabel

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

結論

Csharp Print Pdf Programatically 3 related to 結論

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

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

常見問題解答

如何使用 C# 以程式方式列印 PDF 文件?

您可以使用 IronPrint 在 C# 中以程式設計方式列印 PDF 檔案。透過 NuGet 套件管理器將 IronPrint 整合到您的 .NET 專案中,您可以以最少的設定實現列印過程的自動化。

使用 .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# 中的自動列印,從而提高了準確性和使用者體驗。

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。