產品比較

PDFsharp查看PDF替代方案使用IronPDF

發佈 2023年12月24日
分享:

在軟體開發的動態環境中,處理和展示各種格式的數據至關重要。其中,可攜式文件格式 (PDF) 在文件共享方面,PDF 脫穎而出,成為廣泛使用和標準化的格式。在 C# 編程語言的領域中,無縫查看 PDF 的功能是不容忽視的。

C# 的多功能性使其在不同領域中專門用於開發強大的應用程式變得十分流行。PDF 作為一種格式,確保了文件的完整性和跨平台的一致呈現。將 PDF 查看功能集成到 C# 應用程式中,讓開發人員能夠提升用戶體驗、精簡工作流程、保存和提供高效的解決方案來處理各行各業的文件。

本文探討了使用 C# 查看 PDF 的重要性,介紹了兩個強大的庫 - PDFsharpIronPDF 並提供逐步安裝和使用說明,以便查看 PDF。

1. PDFsharp

PDFsharp 在 C# 程式設計領域中,PDFsharp 作為一個強大的開源庫脫穎而出,為開發人員提供了一個多功能的 PDF 處理工具包。不僅能夠創建和修改 PDF 文件,PDFsharp 還以其在 C# 應用程序中無縫整合 PDF 檢視功能而著稱。這個以輕量設計和易於使用而聞名的庫,使開發人員能夠輕鬆瀏覽和操作 PDF 文件。在我們探索 PDFsharp 的功能和深入實踐應用時,不難發現這個庫是尋求高效解決方案的開發人員在其 C# 項目中增強文件管理的一個寶貴資產。

2. IronPDF

IronPDF 是一個強大且功能豐富的庫,使開發人員能夠以無與倫比的輕鬆度導航在複雜的PDF操作領域。IronPDF在設計上考慮了簡單性和多樣性,使用戶能夠輕鬆創建、編輯和 閱讀PDF文件 在他們的C#應用程序中。除了基本功能之外,IronPDF還以先進功能而聞名,如HTML轉換為PDF、支援各種圖像格式、以及流暢處理複雜的PDF操作。

當我們深入研究IronPDF的功能時,很明顯這個庫不僅僅是用於基本PDF任務的一個工具,而是尋求通過先進的PDF功能來提升其C#項目的開發人員的一個全面解決方案。IronPDF能夠處理PDF並正確地將數據字串格式化為可讀的字串。

3. 安裝 IronPDF

在使用 IronPDF 進行 PDF 查看之前,首先必須安裝這個庫。您可以很輕鬆地添加 IronPDF 使用 NuGet 套件管理器或套件管理器主控台將其添加到您的專案中。只需運行以下命令:

Install-Package IronPdf

此命令會安裝 IronPDF 套件及其依賴項,讓您可以開始將其功能整合到您的 C# 應用程式中。

4. 安裝PDFsharp

與IronPDF相似, PDFsharp 可以使用 NuGet 套件管理器或套件管理器控制台來安裝。執行以下命令以安裝 PDFsharp:

Install-Package PdfSharp

此命令安裝PDFsharp程式庫,使其可以在您的C#專案中使用。

5. 使用 PDFsharp 查看 PDF 頁面內容

在本節中,我們將討論如何使用 PDFsharp 檢視和打開 PDF 文件,並將提取的結果打印到控制台。在下面的代碼示例中,將使用 PDFsharp 檢視 PDF 文件內容。

using System;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
class Program
{
    static void Main()
    {
        // Specify the pdf path
        string pdfFilePath = "output.pdf";
        // Open the PDF document
        PdfDocument document = PdfReader.Open(pdfFilePath, PdfDocumentOpenMode.Import);
        // Iterate through each page of the document
        for (int pageIndex = 0; pageIndex < document.PageCount; pageIndex++)
        {
            // Get the current page, Extract text from the page
            string page = document.Pages [pageIndex].Contents.Elements.GetDictionary(0).Stream.ToString();
            // Print the text to the console
            Console.WriteLine($"Page {pageIndex + 1} Content:\n{page}\n");
        }
        Console.ReadLine(); // Wait for user input before closing the console
    }
}
using System;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
class Program
{
    static void Main()
    {
        // Specify the pdf path
        string pdfFilePath = "output.pdf";
        // Open the PDF document
        PdfDocument document = PdfReader.Open(pdfFilePath, PdfDocumentOpenMode.Import);
        // Iterate through each page of the document
        for (int pageIndex = 0; pageIndex < document.PageCount; pageIndex++)
        {
            // Get the current page, Extract text from the page
            string page = document.Pages [pageIndex].Contents.Elements.GetDictionary(0).Stream.ToString();
            // Print the text to the console
            Console.WriteLine($"Page {pageIndex + 1} Content:\n{page}\n");
        }
        Console.ReadLine(); // Wait for user input before closing the console
    }
}
Imports Microsoft.VisualBasic
Imports System
Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO
Friend Class Program
	Shared Sub Main()
		' Specify the pdf path
		Dim pdfFilePath As String = "output.pdf"
		' Open the PDF document
		Dim document As PdfDocument = PdfReader.Open(pdfFilePath, PdfDocumentOpenMode.Import)
		' Iterate through each page of the document
		For pageIndex As Integer = 0 To document.PageCount - 1
			' Get the current page, Extract text from the page
			Dim page As String = document.Pages (pageIndex).Contents.Elements.GetDictionary(0).Stream.ToString()
			' Print the text to the console
			Console.WriteLine($"Page {pageIndex + 1} Content:" & vbLf & "{page}" & vbLf)
		Next pageIndex
		Console.ReadLine() ' Wait for user input before closing the console
	End Sub
