跳過到頁腳內容
產品比較

PdfiumViewer 在 C# 中打印 PDF(替代教程)

PDF 文件在各種軟體應用中發揮著至關重要的作用,包括產生發票、顯示報告和共享資訊。 在 C# 中處理 PDF 時,開發人員有多種選擇。 本文探討了兩個常用的用於在 C# 中使用 Microsoft Print 列印 PDF 的庫:

  • IronPdf
  • PDFiumViewer

讓我們深入了解它們的功能和易用性,並比較它們的列印功能,以幫助您為下一個 C# 專案做出明智的決定。

IronPDF。

IronPDF。 的概述

IronPDF是一個功能強大的 C# 庫,旨在輕鬆建立、操作和處理 PDF 文件。 它提供了豐富的功能,因此成為開發人員的首選。 IronPDF 的突出之處在於它具備以下能力:

  • 將 HTML、CSS 和影像轉換為 PDF 檔案。 從頭開始產生PDF文件。
  • 編輯現有PDF文件。
  • 支援各種 PDF 文件元素,如圖像、文字、表格和表單。
  • 提供數位簽章、浮水印和加密等高級功能。
  • 無需任何第三方工具或程式庫即可實現靜默列印。
  • 提供使用者友善的介面和全面的文件。

PDFiumViewer

PDFiumViewer概述

PDFiumViewer 是另一個在 C# 中處理 PDF 的熱門選擇。 它基於開源 PDFium 專案構建,並為其功能提供了一個 .NET 封裝。 PDFiumViewer 提供:

  • 能夠在 Windows Forms 應用程式中渲染和顯示 PDF 檔案。
  • 支援在 PDF 文件中進行導覽、縮放和文字選擇。
  • 為 Windows Forms 專案提供簡單直接的整合流程。

安裝 IronPDF。

若要開始使用 IronPDF,請依照下列步驟使用 Visual Studio 中的 NuGet 套件管理器進行安裝:

  1. 開啟 Visual Studio 並建立新的控制台應用程式或開啟一個現有的控制台應用程式。

    ! 使用 C# 實作 PDFiumViewer 列印 PDF(替代教學)圖 1 - 新建專案 - PDFiumViewer 列印 PDF C#

