产品比较

PDFsharp 与 iTextSharp(C# PDF 库比较)

发布 2024年二月18日
分享:

介绍

在软件开发领域,生成和操作PDF文档的能力是一个常见的需求。 开发者通常依赖第三方库来简化流程并提高生产力。

Three popular choices for working with PDFs in the 三个流行的选择用于处理PDFs的工具包括 .NET 生态系统是 PDFSharp、 iTextSharpIronPDF.

在本文中,我们将深入探讨安装过程的细节,提供基本任务的代码示例,并最终评估这三个PDF库,以确定最适合您PDF相关需求的选择。

PDFsharp

PDFsharp 是一个用于在 .NET core 应用程序中创建和处理 PDF 文档的开源库。 由 Empira Software GmbH 开发,PDFsharp 轻巧直接,适用于商业用途。

它特别适合用于免费和学术项目的基本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#

该代码片段创建了一个 PDF 文件,单页包含数据和文本 "Hello, PDFsharp!".

PDFsharp 与 iTextSharp(C# PDF 库比较):图 1

iTextSharp

iTextSharp 是一个广泛使用且功能丰富的库,用于在 .NET 框架中处理 PDF 文件。 由iText软件开发,带有开源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 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字符串,在这种情况下,它包含一个简单的标题标签,写着“你好,IronPDF”。!".

生成的PDF文档使用SaveAs方法保存为名为“ironpdf_example.pdf”的文件。

总的来说,这段简洁的代码展示了IronPDF让开发者能够轻松将HTML内容转换为PDF的便利性,使其成为处理.NET应用程序中生成PDF任务的强大工具。

PDFsharp 与 iTextSharp(C# PDF 库比较):图 3

选择最佳库

虽然PDFsharp和iTextSharp各有其用途,但出于多种原因,IronPDF成为了最佳选择:

  1. 易用性:IronPDF 提供了一个简洁直观的编程 API,使得各个水平的开发者都能轻松使用。
  2. 全面的功能: IronPDF提供了广泛的功能集,涵盖了包括HTML到PDF转换、PDF合并等在内的各种PDF操作任务。
  3. 性能: IronPDF 在性能方面表现出色,即使在资源密集型操作中也能确保效率。
  4. 文档: IronPDF 提供的广泛且组织良好的文档简化了学习过程,并帮助开发者高效地利用其功能。
  5. 许可证: 虽然 IronPDF 需要商业许可,但它所提供的优势以及响应迅速的支持,为专业项目的投资提供了正当理由。

结论

IronPDF depends on your specific project requirements. If you need a library strictly for PDF creation, PDFsharp might be the suitable choice. However, if your needs include complex PDF manipulations like merging, splitting, and editing, iTextSharp is a powerful option. For those looking to integrate PDF functionalities directly into .NET applications with extended support for HTML to PDF conversions, IronPDF offers a comprehensive solution. IronPDF 取决于您项目的具体要求。

PDFsharp 适用于简单任务,iTextSharp 非常适合需要开源的复杂 PDF 操作,而 IronPDF 以其易用性、全面的功能和卓越的性能脱颖而出,是商业应用的理想选择。

考虑您项目的具体需求以及每个库提供的功能,以做出明智的决定。

IronPDF提供了强大的支持和组织良好的文档,简化了开发人员的学习过程,并使其功能的高效利用成为可能。

IronPDF提供最佳的HTML到PDF生成功能,要了解更多关于IronPDF的信息,请访问 这里有关 HTML 转 PDF 的代码示例,请访问以下网站 链接.

IronPDF提供商业许可证用于生产使用,但它也提供了 试用许可 供新用户测试 IronPDF 功能。

< 前一页
iTextSharp读取PDF替代方案(开发人员教程)
下一步 >
在 C# 中使用 iTextSharp 和 IronPDF 分割 PDF 的比较

准备开始了吗? 版本: 2024.9 刚刚发布

免费NuGet下载 总下载量: 10,731,156 查看许可证 >