End Class
VB   C#

以下 C# 程式碼正確地使用 PDFsharp 庫來讀取和提取 PDF 文件中的文字內容。程式首先指定一個 PDF 檔案的路徑,假設該檔案名為 "output.PDF"。然後將 PDF 文件以導入模式打開,允許提取內容。代碼接下來迭代文件的每個 PDF 頁面,提取每頁的實際 PDF 內容,並在控制台上打印出來。

提取的文字是通過訪問頁面內容並將其轉換為字符串來獲取的。輸出包括頁碼及其相應的內容。最後,程式等待用戶輸入後再關閉控制台。請注意,代碼假設示例 PDF 結構簡單,對於更複雜的場景,可能需要額外的解析和處理。

PDFsharp 使用 IronPDF 查看 PDF 替代方案:圖 1 - 控制台輸出:Hello World - 使用 PDFsharp 庫從 output.PDF 文件中提取的內容。

6. IronPDF 檢視 PDF 檔案

使用 IronPDF 檢視 PDF 比 PDFsharp 更加簡單,只需幾行代碼即可完成。

using IronPdf;
using IronSoftware.Drawing;
using System;
var pdf = PdfDocument.FromFile("output.pdf");
string text = pdf.ExtractAllText();
Console.WriteLine(text);
using IronPdf;
using IronSoftware.Drawing;
using System;
var pdf = PdfDocument.FromFile("output.pdf");
string text = pdf.ExtractAllText();
Console.WriteLine(text);
Imports IronPdf
Imports IronSoftware.Drawing
Imports System
Private pdf = PdfDocument.FromFile("output.pdf")
Private text As String = pdf.ExtractAllText()
Console.WriteLine(text)
VB   C#

這段 C# 代碼使用 IronPDF 庫從名為 "output.PDF" 的 PDF 檔案中提取文字內容。首先,它會導入必要的命名空間,然後使用 PdfDocument.FromFile 來載入 PDF 文件。()從IronPDF的方法。隨後,它使用ExtractAllText方法從PDF文件中提取所有文本內容,並將其存儲在名為 "text" 的字符串變量中。最後,提取的文本使用Console.WriteLine打印到控制台。()方法。這段代碼簡化了從 PDF 中提取文本的過程,使其簡潔明了,這要歸功於 IronPDF 庫提供的功能。

PDFsharp 檢視 PDF 替代品 使用 IronPDF:圖 2 - 主控台輸出:Hello World - 使用 IronPDF 程式庫從 output.PDF 文件中擷取的內容。

7. 結論

PDFsharp 和 IronPDF 都為尋求多功能解決方案的開發者提供了引人注目的功能。 PDFsharp,這是一個開源庫,提供了一個輕量級且用戶友好的工具包,使其成為進行基本 PDF 任務和集成到 C# 項目的絕佳選擇。它的功能在於有效地導航和操作 PDF 文件。另一方面, IronPDF emerges as a robust, feature-rich library designed for comprehensive PDF operations. Its advanced functionalities, such as HTML to PDF conversion and support for various other image file formats, distinguish it as a powerful tool for developers aiming to elevate their C# projects with sophisticated PDF capabilities.

作為一個強大且功能豐富的庫,專為綜合PDF操作而設計。其高級功能,如HTML到PDF的轉換和對各種其他圖像文件格式的支持,使其成為開發人員提升其C#項目PDF功能的強大工具。

While both libraries have their merits, IronPDF stands out as the winner for its extensive feature set, simplicity, and versatility. The concise code example for viewing PDF files using IronPDF demonstrates its ease of use and effectiveness in extracting text content. The library's comprehensive capabilities make it a valuable asset for developers tackling complex PDF tasks, making IronPDF a recommended choice for those looking to integrate advanced PDF functionalities seamlessly into their C# applications.

雖然兩個庫都有其優點,IronPDF因其廣泛的特性集、簡單性和多功能性而脫穎而出。使用IronPDF查看PDF文件的簡潔代碼示例展示了其易用性和在提取文本內容方面的有效性。該庫的綜合能力使其成為開發人員應對複雜PDF任務的寶貴資產,成為那些希望將高級PDF功能無縫集成到其C#應用程序中的開發人員的推薦選擇。

IronPDF is free for development use and comes with a

IronPDF 對於開發使用是免費的,並且隨附一個 免費試用如需了解有關使用 IronPDF 查看 PDF 內容的更多信息,請訪問 這裡如需查看更多程式碼範例,請訪問 頁面。

< 上一頁
IronPDF與PdfPig的比較
下一個 >
IronPDF與DinktoPdf之間的比較

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 10,993,239 查看許可證 >