跳過到頁腳內容
使用 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 等常見影像檔案格式。它採用的自動偵測和處理方法能夠確保影像在列印時保持清晰度和解析度。

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。通過在您的 .NET 專案中整合 IronPrint,利用 NuGet 套件管理員進行自動列印以達到最小設置。

使用 .NET 列印函式庫列印 PDF 的優勢是什麼?

使用像 IronPrint 這樣的 .NET 列印函式庫有助於自動化列印過程,減少手動錯誤並提高效率。支持靜默列印、列印對話框選項、高級自定義以及與現有應用程式的無縫整合。

如何設置 PDF 的靜默列印?

使用 IronPrint,您可以啟用靜默列印,將文件直接傳送到印表機而無需用戶互動。這非常適合於 C# 應用程式中的自動批次列印。

我可以使用 .NET 函式庫自定義列印設置,例如紙張大小和方向嗎?

是的,IronPrint 允許 extensive 自定義列印設置,包括紙張大小、方向、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
技術作家

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

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

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me