在生产环境中测试,无水印。
随时随地满足您的需求。
获得30天的全功能产品。
几分钟内就能启动并运行。
在您的产品试用期间,全面访问我们的支持工程团队。
对于使用 PDF 的开发人员来说,拥有一个可靠的 PDF 生成和操作库至关重要。 在 .NET 生态系统中,有几十个 C# PDF 库可供选择,那么您如何选择最适合您需求的一个呢?
在 .NET 应用程序中使用 PDF 功能时,选择合适的库对于高效开发至关重要。 本文详细比较了两个著名的C# PDF库:IronPDF 和 iText 7(前称为iTextSharp)。 我们将探讨它们的功能、性能、许可以及适合各种项目需求的情况,以帮助您做出明智的决定。
PDF 广泛用于报告、发票和法律文件,因此 PDF 生成和操作对于许多应用程序来说至关重要。 选择库时,需考虑的关键因素包括:
IronPDF是一个专为.NET开发人员设计的商业PDF库。 它简化了PDF的生成、操作和转换,使其成为C#应用程序中最易于使用的库之一。
IronPDF 支持 .NET Core、.NET Framework 和 .NET Standard,确保在各种 .NET 环境中的兼容性。 其高度的跨平台兼容性使其成为在不同应用环境中工作的团队的理想选择,并且它与诸如Visual Studio之类的IDE完美集成。 除了其 .NET 版本外,IronPDF 还提供 Java、Python 和 Node.js 版本。
🔹 关键功能:
表单填写和数据提取——以编程方式填充交互式PDF 表单并提取表单数据。
📌 最佳适用:希望获得简便、综合解决方案的开发人员,无需额外的插件或复杂的许可证。
iText 7 是一个强大且灵活的 PDF 库,提供广泛的 PDF 操作功能,包括文档创建、加密和签名。 然而,其核心库本身并不支持HTML到PDF的转换。
🔹 关键功能:
PDF 表单管理:创建和编辑用于交互式 PDF 表单的 AcroForms 和 XFA 表单。
📌 最适合:需要高度可定制的PDF解决方案并愿意购买额外附加功能以扩展功能的开发人员。
在我们进入功能及其相应的代码示例之前,让我们先来看一下IronPDF和iText 7之间的一个最大功能差异,HTML转PDF转换。
iText 7,另一方面,需要pdfHTML 插件,这是一个商业许可证下的付费功能。 这增加了需要网页转PDF功能的开发者的成本。
HTML 转 PDF 比较流程图
📌 底线:如果您需要HTML 到 PDF 转换,IronPDF 是更具成本效益的解决方案,因为它开箱即用包含此功能。
IronPDF 拥有丰富的功能集,可以处理 PDF 文档。 这些涵盖了从PDF创建到PDF操作和安全。 为了更清楚地了解这个库所提供的广泛功能,我们将查看几个精选的关键功能。
将HTML内容转换为高质量的PDF文档,使用IronPDF强大的渲染引擎。IronPDF的渲染器不仅仅是转换HTML内容,它还可以保持所有原始CSS样式和JavaScript交互性。
using IronPdf;
public class Program
{
static void Main(string[] args)
{
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlFileAsPdf("example.html");
pdf.SaveAs("HtmlToPdf.pdf");
}
}
using IronPdf;
public class Program
{
static void Main(string[] args)
{
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlFileAsPdf("example.html");
pdf.SaveAs("HtmlToPdf.pdf");
}
}
输入 HTML
输入HTML内容
输出 PDF
使用 IronPDF 的 HTML 转 PDF 输出
在此代码示例中,我们首先创建了一个新的ChromePdfRenderer
实例,使我们能够访问IronPDF用于将HTML渲染为PDF的强大渲染引擎。 然后,我们将一个 HTML 文件传递给 RenderHtmlFileAsPdf()
方法,该方法将 HTML 渲染为 PDF,并存储在 PdfDocument
对象中。 最后,我们将 PDF 保存到指定的文件位置。
对于希望将URL内容转换为PDF的开发人员,IronPDF是您的不二之选。 使用此库,您将能够通过ChromePdfRenderer渲染引擎创建像素完美的PDF文档,该引擎在渲染URL到PDF时会保持所有原始样式和布局。 在这个例子中,我们将使用这个URL,来演示IronPDF如何处理更复杂的CSS样式。
using IronPdf;
public class Program
{
static void Main(string[] args)
{
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.apple.com");
pdf.SaveAs("UrlToPdf.pdf");
}
}
using IronPdf;
public class Program
{
static void Main(string[] args)
{
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.apple.com");
pdf.SaveAs("UrlToPdf.pdf");
}
}
PDF 输出:
使用 IronPDF 从 URL 导出为 PDF
就像在我们的HTML转PDF示例中一样,使用IronPDF将任何URL转换为PDF的第一步是首先创建一个新的ChromePdfRenderer
实例。 一旦方法将 URL 内容渲染成 PDF 格式,使用RenderUrlAsPdf
,它将生成的 PDF 保存到一个新的PdfDocument
对象,然后我们使用SaveAs
方法保存 PDF。
通过在您的 PDF 文档上应用数字签名,确保您的 PDF 文档的真实性。 开发人员可能会考虑应用数字签名的不同方法,例如使用安全证书对PDF进行数字签名,将手写签名的图像添加到PDF中,或将证书的图像直接盖印到PDF上。
using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;
using IronSoftware.Drawing;
public class Program
{
static void Main(string[] args)
{
// Create PdfSignature object
X509Certificate2 cert = new X509Certificate2("IronSoftware.pfx", "your-password", X509KeyStorageFlags.Exportable);
var sig = new PdfSignature(cert);
sig.SignatureImage = new PdfSignatureImage("IronPdf.png", 0, new Rectangle(150, 100, 350, 250));
// Sign and save PDF document
sig.SignPdfFile("product_report.pdf");
}
}
using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;
using IronSoftware.Drawing;
public class Program
{
static void Main(string[] args)
{
// Create PdfSignature object
X509Certificate2 cert = new X509Certificate2("IronSoftware.pfx", "your-password", X509KeyStorageFlags.Exportable);
var sig = new PdfSignature(cert);
sig.SignatureImage = new PdfSignatureImage("IronPdf.png", 0, new Rectangle(150, 100, 350, 250));
// Sign and save PDF document
sig.SignPdfFile("product_report.pdf");
}
}
输出 PDF
使用 IronPDF 的数字签名输出
在此示例中,我们加载了证书对象,创建了签名的视觉表示,或者在我们的例子中是IronPDF图像,并创建了一个新的PdfSignature
对象,该对象负责PDF文档本身的签署。 最后,我们使用SignPdfFile
对我们的PDF文档进行了签名并保存。
如果您想探索更多 IronPDF 提供的功能,请务必查看其信息丰富的功能页面,或包含每个功能深入代码示例的操作指南。
iText7 提供了广泛的功能,以定制和增强您的 PDF 文档。 这款 PDF 库涵盖了多种 PDF 标准的广泛格式支持和先进的 PDF 操作功能。 然而,如前所述,iText7 可能需要额外的软件包才能执行某些与 PDF 相关的任务,例如 HTML 转 PDF。
虽然iText7本身不能处理HTML到PDF的转换,但我们可以利用iText7商业许可下的付费附加组件pdfHTML,将IronPDF示例中使用的HTML文件转换为PDF文档。
using iText.Html2pdf;
public class Program
{
static void Main(string[] args)
{
using (FileStream htmlSource = File.Open("example.html", FileMode.Open))
using (FileStream pdf = File.Open("HtmlToPdfOutput.pdf", FileMode.Create))
{
ConverterProperties converterProperties = new ConverterProperties();
HtmlConverter.ConvertToPdf(htmlSource, pdf, converterProperties);
pdf.Close();
}
}
}
using iText.Html2pdf;
public class Program
{
static void Main(string[] args)
{
using (FileStream htmlSource = File.Open("example.html", FileMode.Open))
using (FileStream pdf = File.Open("HtmlToPdfOutput.pdf", FileMode.Create))
{
ConverterProperties converterProperties = new ConverterProperties();
HtmlConverter.ConvertToPdf(htmlSource, pdf, converterProperties);
pdf.Close();
}
}
}
输出 PDF
iText7 HTML 转换为 PDF 输出
在这个例子中,我们加载了HTML文件,并指定了保存渲染PDF的文件位置。 然后,使用ConvertToPdf
方法,我们可以轻松地将HTML文件转换为PDF文档。
现在,是时候比较一下iText7在将URL转换为PDF时与IronPDF的表现如何。 为此,我们将使用与之前完全相同的 URL 以确保公平比较。
using System;
using System.Net.Http;
using System.IO;
using iText.Html2pdf;
public class Program
{
public static async System.Threading.Tasks.Task Main(string[] args)
{
string url = "https://www.apple.com"; // Replace with your target URL
string outputPdfPath = "output.pdf";
try
{
// Download HTML content from the URL
using (HttpClient client = new HttpClient())
{
string htmlContent = await client.GetStringAsync(url);
// Convert HTML to PDF
using (FileStream pdfStream = new FileStream(outputPdfPath, FileMode.Create))
{
ConverterProperties properties = new ConverterProperties();
HtmlConverter.ConvertToPdf(htmlContent, pdfStream, properties);
}
}
Console.WriteLine("PDF created successfully: " + outputPdfPath);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
using System;
using System.Net.Http;
using System.IO;
using iText.Html2pdf;
public class Program
{
public static async System.Threading.Tasks.Task Main(string[] args)
{
string url = "https://www.apple.com"; // Replace with your target URL
string outputPdfPath = "output.pdf";
try
{
// Download HTML content from the URL
using (HttpClient client = new HttpClient())
{
string htmlContent = await client.GetStringAsync(url);
// Convert HTML to PDF
using (FileStream pdfStream = new FileStream(outputPdfPath, FileMode.Create))
{
ConverterProperties properties = new ConverterProperties();
HtmlConverter.ConvertToPdf(htmlContent, pdfStream, properties);
}
}
Console.WriteLine("PDF created successfully: " + outputPdfPath);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
输出 PDF
iText7 URL 转换为 PDF 输出
如这里所见,iText7 将 URL 转换为 PDF 的方法更加手动和复杂。 首先,我们需要从URL下载HTML内容,然后按照HTML转PDF示例中的类似步骤,将我们的URL内容渲染为PDF文档并保存。 如您在输出图像中所见,iText7 未能保持大部分原始样式和布局,而 IronPDF 则不同。
using System.Security.Cryptography.X509Certificates;
using iText.Kernel.Pdf;
using iText.Signatures;
using iText.Bouncycastle.Crypto;
using iText.Commons.Bouncycastle.Cert;
using iText.Commons.Bouncycastle.Crypto;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Crypto;
using iText.Bouncycastle.X509;
using iText.Kernel.Crypto;
public partial class Program
{
static void Main(string[] args)
{
string inputPdf = "input.pdf"; // PDF to be signed
string outputPdf = "signed_output.pdf"; // Signed PDF output
string pfxFile = "IronSoftware.pfx"; // Path to your PFX certificate
string password = "Passw0rd"; // Password for PFX file
try
{
// Load your certificate
Pkcs12Store ks = new Pkcs12StoreBuilder().Build();
using (FileStream fs = new FileStream(pfxFile, FileMode.Open, FileAccess.Read))
{
ks.Load(fs, password.ToCharArray());
}
string? alias = null;
foreach (string al in ks.Aliases)
{
if (ks.IsKeyEntry(al))
{
alias = al;
break;
}
}
if (alias == null)
{
throw new Exception("Alias not found in the PFX file.");
}
ICipherParameters pk = ks.GetKey(alias).Key;
X509CertificateEntry[] chain = ks.GetCertificateChain(alias);
X509Certificate2 cert = new X509Certificate2(chain[0].Certificate.GetEncoded());
// Convert BouncyCastle certificate to iText certificate
var itextCertChain = new IX509Certificate[chain.Length];
for (int i = 0; i < chain.Length; i++)
{
itextCertChain[i] = new X509CertificateBC(chain[i].Certificate);
}
// Create output PDF with signed content
using (PdfReader reader = new PdfReader(inputPdf))
using (FileStream os = new FileStream(outputPdf, FileMode.Create, FileAccess.Write))
{
PdfSigner signer = new PdfSigner(reader, os, new StampingProperties().UseAppendMode());
// Set up the external signature (private key + digest algorithm)
IPrivateKey iTextPrivateKey = new PrivateKeyBC(pk);
IExternalSignature pks = new PrivateKeySignature(iTextPrivateKey, DigestAlgorithms.SHA256);
IExternalDigest digest = new BouncyCastleDigest();
// Perform the signing (detached signature)
signer.SignDetached(digest, pks, itextCertChain, null, null, null, 0, PdfSigner.CryptoStandard.CMS);
}
Console.WriteLine($"PDF digitally signed successfully: {outputPdf}");
}
catch (Exception ex)
{
Console.WriteLine($"Error signing PDF: {ex.Message}");
}
}
}
using System.Security.Cryptography.X509Certificates;
using iText.Kernel.Pdf;
using iText.Signatures;
using iText.Bouncycastle.Crypto;
using iText.Commons.Bouncycastle.Cert;
using iText.Commons.Bouncycastle.Crypto;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Crypto;
using iText.Bouncycastle.X509;
using iText.Kernel.Crypto;
public partial class Program
{
static void Main(string[] args)
{
string inputPdf = "input.pdf"; // PDF to be signed
string outputPdf = "signed_output.pdf"; // Signed PDF output
string pfxFile = "IronSoftware.pfx"; // Path to your PFX certificate
string password = "Passw0rd"; // Password for PFX file
try
{
// Load your certificate
Pkcs12Store ks = new Pkcs12StoreBuilder().Build();
using (FileStream fs = new FileStream(pfxFile, FileMode.Open, FileAccess.Read))
{
ks.Load(fs, password.ToCharArray());
}
string? alias = null;
foreach (string al in ks.Aliases)
{
if (ks.IsKeyEntry(al))
{
alias = al;
break;
}
}
if (alias == null)
{
throw new Exception("Alias not found in the PFX file.");
}
ICipherParameters pk = ks.GetKey(alias).Key;
X509CertificateEntry[] chain = ks.GetCertificateChain(alias);
X509Certificate2 cert = new X509Certificate2(chain[0].Certificate.GetEncoded());
// Convert BouncyCastle certificate to iText certificate
var itextCertChain = new IX509Certificate[chain.Length];
for (int i = 0; i < chain.Length; i++)
{
itextCertChain[i] = new X509CertificateBC(chain[i].Certificate);
}
// Create output PDF with signed content
using (PdfReader reader = new PdfReader(inputPdf))
using (FileStream os = new FileStream(outputPdf, FileMode.Create, FileAccess.Write))
{
PdfSigner signer = new PdfSigner(reader, os, new StampingProperties().UseAppendMode());
// Set up the external signature (private key + digest algorithm)
IPrivateKey iTextPrivateKey = new PrivateKeyBC(pk);
IExternalSignature pks = new PrivateKeySignature(iTextPrivateKey, DigestAlgorithms.SHA256);
IExternalDigest digest = new BouncyCastleDigest();
// Perform the signing (detached signature)
signer.SignDetached(digest, pks, itextCertChain, null, null, null, 0, PdfSigner.CryptoStandard.CMS);
}
Console.WriteLine($"PDF digitally signed successfully: {outputPdf}");
}
catch (Exception ex)
{
Console.WriteLine($"Error signing PDF: {ex.Message}");
}
}
}
输出 PDF
iText7 数字签名输出
如您所见,虽然iText7能够对PDF文档进行数字签名,但这一过程往往比IronPDF要复杂得多。 此代码加载 PFX 证书并使用它对 PDF 进行数字签名。 它提取私钥和证书,设置签名者,并向PDF添加分离签名,然后保存已签名的文档。
IronPDF以其易用性和优化的高性能而闻名,提供了一个更简单的API,简化了诸如HTML转PDF等常见任务。 它提供了高级方法,可以抽象复杂的PDF处理任务,使开发人员能够以最少的代码生成、编辑和操作PDF。 对于多线程或大规模文档处理,IronPDF支持并行执行。
另一方面,iText 7 提供了更详细和颗粒化的控制,这对需要广泛自定义的开发人员来说可能是有利的。 它提供了用于处理低级PDF操作的强大API。然而,由于其复杂性,iText 7通常需要更多的代码才能实现与IronPDF相似的结果。
在为 .NET 项目选择 PDF 库时,许可和成本考虑至关重要。 IronPDF和iText 7采用不同的授权模式,选择合适的授权取决于项目的要求、预算和合规需求。
IronPDF 采用商业许可模式,这意味着虽然它为商业许可提供免费试用,并且在开发和评估时免费,但在全面生产使用时需要付费许可证。定价透明,并取决于使用规模、开发人员数量和项目类型等因素。
🔹 IronPDF 许可:
✅ 最佳选择企业需要简单且具有成本效益的PDF解决方案。
📌 底线:IronPDF 将所有主要功能包含在一个许可证中,使其成为团队和企业的经济实惠选择。
iText7 采用 双重许可模式,其中包括:
AGPL(GNU Affero 通用公共许可证)——对开源项目免费,但要求使用 iText7 的整个项目必须是开源的并符合 AGPL。 这意味着对项目所作的任何修改或添加也必须公开共享。
商业许可证 – 适用于任何不愿公开其源代码的专有软件或商业应用程序。 定价结构根据使用情况、支持水平和部署规模而有所不同。
对于希望将iText 7集成到专有软件中的公司,商业许可证是必需的。 成本可能很高,尤其是针对企业级解决方案,因为其定价是按照每位开发人员计算的,但它提供专业支持并确保合法合规。
🔹 为什么 iText 7 可能更昂贵:
基本功能需要昂贵的附加组件:
pdfHTML(付费)– 用于HTML到PDF的转换。
pdfOCR(付费)– 需要用于从图像中识别文本。
pdfCalligraph(付费)– 改善文本渲染和字体支持。
📌 底线:如果您的团队需要多个开发人员以及 HTML 转 PDF 和 OCR 等关键功能,iText 7 的费用可能会比 IronPDF 高出许多,而 IronPDF 提供这些功能且无需额外费用。
小型 vs. 企业项目
对于需要快速实现PDF功能且配置要求较少的小型到中型项目而言,IronPDF 是一个引人注目的选择,因为它拥有用户友好的 API 和全面的功能集。 需要广泛定制和遵循特定 PDF 标准的企业级项目可能会从 iText7 的高级功能中受益,然而,IronPDF 由于其高性能、灵活的许可选项和可用性,也被证明是这一层次工作的强有力候选者。
学术和商业用途
在学术环境或开源项目中,iText 7 的 AGPL 许可证允许免费使用,前提是项目的许可证兼容。 对于商业应用,IronPDF和iText 7都需要购买商业许可证。 建议查看每个库的许可条款,以确保符合您项目的目标。
为您的 .NET 项目选择合适的 PDF 库是一个关键的决定,取决于易用性、功能集和许可需求等因素。 IronPDF 和 iText 7 都提供强大的 PDF 功能,但它们适用于不同的需求。 除了这两个库以外,Aspose、Syncfusion 和 PDFSharp 等竞争对手都提供有竞争力的 .NET PDF 库。 然而,IronPDF 凭借其易于使用的 API、全面的功能集以及成本效益,始终在 PDF 行业中处于领先地位。
以下是我们总结的一个有力论据,说明为什么IronPDF是满足所有.NET PDF库需求的绝佳选择。
IronPDF与iText7对比总结
如果您的项目需要快速生成 PDF,尤其是从 Web 内容生成,并且您正在寻找一种易于使用的解决方案,那么 IronPDF 可能是更好的选择。但是,如果您的应用程序需要高级 PDF 操作或严格符合 PDF 标准,并且您需要灵活性来进行广泛的自定义,那么 iText7 可能更适合。 考虑您的项目特定需求和许可证限制,以确定最适合您的库。
🎯 今日试用 IronPDF: 下载免费试用版,开始亲自探索 IronPDF 的强大功能!
立即在您的项目中开始使用IronPDF,并享受免费试用。