iTextSharp与IronPDF的PDF编辑比较
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和IronPDF编辑PDF比较:图1 - 在C#中使用iTextSharp创建PDF。
要在您的项目中安装iTextSharp库,可以使用NuGet包管理器。 打开您的Visual Studio项目,然后在解决方案资源管理器中右键单击项目名称。 从上下文菜单中选择"管理NuGet包"。 在NuGet包管理器中,搜索"iTextSharp"并安装最新版本的包。
!iTextSharp和IronPDF编辑PDF比较:图2 - 探索如何在ASP.NET C#中使用iTextSharp
创建一个新的PDF文件
要使用iTextSharp创建一个新的PDF文件,我们需要创建一个"文档"类的新实例,并将一个新的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);
}
}在上面的代码中,我们创建了一个名为"newfile.pdf"的新PDF文件并添加了一个段落标题。
!iTextSharp和IronPDF编辑PDF比较:图3 - iTextSharp PDF创建教程在C#中
编辑现有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();
}在此代码中,打开一个现有的PDF,并为其页面添加标题,同时确保文字对齐。
使用IronPDF编辑PDF文档
IronPDF是一个用于C#的强大PDF库,为编辑PDF文档提供便利。 本教程将指导如何使用IronPDF编辑现有的PDF文件,包括创建新的PDF文档、添加页面、合并PDF等。
!iTextSharp和IronPDF编辑PDF比较:图4 - IronPDF功能概述
先决条件
确保您有:
- Visual Studio IDE
- IronPDF库
步骤1:创建新项目
在 Visual Studio 中创建一个新的 C# 项目。 选择"控制台应用程序"项目类型。
步骤2:安装IronPDF
!iTextSharp和IronPDF编辑PDF比较:图5 - 安装IronPDF NuGet包
使用NuGet包管理器将IronPDF库安装到您的项目中。
// Execute this command in the Package Manager Console
Install-Package IronPdf// Execute this command in the Package Manager Console
Install-Package IronPdf步骤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);!iTextSharp和IronPDF编辑PDF比较:图6 - 使用IronPDF创建PDF
步骤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;步骤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");
}iTextSharp和IronPDF之间的差异
!iTextSharp和IronPDF编辑PDF比较:图7 - 在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);
}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));
}性能
IronPDF的设计比iTextSharp更快更高效,允许使用更少的资源更快地生成PDF。 这种效率对大或复杂的文档至关重要。
定价
iTextSharp对某些用例需要商业许可证,可能会很贵。而IronPDF提供更实惠的定价模式,拥有针对不同需求和预算量身定制的多种选项。
许可证和定价
iTextSharp和IronPDF的主要区别之一在于其许可证和定价模型。
- iTextSharp:根据 AGPL 许可协议授权,非开源项目需要商业许可。 商业许可证的费用各不相同。
- IronPDF:提供免费试用版和灵活的许可方式,包括开发者许可和服务器许可,使其适合商业用途。
结论
总之,虽然iTextSharp和IronPDF都能处理C#中的PDF操作,IronPDF在多功能性和效率上卓越,它提供先进的功能,直观的API和更好的性能。 其灵活的定价使其适用于商业项目和更大规模的组织。
通过IronPDF卓越的HTML到PDF转换,开发人员可以轻松生成包含丰富媒体或交互式内容的报告或文档。 加上具有成本效益的价格,IronPDF是需要强大且高效的C#项目PDF库开发人员的绝佳选择。
常见问题解答
如何在 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 操作的全面功能集。






