产品比较 iTextSharp与IronPDF的PDF编辑比较 Curtis Chau 已更新:2026年1月18日 下载 IronPDF NuGet 下载 DLL 下载 Windows 安装程序 免费试用 LLM副本 LLM副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在 Grok 中打开 向 Grok 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 Full Comparison Looking for a detailed feature-by-feature breakdown? See how IronPDF stacks up against Itext on pricing, HTML support, and licensing. View Full Comparison PDF(可携带文档格式)是一种广泛使用的文档格式,因为它能够保留文档格式、安全性和可移植性而受到欢迎。 PDF文件已成为世界上最广泛使用的文档格式之一,有多种库可用于在C#语言中创建和操作PDF。 了解如何使用C#结合IronPDF和iTextSharp编辑PDF文件,通过利用这些强大的库使任务变得简单。 在本文中,我们将比较两个用于PDF操作的C#流行库:iTextSharp和IronPDF。 我们将讨论如何使用这两个库编辑PDF文件,然后我们将探讨IronPDF如何在输出打印、性能和定价方面优于iTextSharp。 iTextSharp DLL和IronPDF库简介 可以通过iTextSharp和IronPDF功能和试用信息帮助开发人员有效处理C#中的PDF文件。 这两个库提供了一系列广泛的功能来创建、编辑和操作PDF文档。 iTextSharp DLL是基于Java的iText库的C#端口。 它提供了一个简单易用的API,用于创建和操作PDF文档。 iTextSharp是一个开源库,按照AGPL许可证提供。 IronPDF是一个.NET库,专为使用C#创建、编辑和操作PDF文件而设计。 它提供了一个现代且直观的API,用于处理PDF文档。 IronPDF是一个商业库,提供免费试用版本和订阅选项以用于更广泛的使用。 比较iTextSharp和IronPDF库 iTextSharp和IronPDF库都提供了广泛的功能和功能来创建、编辑和操作PDF文档。 然而,IronPDF在多个方面都优于iTextSharp,使其成为C#中处理PDF文档的首选。 使用iTextSharp和IronPDF编辑PDF文件 现在我们已经讨论了iTextSharp和IronPDF之间的差异,让我们来看看如何使用这两个库编辑PDF文件。 我们将探索如何使用iTextSharp和IronPDF在现有PDF文档中添加文本、表单字段以及填写表单的示例。 使用iTextSharp编辑PDF文件 先决条件 开始之前,您将需要以下内容: 在您的计算机上安装Visual Studio。 拥有C#编程语言的基本知识。 在您的项目中安装iTextSharp库。 要在您的项目中安装iTextSharp库,可以使用NuGet包管理器。 打开您的Visual Studio项目,然后在解决方案资源管理器中右键单击项目名称。 从上下文菜单中选择"管理NuGet包"。 在NuGet包管理器中,搜索"iTextSharp"并安装最新版本的包。 创建一个新的PDF文件 要使用 iTextSharp 创建新的 PDF 文件,我们需要创建一个"Document"类的新实例,并将一个新的 FileStream 对象传递给其构造函数。 下面是一个例子: using iText.Kernel.Pdf; using iText.Layout; using iText.Layout.Element; using iText.Layout.Properties; using System.IO; // Create a new PDF document using (var writer = new PdfWriter(new FileStream("newfile.pdf", FileMode.Create))) { using (var pdf = new PdfDocument(writer)) { var document = new Document(pdf); // Create a header paragraph Paragraph header = new Paragraph("HEADER") .SetTextAlignment(TextAlignment.CENTER) .SetFontSize(16); // Add the header to the document document.Add(header); // Loop through pages and align header text for (int i = 1; i <= pdf.GetNumberOfPages(); i++) { Rectangle pageSize = pdf.GetPage(i).GetPageSize(); float x = pageSize.GetWidth() / 2; float y = pageSize.GetTop() - 20; // Add the header text to each page document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0); } // Set the margins document.SetTopMargin(50); document.SetBottomMargin(50); } } using iText.Kernel.Pdf; using iText.Layout; using iText.Layout.Element; using iText.Layout.Properties; using System.IO; // Create a new PDF document using (var writer = new PdfWriter(new FileStream("newfile.pdf", FileMode.Create))) { using (var pdf = new PdfDocument(writer)) { var document = new Document(pdf); // Create a header paragraph Paragraph header = new Paragraph("HEADER") .SetTextAlignment(TextAlignment.CENTER) .SetFontSize(16); // Add the header to the document document.Add(header); // Loop through pages and align header text for (int i = 1; i <= pdf.GetNumberOfPages(); i++) { Rectangle pageSize = pdf.GetPage(i).GetPageSize(); float x = pageSize.GetWidth() / 2; float y = pageSize.GetTop() - 20; // Add the header text to each page document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0); } // Set the margins document.SetTopMargin(50); document.SetBottomMargin(50); } } $vbLabelText $csharpLabel 在上面的代码中,我们创建了一个名为"newfile.pdf"的新PDF文件并添加了一个段落标题。 编辑现有PDF文件 要使用 iTextSharp 编辑现有的 PDF 文件,您需要一个 PdfReader 对象来读取现有的 PDF 文档,还需要一个 PdfStamper 对象来修改它。 下面是一个例子: using iText.Kernel.Pdf; using iText.Layout; using iText.Layout.Element; using iText.Layout.Properties; using iText.Html2pdf; using System.IO; /** * iText URL to PDF * anchor-itext-url-to-pdf **/ private void ExistingWebURL() { // Initialize PDF writer PdfWriter writer = new PdfWriter("wikipedia.pdf"); // Initialize PDF document using PdfDocument pdf = new PdfDocument(writer); ConverterProperties properties = new ConverterProperties(); properties.SetBaseUri("https://en.wikipedia.org/wiki/Portable_Document_Format"); // Convert HTML to PDF Document document = HtmlConverter.ConvertToDocument( new FileStream("Test_iText7_1.pdf", FileMode.Open), pdf, properties); // Create and add a header paragraph Paragraph header = new Paragraph("HEADER") .SetTextAlignment(TextAlignment.CENTER) .SetFontSize(16); document.Add(header); // Align header text for each page for (int i = 1; i <= pdf.GetNumberOfPages(); i++) { Rectangle pageSize = pdf.GetPage(i).GetPageSize(); float x = pageSize.GetWidth() / 2; float y = pageSize.GetTop() - 20; // Add header text aligned at the top document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0); } // Set the top and bottom margins document.SetTopMargin(50); document.SetBottomMargin(50); document.Close(); } using iText.Kernel.Pdf; using iText.Layout; using iText.Layout.Element; using iText.Layout.Properties; using iText.Html2pdf; using System.IO; /** * iText URL to PDF * anchor-itext-url-to-pdf **/ private void ExistingWebURL() { // Initialize PDF writer PdfWriter writer = new PdfWriter("wikipedia.pdf"); // Initialize PDF document using PdfDocument pdf = new PdfDocument(writer); ConverterProperties properties = new ConverterProperties(); properties.SetBaseUri("https://en.wikipedia.org/wiki/Portable_Document_Format"); // Convert HTML to PDF Document document = HtmlConverter.ConvertToDocument( new FileStream("Test_iText7_1.pdf", FileMode.Open), pdf, properties); // Create and add a header paragraph Paragraph header = new Paragraph("HEADER") .SetTextAlignment(TextAlignment.CENTER) .SetFontSize(16); document.Add(header); // Align header text for each page for (int i = 1; i <= pdf.GetNumberOfPages(); i++) { Rectangle pageSize = pdf.GetPage(i).GetPageSize(); float x = pageSize.GetWidth() / 2; float y = pageSize.GetTop() - 20; // Add header text aligned at the top document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0); } // Set the top and bottom margins document.SetTopMargin(50); document.SetBottomMargin(50); document.Close(); } $vbLabelText $csharpLabel 在此代码中,打开一个现有的PDF,并为其页面添加标题,同时确保文字对齐。 使用IronPDF编辑PDF文档 IronPDF是一个用于C#的强大PDF库,为编辑PDF文档提供便利。 本教程将指导如何使用IronPDF编辑现有的PDF文件,包括创建新的PDF文档、添加页面、合并PDF等。 先决条件 确保您有: Visual Studio IDE IronPDF库 步骤1:创建新项目 在 Visual Studio 中创建一个新的 C# 项目。 选择"控制台应用程序"项目类型。 步骤2:安装IronPDF 使用NuGet包管理器将IronPDF库安装到您的项目中。 // Execute this command in the Package Manager Console Install-Package IronPdf // Execute this command in the Package Manager Console Install-Package IronPdf SHELL 步骤3:加载一个现有PDF文档 使用 PdfDocument 类加载现有 PDF 文档: using IronPdf; // Path to an existing PDF file var existingPdf = @"C:\path\to\existing\pdf\document.pdf"; // Load the PDF document var pdfDoc = PdfDocument.FromFile(existingPdf); using IronPdf; // Path to an existing PDF file var existingPdf = @"C:\path\to\existing\pdf\document.pdf"; // Load the PDF document var pdfDoc = PdfDocument.FromFile(existingPdf); $vbLabelText $csharpLabel 步骤4:向现有PDF文档添加新页面 添加新页面: // Add a new page with default size var newPage = pdfDoc.AddPage(); newPage.Size = PageSize.Letter; // Add a new page with default size var newPage = pdfDoc.AddPage(); newPage.Size = PageSize.Letter; $vbLabelText $csharpLabel 步骤5:从网站创建PDF 直接从网页URL生成PDF。 下面是一个例子: using IronPdf; /** * IronPDF URL to PDF * anchor-ironpdf-website-to-pdf **/ private void ExistingWebURL() { // Create PDF from a webpage var Renderer = new IronPdf.ChromePdfRenderer(); // Set rendering options Renderer.RenderingOptions.MarginTop = 50; // millimeters Renderer.RenderingOptions.MarginBottom = 50; Renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print; Renderer.RenderingOptions.TextHeader = new TextHeaderFooter() { CenterText = "{pdf-title}", DrawDividerLine = true, FontSize = 16 }; Renderer.RenderingOptions.TextFooter = new TextHeaderFooter() { LeftText = "{date} {time}", RightText = "Page {page} of {total-pages}", DrawDividerLine = true, FontSize = 14 }; Renderer.RenderingOptions.EnableJavaScript = true; Renderer.RenderingOptions.RenderDelay = 500; // milliseconds // Render URL as PDF using var PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format"); PDF.SaveAs("wikipedia.pdf"); } using IronPdf; /** * IronPDF URL to PDF * anchor-ironpdf-website-to-pdf **/ private void ExistingWebURL() { // Create PDF from a webpage var Renderer = new IronPdf.ChromePdfRenderer(); // Set rendering options Renderer.RenderingOptions.MarginTop = 50; // millimeters Renderer.RenderingOptions.MarginBottom = 50; Renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print; Renderer.RenderingOptions.TextHeader = new TextHeaderFooter() { CenterText = "{pdf-title}", DrawDividerLine = true, FontSize = 16 }; Renderer.RenderingOptions.TextFooter = new TextHeaderFooter() { LeftText = "{date} {time}", RightText = "Page {page} of {total-pages}", DrawDividerLine = true, FontSize = 14 }; Renderer.RenderingOptions.EnableJavaScript = true; Renderer.RenderingOptions.RenderDelay = 500; // milliseconds // Render URL as PDF using var PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format"); PDF.SaveAs("wikipedia.pdf"); } $vbLabelText $csharpLabel iTextSharp和IronPDF之间的差异 iTextSharp是一个流行的开源库,用于在C#中创建、操作和提取PDF文档中的数据。 它文档齐全且被广泛使用。 另一方面,IronPDF更加现代,具有额外的功能和优势,使其成为开发人员更好的选择。 从HTML输入字符串生成PDF 以下是使用IronPDF从HTML创建PDF的方法: using IronPdf; /** * IronPDF HTML to PDF * anchor-ironpdf-document-from-html **/ private void HTMLString() { // Render HTML to PDF var Renderer = new IronPdf.ChromePdfRenderer(); using var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>"); Renderer.RenderingOptions.TextFooter = new HtmlHeaderFooter() { HtmlFragment = "<div style='text-align:right'><em style='color:pink'>page {page} of {total-pages}</em></div>" }; var OutputPath = "ChromeHtmlToPdf.pdf"; PDF.SaveAs(OutputPath); } using IronPdf; /** * IronPDF HTML to PDF * anchor-ironpdf-document-from-html **/ private void HTMLString() { // Render HTML to PDF var Renderer = new IronPdf.ChromePdfRenderer(); using var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>"); Renderer.RenderingOptions.TextFooter = new HtmlHeaderFooter() { HtmlFragment = "<div style='text-align:right'><em style='color:pink'>page {page} of {total-pages}</em></div>" }; var OutputPath = "ChromeHtmlToPdf.pdf"; PDF.SaveAs(OutputPath); } $vbLabelText $csharpLabel iText 7 HTML到PDF 使用iText 7将HTML文本转换为PDF: using iText.Html2pdf; using System.IO; /** * iText HTML to PDF * anchor-itext-html-to-pdf **/ private void HTMLString() { HtmlConverter.ConvertToPdf("<h1>Hello iText7</h1>", new FileStream("iText7HtmlToPdf.pdf", FileMode.Create)); } using iText.Html2pdf; using System.IO; /** * iText HTML to PDF * anchor-itext-html-to-pdf **/ private void HTMLString() { HtmlConverter.ConvertToPdf("<h1>Hello iText7</h1>", new FileStream("iText7HtmlToPdf.pdf", FileMode.Create)); } $vbLabelText $csharpLabel 性能 IronPDF的设计比iTextSharp更快更高效,允许使用更少的资源更快地生成PDF。 这种效率对大或复杂的文档至关重要。 定价 iTextSharp对某些用例需要商业许可证,可能会很贵。而IronPDF提供更实惠的定价模式,拥有针对不同需求和预算量身定制的多种选项。 许可证和定价 iTextSharp和IronPDF的主要区别之一在于其许可证和定价模型。 iTextSharp:根据 AGPL 许可协议授权,非开源项目需要商业许可。 商业许可证的费用各不相同。 IronPDF:提供免费试用版和灵活的许可方式,包括开发者许可和服务器许可,使其适合商业用途。 结论 总之,虽然iTextSharp和IronPDF都能处理C#中的PDF操作,IronPDF在多功能性和效率上卓越,它提供先进的功能,直观的API和更好的性能。 其灵活的定价使其适用于商业项目和更大规模的组织。 通过IronPDF卓越的HTML到PDF转换,开发人员可以轻松生成包含丰富媒体或交互式内容的报告或文档。 加上具有成本效益的价格,IronPDF是需要强大且高效的C#项目PDF库开发人员的绝佳选择。 {i:(iTextSharp 是其各自所有者的注册商标。 本网站与 iTextSharp 无关,也未得到 iTextSharp 的支持或赞助。所有产品名称、徽标和品牌均为其各自所有者的财产。 比较仅供参考,反映撰写时公开可用的信息。)}] 常见问题解答 如何在 C# 中编辑 PDF 文件而不丢失格式? 您可以使用 IronPDF 在 C# 中编辑 PDF 文件,确保保留格式。IronPDF 提供先进的功能和现代 API 以高效处理 PDF。 在 Visual Studio 中安装 PDF 库需要哪些步骤? 要在 Visual Studio 中安装像 IronPDF 这样的 PDF 库,打开 NuGet 包管理器,搜索 IronPDF,并将包安装到您的项目中。 如何在 C# 中将网页 URL 转换为 PDF? IronPDF 允许您使用 ChromePdfRenderer 类将网页 URL 转换为 PDF,确保高质量输出。 iTextSharp 和 IronPDF 在授权方面有哪些区别? iTextSharp 在 AGPL 下授权,需要商业许可证用于非开源项目,而 IronPDF 提供灵活的授权选项,包括免费试用。 如何使用 C# 向现有 PDF 添加文本? 使用 IronPDF,您可以通过在 PdfDocument 对象上使用 AddText 方法来向现有 PDF 添加文本,实现无缝编辑。 使用 IronPDF 而不是 iTextSharp 有哪些优势? IronPDF 提供优秀的性能、现代化的 API 和灵活的定价。它还提供先进的 HTML 转 PDF 转换和更好的输出质量,使其成为 C# 中 PDF 编辑的首选。 在 C# 项目中使用 IronPDF 需要什么? 您需要 Visual Studio IDE 和通过 NuGet 包管理器安装的 IronPDF 库,以便在您的 C# 项目中使用 IronPDF。 我可以在 C# 中从 HTML 字符串创建 PDF 吗? 可以,IronPDF 允许您使用 RenderHtmlAsPdf 方法从 HTML 字符串创建 PDF,提供了一个强大的 HTML 转 PDF 转换工具。 是什么使 IronPDF 成为 C# 开发人员的多功能工具? IronPDF 的直观 API、高效性能、先进的 HTML 转 PDF 转换以及具有成本效益的定价,使其成为 C# 开发人员的多功能工具。 开发人员如何确保在 C# 中生成高质量 PDF? 通过使用 IronPDF,开发人员可以确保高质量的 PDF 输出,因为其先进的渲染引擎和专业 PDF 操作的全面功能集。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 相关文章 已更新2026年3月1日 在ASP.NET MVC中生成PDF:iTextSharp vs. IronPDF指南 使用 iTextSharp 与 IronPDF for .NET 比较 ASP.NET MVC 中的 PDF 生成方法。了解哪个库能提供更好的 HTML 渲染和更简便的实施。 阅读更多 已更新2026年2月1日 Ghostscript GPL 与 IronPDF:技术比较指南 了解 Ghostscript GPL 和 IronPDF 的主要区别。比较 AGPL 许可与商业许可、命令行开关与本地 .NET API 以及 HTML 到 PDF 的功能。 阅读更多 已更新2026年3月1日 ASP PDF 库:比较 IronPDF、Aspose 和 Syncfusion 发现适合ASP.NET Core应用程序的最佳PDF库。比较IronPDF的Chrome引擎与Aspose和Syncfusion的替代品。 阅读更多 创建PDF以字节数组C# iTextSharp(vs IronPDF)什么是iText?(使用IronPDF...
已更新2026年3月1日 在ASP.NET MVC中生成PDF:iTextSharp vs. IronPDF指南 使用 iTextSharp 与 IronPDF for .NET 比较 ASP.NET MVC 中的 PDF 生成方法。了解哪个库能提供更好的 HTML 渲染和更简便的实施。 阅读更多
已更新2026年2月1日 Ghostscript GPL 与 IronPDF:技术比较指南 了解 Ghostscript GPL 和 IronPDF 的主要区别。比较 AGPL 许可与商业许可、命令行开关与本地 .NET API 以及 HTML 到 PDF 的功能。 阅读更多
已更新2026年3月1日 ASP PDF 库:比较 IronPDF、Aspose 和 Syncfusion 发现适合ASP.NET Core应用程序的最佳PDF库。比较IronPDF的Chrome引擎与Aspose和Syncfusion的替代品。 阅读更多