在生产环境中测试,无水印。
随时随地满足您的需求。
获得30天的全功能产品。
几分钟内就能启动并运行。
在您的产品试用期间,全面访问我们的支持工程团队。
在软件开发领域,生成和操作PDF文档的能力是一个常见的需求。 开发者通常依赖第三方库来简化流程并提高生产力。
Three popular choices for working with PDFs in the 三个流行的选择用于处理PDFs的工具包括.NET生态系统是 PDFSharp、iTextSharp和IronPDF.
在本文中,我们将深入探讨安装过程的细节,提供基本任务的代码示例,并最终评估这三个PDF库,以确定最适合您PDF相关需求的选择。
PDFsharp 是一个用于在 .NET core 应用程序中创建和处理 PDF 文档的开源库。 由 Empira Software GmbH 开发,PDFsharp 轻巧直接,适用于商业用途。
它特别适合用于免费和学术项目的基本PDF生成任务,通常在简便性是优先考虑的情况下使用。
安装 PDFsharp 是一个相对简单的过程。 该库可作为NuGet包提供,使其集成到您的项目中非常简便。
要通过 NuGet 安装 PDFsharp,请在 Visual Studio 中打开 NuGet 包管理器控制台并执行以下命令:
Install-Package 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();
}
}
该代码片段创建了一个 PDF 文件,单页包含数据和文本 "Hello, PDFsharp!".
iTextSharp 是一个广泛使用且功能丰富的库,用于在 .NET 框架中处理 PDF 文件。 该库由 iText 软件公司使用开源 Java 代码库开发,提供了一套全面的工具和库,用于创建、操作 PDF 文件和文档并从中提取内容。
iTextSharp以其灵活性和广泛的文档而闻名。
要安装iTextSharp,您可以使用以下命令通过NuGet包管理器控制台进行安装:
Install-Package itext7
该命令将在项目中安装最新版本的 iTextSharp 7。
让我们使用 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!"));
}
}
}
}
该代码片段实现了与 PDFsharp 示例相同的结果,创建了一个文本为 "Hello, iTextSharp "的 PDF 文档。!".
IronPDFIronPDF是一个功能强大的.NET库,用于创建、编辑和操作PDF文档。 由Iron Software开发,以其易用性、全面的功能和卓越的性能而著称。
虽然它是一个商业库,但它提供的优势使其在PDF文件操作领域成为一个强有力的竞争者。
要安装IronPDF,您需要从Iron Software网站获取许可证。一旦拥有了许可证,您就可以下载IronPDF包并将其集成到您的项目中。
对于NuGet安装,您可以使用以下命令:
Install-Package IronPdf
现在,让我们通过一个代码示例来探讨如何使用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");
此代码片段使用IronPDF库将HTML内容转换为PDF。 它首先导入 IronPDF 命名空间并创建一个 HtmlToPdf
类的实例。
HtmlToPdf
对象随后被用来渲染一个HTML字符串,在这种情况下,它包含一个简单的标题标签,写着“你好,IronPDF”。!".
生成的PDF文档使用SaveAs方法保存为名为“ironpdf_example.pdf”的文件。
总之,这段简洁的代码展示了 IronPDF 可让开发人员轻松地将 HTML 内容转换为 PDF,使其成为在 .NET 应用程序中处理 PDF 生成任务的强大工具。
虽然PDFsharp和iTextSharp各有其用途,但出于多种原因,IronPDF成为了最佳选择:
易用性:IronPDF 提供了一个简洁直观的编程 API,使得各个水平的开发者都能轻松使用。
全面的功能: IronPDF提供了广泛的功能集,涵盖了包括HTML到PDF转换、PDF合并等在内的各种PDF操作任务。
性能: IronPDF 在性能方面表现出色,即使在资源密集型操作中也能确保效率。
Documentation: 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 的更多信息,请访问IronPDF文档. 有关 HTML 转 PDF 的代码示例,请访问HTML 转 PDF 示例.
IronPDF 提供用于生产的商业许可证,但同时也提供了试用许可供新用户测试 IronPDF 功能。