使用 IRONPRINT

C#以程式方式列印PDF(程式碼範例教學)

發佈 2025年1月30日
分享:

列印PDF檔在 .NET 中必須快速且可靠。 手動處理列印任務可能會很繁瑣,特別是在面對大量文件時。 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
Install-Package IronPrint
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronPrint
$vbLabelText   $csharpLabel

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

安裝後,確保所有依賴項已正確恢復。

.NET專案的基本設置和配置

安裝完成後,IronPrint 只需進行最少的配置即可開始運作。 首先,確保您的應用程式可以存取所需的打印機驅動程式。 然後,透過引用該庫並設置基本參數來初始化 IronPrint。 一個簡單的實現可能如下所示:

using IronPrint;
Printer.Print("sample-document.pdf");
using IronPrint;
Printer.Print("sample-document.pdf");
Imports IronPrint
Printer.Print("sample-document.pdf")
$vbLabelText   $csharpLabel

此基本設置可使應用程式立即將 PDF 檔案發送到預設的打印機。 開發人員可以透過指定不同的打印機、調整打印設定,或根據他們的需求整合其他配置來自訂行為。 安裝和設定完成後,下一步是探索 IronPrint 的核心功能,以有效管理不同的列印情境。

核心列印功能

程式化列印 PDF

IronPrint 使打印 PDF 和圖像變得簡單。 Print 方法能有效處理 PDF 檔案,直接將其發送到印表機,無需不必要的步驟。 當您需要快速且高效地列印 PDF 文件時,這特別有用。 一個簡單的實現看起來像這樣:

using IronPrint;
Printer.Print("invoice.pdf");
using IronPrint;
Printer.Print("invoice.pdf");
Imports IronPrint
Printer.Print("invoice.pdf")
$vbLabelText   $csharpLabel

列印圖片

對於圖像文件,IronPrint 支援常見格式如 PNG、JPEG 和 BMP。Print 方法會自動檢測和處理圖像,確保在列印時保持清晰度和解析度。

using IronPrint;
Printer.Print("logo.png");
using IronPrint;
Printer.Print("logo.png");
Imports IronPrint
Printer.Print("logo.png")
$vbLabelText   $csharpLabel

單一文件中的多個圖像會按順序處理。 IronPrint 確保格式保持完好,防止失真或質量損失。 這使其成為列印複雜報告或圖形文件以及 PDF 文件的理想選擇。

靜默列印 vs. 列印對話框

IronPrint 讓開發者可以控制文件的打印方式。 靜默列印直接將 PDF 文件發送到印表機,無需用戶輸入。 這對於自動化批次列印的應用程式非常有用,能減少中斷並加快工作流程。 您也可以高效列印多個 PDF 檔案。

using IronPrint;
Printer.Print("report.pdf");
using IronPrint;
Printer.Print("report.pdf");
Imports IronPrint
Printer.Print("report.pdf")
$vbLabelText   $csharpLabel

或者,如果用戶需要在列印前調整設定,IronPrint 可以觸發列印對話框。 這允許使用者選擇印表機、設定頁面方向,並在完成列印工作之前調整列印質量。

using IronPrint;
Printer.ShowPrintDialog("document.pdf");
using IronPrint;
Printer.ShowPrintDialog("document.pdf");
Imports IronPrint
Printer.ShowPrintDialog("document.pdf")
$vbLabelText   $csharpLabel

靜默列印最適合不需要用戶干預的自動化工作流程,當需要自訂時,列印對話框則是理想選擇。 這兩種方法都能根據應用程式的需求確保靈活性和效率。 通過利用這些功能,開發人員可以在其 .NET 應用程式中整合自動化和使用者友好的列印功能,確保順利處理列印 pdf 文件。

高級列印設定

紙張配置

使用IronPrint,開發者可以自訂紙張大小和方向以符合應用程式的需求。 這對於在非標準紙張格式上列印或需要特定版面設計時特別有用。目前的列印文件對象允許開發人員有效地管理這些配置。 若要設定紙張大小和方向,只需初始化 PrintSettings 類別並指定所需屬性:

using IronPrint;

PrintSettings printSettings = new PrintSettings
{
    PaperSize = PaperSize.A4,
    PaperOrientation = PaperOrientation.Landscape
};

Printer.Print("document.pdf", printSettings);
using IronPrint;

PrintSettings printSettings = new PrintSettings
{
    PaperSize = PaperSize.A4,
    PaperOrientation = PaperOrientation.Landscape
};

Printer.Print("document.pdf", printSettings);
Imports IronPrint

Private printSettings As New PrintSettings With {
	.PaperSize = PaperSize.A4,
	.PaperOrientation = PaperOrientation.Landscape
}

Printer.Print("document.pdf", printSettings)
$vbLabelText   $csharpLabel

