產品比較

PDFsharp 與 iTextSharp (C# PDF 函式庫比較)

發佈 2024年2月18日
分享:

介紹

在軟體開發領域,生成和操作 PDF 文件的能力是一項常見要求。開發人員通常依賴第三方庫來簡化過程並提高生產力。

在 PDF 相關工作中,三個受歡迎的選擇是 .NET 生態系統是 PDFSharp, iTextSharp,和 IronPDF在這篇文章中,我們將深入探討安裝過程,提供基本任務的程式碼範例,並最終評估這三個 PDF 函式庫,以確定最適合您 PDF 相關需求的選擇。

PDFsharp

PDFsharp 是一個開源庫,用於在 .NET Core 應用程式中創建和處理 PDF 文件。PDFsharp 由 Empira Software GmbH 開發,輕量化且適合商業用途。

它特別適合用於基本的 PDF 生成任務,適合免費和學術專案,並且經常用於簡單性優先的情況。

安裝 PDFsharp

安裝 PDFsharp 是一個相對簡單的過程。該庫可作為 NuGet 套件使用,使其很容易集成到您的專案中。

要通過 NuGet 安裝 PDFsharp,請在 Visual Studio 中打開 NuGet 套件管理器主控台並執行以下命令:

Install-Package PdfSharp

此命令將下載並安裝最新版本的PDFsharp到您的專案中。

PDFsharp 代碼範例

讓我們來看一個使用 PDFsharp 創建基本 PDF 的簡單代碼範例:

using PdfSharp.Pdf;
class Program
{
    static void Main()
    {
        // Create a new PDF document
        PdfDocument document = new PdfDocument();
        // Add a page to the document
        PdfPage page = document.AddPage();
        // Obtain a graphics object to draw on the page
        XGraphics gfx = XGraphics.FromPdfPage(page);
        // Draw "Hello, PDFsharp!" on the page
        gfx.DrawString("Hello, PDFsharp!", new XFont("Arial", 12), XBrushes.Black,
            new XRect(10, 10, page.Width, page.Height), XStringFormats.TopLeft);
        // Save the document to a file
        document.Save("output.pdf");
        // Close the document
        document.Close();
    }
}
using PdfSharp.Pdf;
class Program
{
    static void Main()
    {
        // Create a new PDF document
        PdfDocument document = new PdfDocument();
        // Add a page to the document
        PdfPage page = document.AddPage();
        // Obtain a graphics object to draw on the page
        XGraphics gfx = XGraphics.FromPdfPage(page);
        // Draw "Hello, PDFsharp!" on the page
        gfx.DrawString("Hello, PDFsharp!", new XFont("Arial", 12), XBrushes.Black,
            new XRect(10, 10, page.Width, page.Height), XStringFormats.TopLeft);
        // Save the document to a file
        document.Save("output.pdf");
        // Close the document
        document.Close();
    }
}
Imports PdfSharp.Pdf
Friend Class Program
	Shared Sub Main()
		' Create a new PDF document
		Dim document As New PdfDocument()
		' Add a page to the document
		Dim page As PdfPage = document.AddPage()
		' Obtain a graphics object to draw on the page
		Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
		' Draw "Hello, PDFsharp!" on the page
		gfx.DrawString("Hello, PDFsharp!", New XFont("Arial", 12), XBrushes.Black, New XRect(10, 10, page.Width, page.Height), XStringFormats.TopLeft)
		' Save the document to a file
		document.Save("output.pdf")
		' Close the document
		document.Close()
	End Sub
End Class
VB   C#

這段代碼片段創建了一個包含數據和文本 "Hello, PDFsharp" 的單頁 PDF。!".

PDFsharp 對比 iTextSharp(C# PDF 程式庫比較):圖 1

iTextSharp

iTextSharp 是一個廣泛使用且功能豐富的庫,用於在 .NET 框架中處理 PDF。 由 iText Software 開發,並具有開源 Java 原始碼,這個庫提供了創建、操作和從 PDF 文件中提取內容的全面工具和庫集。

iTextSharp 以其靈活性和豐富的文檔而聞名。

安裝 iTextSharp

要安裝 iTextSharp,您可以使用 NuGet 套件管理器主控台,並輸入以下命令:

Install-Package itext7

此指令將把最新版本的iTextSharp 7安裝到您的專案中。

iTextSharp 代碼示例

讓我們探索一個使用 iTextSharp 創建簡單 PDF 文件的基本代碼示例:

using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
class Program
{
    static void Main()
    {
        // Create a new PDF document
        using (var writer = new PdfWriter("output.pdf"))
        {
            using (var pdf = new PdfDocument(writer))
            {
                // Create a document
                var document = new Document(pdf);
                // Add a paragraph with the text "Hello, iTextSharp!"
                document.Add(new Paragraph("Hello, iTextSharp!"));
            }
        }
    }
}
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
class Program
{
    static void Main()
    {
        // Create a new PDF document
        using (var writer = new PdfWriter("output.pdf"))
        {
            using (var pdf = new PdfDocument(writer))
            {
                // Create a document
                var document = new Document(pdf);
                // Add a paragraph with the text "Hello, iTextSharp!"
                document.Add(new Paragraph("Hello, iTextSharp!"));
            }
        }
    }
}
Imports System
Imports System.IO
Imports iText.Kernel.Pdf
Imports iText.Layout
Imports iText.Layout.Element
Friend Class Program
	Shared Sub Main()
		' Create a new PDF document
		Using writer = New PdfWriter("output.pdf")
			Using pdf = New PdfDocument(writer)
				' Create a document
				Dim document As New Document(pdf)
				' Add a paragraph with the text "Hello, iTextSharp!"
				document.Add(New Paragraph("Hello, iTextSharp!"))
			End Using
		End Using
	End Sub
End Class
VB   C#

此代碼片段達到與PDFsharp範例相同的效果,創建一個帶有文本「Hello, iTextSharp」的PDF文檔。!".

PDFsharp與iTextSharp(C# PDF庫比較):圖2

IronPDF:領先的競爭者

IronPDF 是一個強大的 .NET 庫,用於創建、編輯和操作 PDF 文件。由 Iron Software 開發,它以使用簡便、功能全面和卓越的性能而著稱。

儘管它是一個商業庫,但它所帶來的優勢使其在 PDF 文件操作領域成為強有力的競爭者。

安裝 IronPDF

要安裝 IronPDF,您需要從 Iron Software 網站獲取許可證。一旦擁有許可證,您便可以下載 IronPDF 套件並將其整合到您的專案中。

對於 NuGet 安裝,可以使用以下命令:

Install-Package IronPdf

使用 IronPDF 創建 PDF 文件

現在,讓我們通過一個代碼範例來探索如何使用 IronPDF 來創建和編輯 PDF:

using IronPdf;
var htmlToPdf = new HtmlToPdf();
var pdf = htmlToPdf.RenderHtmlAsPdf("<html><body><h1>Hello, IronPDF!</h1></body></html>");
pdf.SaveAs("ironpdf_example.pdf");
using IronPdf;
var htmlToPdf = new HtmlToPdf();
var pdf = htmlToPdf.RenderHtmlAsPdf("<html><body><h1>Hello, IronPDF!</h1></body></html>");
pdf.SaveAs("ironpdf_example.pdf");
Imports IronPdf
Private htmlToPdf = New HtmlToPdf()
Private pdf = htmlToPdf.RenderHtmlAsPdf("<html><body><h1>Hello, IronPDF!</h1></body></html>")
pdf.SaveAs("ironpdf_example.pdf")
VB   C#

這段程式碼片段使用了 IronPDF 函式庫將 HTML 內容轉換為 PDF。它首先通過引入 IronPDF 命名空間並創建 HtmlToPdf 類的一個例項。

然後使用 HtmlToPdf 物件來轉換一個 HTML 字串,在這個範例中,這個字串包含了一個簡單的標題標籤,內容為 "Hello, IronPDF"!生成的 PDF 文件將使用 SaveAs 方法保存到名為 "ironpdf_example.pdf" 的文件中。

總的來說,這段簡潔的代碼展示了 IronPDF 如何輕鬆地讓開發人員將 HTML 內容轉換為 PDF,使其成為在 .NET 應用程式中處理生成 PDF 任務的強大工具。

PDFsharp與iTextSharp(C# PDF庫比較):圖3

選擇最佳的程式庫

雖然 PDFsharp 和 iTextSharp 也各有其優點,但 IronPDF 因為以下幾個原因成為最佳選擇:

  1. 易於使用: IronPDF 提供了一個簡潔且直觀的程式介面,使得各種技能水平的開發者都能輕鬆使用。

  2. 全面的功能: IronPDF 提供了廣泛的功能集,涵蓋了各種 PDF 操作任務,包括 HTML 到 PDF 的轉換、PDF 合併等。

  3. 效能: IronPDF 在效能方面表現卓越,即使在資源密集的操作中也能保證效率。

  4. 文件: 廣泛且組織良好的 IronPDF 文件簡化了學習過程,並幫助開發者有效利用其功能。

  5. 授權: 雖然 IronPDF 需要商業授權,但其提供的優勢,加上回應迅速的支援,為專業專案的投資提供了正當理由。

結論

綜上所述,在 PDFsharp、iTextSharp 和 IronPDF 取決於您專案的具體需求。

PDFsharp 適合處理簡單任務,iTextSharp 是在具有開源考量的複雜 PDF 操作中的理想選擇,而 IronPDF 以其易於使用、全面的功能和出色的性能脫穎而出,使其成為商業應用的明智投資。

考慮您專案的具體需求和每個庫所提供的功能,做出明智的決定。

IronPDF 提供強大支持和井然有序的文件,簡化了開發人員的學習過程,並提升了其功能的高效利用。

IronPDF 提供最佳的 HTML 到 PDF 生成功能,欲知更多資訊請訪問 IronPDF 這裡如需 HTML 轉 PDF 的代碼範例,請訪問以下網站 連結.

IronPDF 提供生產使用的商業許可證,但它也提供 試用授權 供新用戶測試IronPDF功能。

< 上一頁
iTextSharp 讀取 PDF 替代方案(開發人員教程)
下一個 >
比較在C#中使用iTextSharp和IronPDF進行PDF拆分

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

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