2.在 "解決方案總管 "中的專案上按一下滑鼠右鍵,然後選擇 "管理 NuGet 套件"。

  1. 切換到"瀏覽"選項卡,搜尋"IronPDF",然後按一下"安裝"。

    ! PdfiumViewer 列印 PDF(C# 替代教學)圖 2 - IronPDF 安裝

IronPDF安裝成功後,我們就可以開始使用它來列印PDF和其他與PDF文件相關的任務了。 在此之前,我們先在系統中安裝 PDFiumViewer。

安裝 PDFiumViewer

您也可以透過 NuGet 套件管理器安裝 PDFiumViewer。 方法如下

  1. 開啟 Visual Studio 專案並建立 Windows 窗體應用程式。

    ! PdfiumViewer 列印 PDF(C# 替代教學)圖 3 - 新項目

  2. 將一個按鈕拖曳到表單中,並將其命名為"列印 PDF"。

    ! PdfiumViewer 列印 PDF(C# 教學)(替代教學)圖 4 - 列印按鈕

  3. 在解決方案資源管理器中以滑鼠右鍵按一下項目,然後選擇"管理 NuGet 套件"。
  4. 在"NuGet 套件管理員"視窗中,切換到"瀏覽"選項卡,搜尋"PDFiumViewer",然後按一下"安裝"。

    ! 使用 C# 中的 PDFiumViewer 列印 PDF(替代教學)圖 5 - PDFiumViewer 安裝

  5. 或者,要安裝 PDFium DLL,您可以搜尋"PDFiumViewer.Native",根據您的作業系統要求,選擇 32 位元或 64 位元版本。 在 Windows 窗體應用程式中使用 PDFiumViewer 載入 PDF 檔案或頁面需要此 DLL。

    ! 使用 C# 中的 PDFiumViewer 列印 PDF(替代教學)圖 6 - PDFiumViewer.Native

安裝完成後,即可開始使用 PDFiumViewer 列印 PDF 檔案和其他與 PDF 相關的任務。

使用 IronPDF 列印 PDF

使用 IronPDF列印 PDF 檔案非常簡單。 以下是示範如何在不指定印表機名稱的情況下列印 PDF 檔案的原始程式碼範例:

using IronPdf;

class Program
{
    static void Main()
    {
        // Create a new PDF renderer
        var renderer = new ChromePdfRenderer();

        // Render a PDF from a URL
        PdfDocument pdf = renderer.RenderUrlAsPdf("https://ironpdf.com");

        // Print the PDF document, 300 dpi, without printer name
        pdf.Print(300, true);
    }
}
using IronPdf;

class Program
{
    static void Main()
    {
        // Create a new PDF renderer
        var renderer = new ChromePdfRenderer();

        // Render a PDF from a URL
        PdfDocument pdf = renderer.RenderUrlAsPdf("https://ironpdf.com");

        // Print the PDF document, 300 dpi, without printer name
        pdf.Print(300, true);
    }
}
Imports IronPdf

Friend Class Program
	Shared Sub Main()
		' Create a new PDF renderer
		Dim renderer = New ChromePdfRenderer()

		' Render a PDF from a URL
		Dim pdf As PdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com")

		' Print the PDF document, 300 dpi, without printer name
		pdf.Print(300, True)
	End Sub
End Class
$vbLabelText   $csharpLabel

在這個程式碼範例中,IronPDF 可以有效率地從 URL 渲染 PDF,並將其傳送到預設印表機進行列印。 Print方法配置為 300 DPI 分辨率,允許靜默列印而無需提示使用者。 如需更多進階列印選項,請造訪C# 列印 PDF 文件

輸出

專案執行時,列印方法會顯示一個列印對話框,用於將文件儲存為 PDF 文件。 如果預設印表機設定為實體印表機,則文件將直接列印。

! PdfiumViewer 列印 PDF(C# 替代教學)圖 7 - 列印對話框

儲存的輸出結果是一個像素級完美的PDF文件:

! 使用 C# 中的 PdfiumViewer 列印 PDF(替代教程)圖 8 - PDF 輸出

使用 PDFiumViewer 列印 PDF

雖然 PDFiumViewer 在渲染和顯示 PDF 方面表現出色,但它不提供原生 PDF 列印功能。 要使用 PDFiumViewer 列印 PDF 文檔,您需要使用額外的第三方繪圖工具或函式庫。 要使用 PDFiumViewer 直接列印,我們需要將 Microsoft 的 System.Drawing.Printing 組件與 PDFiumViewer 庫一起使用。

在以下程式碼中,首先,我們使用PdfDocument.Load方法來載入 PDF。 然後,我們使用來自System.Drawing.Printing命名空間的CreatePrintDocument方法建立一個名為printDocument的列印物件。 最後,我們使用Print方法將載入的 PDF 檔案傳送到印表機進行列印。

using System;
using System.Drawing.Printing;
using PdfiumViewer;

class Program
{
    static void Main()
    {
        string pathToFile = @"C:\assets\input.pdf"; // Absolute path with filename

        // Load the PDF document
        using (var pdf = PdfDocument.Load(pathToFile))
        {
            // Create a print document for the loaded PDF
            var printDocument = pdf.CreatePrintDocument();

            // Print the document
            printDocument.Print();
        }
    }
}
using System;
using System.Drawing.Printing;
using PdfiumViewer;

class Program
{
    static void Main()
    {
        string pathToFile = @"C:\assets\input.pdf"; // Absolute path with filename

        // Load the PDF document
        using (var pdf = PdfDocument.Load(pathToFile))
        {
            // Create a print document for the loaded PDF
            var printDocument = pdf.CreatePrintDocument();

            // Print the document
            printDocument.Print();
        }
    }
}
Imports System
Imports System.Drawing.Printing
Imports PdfiumViewer

Friend Class Program
	Shared Sub Main()
		Dim pathToFile As String = "C:\assets\input.pdf" ' Absolute path with filename

		' Load the PDF document
		Using pdf = PdfDocument.Load(pathToFile)
			' Create a print document for the loaded PDF
			Dim printDocument = pdf.CreatePrintDocument()

			' Print the document
			printDocument.Print()
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

注意: PDFiumViewer 需要System.Windows.Forms組件才能運作; 否則,它將拋出異常。 這是因為 PDFiumViewer 函式庫是設計用於 Windows Forms 應用程式的。 請確保在有效的 Windows Forms 應用程式中執行此任務。

執行應用程式後,將顯示一個帶有"列印 PDF"按鈕的 Windows 窗體。 點擊後,將顯示列印對話框。 將文件另存為PDF文件。

! PdfiumViewer 在 C# 中列印 PDF(替代教程)圖 9 - 儲存為 PDF

輸出結果與輸入的PDF檔案相同。如果印表機設定中包含實際的印表機名稱,那麼它就能完美地列印在紙上。

! PdfiumViewer 用 C# 列印 PDF(替代教程)圖 10 - 輸出

結論

IronPDF 和 PDFiumViewer 在處理 PDF 檔案時各有不同的用途。 IronPDF 提供了一套全面的功能,用於建立、操作和列印 PDF 文件。 其易用性和豐富的功能使其成為 .NET 開發人員的熱門選擇。

另一方面,PDFiumViewer 在 Windows Forms 應用程式中渲染和顯示 PDF 方面表現出色。 但是,它缺乏原生的 PDF 列印功能,可能需要像上面的例子所示的其他列印解決方案。

IronPDF 和 PDFiumViewer 之間的選擇取決於您的特定專案需求。 如果您需要一個功能全面、具備強大PDF處理功能的函式庫,IronPDF是一個絕佳的選擇。另一方面,如果您主要關注的是在Windows窗體應用程式中顯示PDF,PDFiumViewer則可以滿足您的需求。

IronPDF 是一個功能強大的 PDF 庫,適用於 C# 開發人員。 它免費用於開發目的,商業許可證起價為單一開發人員的$799 。 此外,還提供包含所有功能和支援的免費試用版,因此您可以在購買前先試用。 您可以從這裡下載軟體。

請注意PDFiumViewer 為其各自所有者的註冊商標。 本網站與 PDFiumViewer 無任何關聯,亦未獲得其認可或贊助。 所有產品名稱、標誌和品牌均為其各自擁有者的財產。 比較資料僅供參考,並反映撰寫時的公開資訊。

常見問題解答

如何使用 C# 將 HTML 轉換為 PDF?

您可以使用 IronPDF 的 RenderHtmlAsPdf 方法將 HTML 字串轉換為 PDF。此外,RenderHtmlFileAsPdf 方法允許直接將 HTML 檔案轉換為 PDF 文件。

在 C# 應用程式中列印 PDF 的最佳方式是什麼?

IronPDF 提供內建功能,可從 C# 應用程式中靜默列印 PDF,讓您不需使用第三方工具即可直接列印文件。

如何在 Windows Forms 應用程式中顯示 PDF?

PDFiumViewer 是一個適用於在 Windows Forms 應用程式中渲染和顯示 PDF 的函式庫。它支援導覽、縮放和文字選擇,但缺乏原生列印功能。

在 Visual Studio 中使用 NuGet 安裝 PDF 函式庫的步驟為何?

若要安裝 IronPDF,請開啟 Visual Studio,導覽專案中的「管理 NuGet 套件」,搜尋「IronPDF」,然後按一下「安裝」。至於 PDFiumViewer,請依照相同步驟搜尋「PDFiumViewer」。

IronPDF 為 C# 語言的 PDF 處理提供哪些功能?

IronPDF 提供創建和編輯 PDF 的全面功能,包括 HTML 到 PDF 的轉換、添加數位簽名、水印、加密以及靜音列印 PDF 的功能。

PDFiumViewer 是否適合直接從 C# 應用程式列印 PDF?

PDFiumViewer 不支援直接列印 PDF。需要額外的函式庫或工具,例如 System.Drawing.Printing 來處理列印任務。

為什麼選擇 IronPDF 用 C# 來處理 PDF?

IronPDF 是需要強大的 PDF 建立、處理和列印解決方案的開發人員的理想選擇。它支援先進的功能,並提供詳盡的說明文件和友善的使用者整合。

在開發過程中,我可以免費使用 IronPdf 嗎?

是的,IronPDF 可供免費開發使用。對於商業應用程式,我們提供特定費用的授權。

IronPDF 和 PDFiumViewer 哪個函式庫更適合我的 C# 專案?

選擇取決於您的需求。IronPDF 更適合全面性的 PDF 建立與列印,而 PDFiumViewer 則是針對在 Windows Forms 應用程式中顯示 PDF 而最佳化。

Curtis Chau
技術作家

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

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