列印品質與複本數

為了更好地控制列印品質,開發人員可以調整 DPI(每英寸點數)設定以確保文件以正確的細節層次列印。 更高的 DPI 設置可提高清晰度,但可能會增加列印時間。此外,您可以指定要列印的副本數量:

using IronPrint;

PrintSettings printSettings = new PrintSettings
{
    Dpi = 300,
    NumberOfCopies = 2
};

Printer.Print("report.pdf", printSettings);
using IronPrint;

PrintSettings printSettings = new PrintSettings
{
    Dpi = 300,
    NumberOfCopies = 2
};

Printer.Print("report.pdf", printSettings);
Imports IronPrint

Private printSettings As New PrintSettings With {
	.Dpi = 300,
	.NumberOfCopies = 2
}

Printer.Print("report.pdf", printSettings)
$vbLabelText   $csharpLabel

印表機選擇

開發人員可以指定使用哪個打印機,而不是依賴系統的默認打印機。 這在有多台印表機的辦公環境中特別有用:

using IronPrint;

PrintSettings printSettings = new PrintSettings
{
    // Specified Printer name
    PrinterName = "Your Printer Name"
};

// Input PDF file for PDF printing process
Printer.Print("invoice.pdf", printSettings);
using IronPrint;

PrintSettings printSettings = new PrintSettings
{
    // Specified Printer name
    PrinterName = "Your Printer Name"
};

// Input PDF file for PDF printing process
Printer.Print("invoice.pdf", printSettings);
Imports IronPrint

Private printSettings As New PrintSettings With {.PrinterName = "Your Printer Name"}

' Input PDF file for PDF printing process
Printer.Print("invoice.pdf", printSettings)
$vbLabelText   $csharpLabel

邊距和灰階列印

自訂邊距有助於確保文件的正確對齊,而灰階列印則可以在無需顏色時節省墨水。 邊距以毫米為單位進行精確調整:

using IronPrint;

PrintSettings printSettings = new PrintSettings
{
    PaperMargins = new Margins
    {
        Top = 10,
        Right = 10,
        Bottom = 10,
        Left = 10
    },
    Grayscale = true
};

Printer.Print("draft.pdf", printSettings);
using IronPrint;

PrintSettings printSettings = new PrintSettings
{
    PaperMargins = new Margins
    {
        Top = 10,
        Right = 10,
        Bottom = 10,
        Left = 10
    },
    Grayscale = true
};

Printer.Print("draft.pdf", printSettings);
Imports IronPrint

Private printSettings As New PrintSettings With {
	.PaperMargins = New Margins With {
		.Top = 10,
		.Right = 10,
		.Bottom = 10,
		.Left = 10
	},
	.Grayscale = True
}

Printer.Print("draft.pdf", printSettings)
$vbLabelText   $csharpLabel

通過利用這些先進的列印設定,開發人員可以微調列印過程以滿足特定需求,實現一致且專業的輸出品質。

檢索列印機資訊

IronPrint允許開發人員檢索系統上所有可用的打印機列表。 這在用戶需要選擇特定打印機以開始打印作業的應用程序中非常有用。 GetPrinterNames 方法返回安裝的印表機名稱陣列。

using IronPrint;

var printers = Printer.GetPrinterNames();
foreach (var printer in printers)
{
    Console.WriteLine(printer); // Outputs: OneNote (Desktop), Microsoft Print to PDF
}
using IronPrint;

var printers = Printer.GetPrinterNames();
foreach (var printer in printers)
{
    Console.WriteLine(printer); // Outputs: OneNote (Desktop), Microsoft Print to PDF
}
Imports IronPrint

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 提供一個免費試用讓開發人員在承諾之前探索其全部功能。 其授權價格從 $749 起,為各種規模的企業提供具有成本效益且可擴展的解決方案。

Kannaopat Udonpant

坎納帕特·烏頓潘

軟體工程師

 LinkedIn

在成為軟體工程師之前,Kannapat 在日本北海道大學完成了環境資源博士學位。在攻讀學位期間,Kannapat 也成為了車輛機器人實驗室的成員,該實驗室隸屬於生物生產工程學系。2022 年,他利用自己的 C# 技能,加入了 Iron Software 的工程團隊,專注於 IronPDF 的開發。Kannapat 珍視這份工作,因為他可以直接向負責撰寫大部分 IronPDF 程式碼的開發人員學習。除了同儕學習外,Kannapat 還享受在 Iron Software 工作的社交方面。當他不在撰寫程式碼或文件時,Kannapat 通常會在 PS5 上玩遊戲或重看《最後生還者》。
< 上一頁
如何在 C# Windows 應用程式中列印 PDF 檔案
下一個 >
C# 將 PDF 發送到打印機(步驟指南)