如何使用 iTextSharp | IronPDF 在 C# 中为 PDF 添加数字签名
Full Comparison
Looking for a detailed feature-by-feature breakdown? See how IronPDF stacks up against Syncfusion PDF on pricing, HTML support, and licensing.
从Syncfusion PDFFramework 迁移到IronPDF可将您的 PDF 生成工作流程从捆绑在大型套件中的基于坐标的图形 API 转变为具有现代 Chromium 渲染功能的独立、HTML/CSS 优先库。 本指南提供了一个完整的、循序渐进的迁移路径,消除了仅适用于套件的许可、复杂的部署要求和基于坐标的定位。
为什么要从Syncfusion PDF迁移到 IronPDF.
了解Syncfusion PDF框架
Syncfusion PDF Framework 是一个综合库,可为使用 C# 创建、编辑和保护 PDF 文档提供广泛的功能。 它是SyncfusionEssential Studio 的一部分,其中包括跨越多个平台的一千多个组件。
PDF 库不能作为单个组件购买。Syncfusion现在提供一个"文档 SDK"层,将 PDF、Word、Excel 和 PowerPoint 库捆绑在一起,不包含 UI 组件,同时还有完整的 Essential Studio 套件。仅需要 PDF 功能的团队至少必须选择文档 SDK,当其他文档库未使用时,这可能会很麻烦。
捆绑许可问题
Syncfusion 的 License 模式给只需要 PDF 功能的团队带来了巨大的挑战:
- 无单个库购买: PDF 被捆绑在文档 SDK(PDF + Word + Excel + PowerPoint)或完整的 Essential Studio 中——不能单独授权
- 社区许可证限制: 免费层要求年收入 <$1M,≤5 名开发者,AND ≤10 名总员工,并且外部资本的终身上限为 $3M 3.复杂的部署许可: Web、桌面、服务器部署需要不同的许可证
- 需要年度续订: 订阅模式具有年度成本(至少 1 年期限) 5.按开发人员定价:成本与团队规模呈线性关系
- 捆绑膨胀: 文档 SDK 会拉入 Word/Excel/PowerPoint 依赖项,虽然您只需要 PDF; 完整的 Essential Studio 还增加了 1000+ 个 UI 组件
Syncfusion PDF与IronPDF对比
| 方面 | Syncfusion PDF | IronPDF |
|---|---|---|
| 购买模式 | 文档 SDK 捆绑或完整 Essential Studio(没有单独库 SKU) | 单机版 |
| 许可 | 复杂的层级 | 针对每个开发人员的简单翻译 |
| 社区限制 | <$1M revenue, ≤5 devs, ≤10 employees, ≤$3M outside capital | 免费试用,然后授权 |
| 部署 | 多种许可证类型 | 一个许可证涵盖所有 |
| API 风格 | 基于坐标的图形 + HTML 转换器 | HTML/CSS 优先 |
| HTML 引擎 | Blink (单独的 Windows/Linux/Mac NuGets) | 捆绑的 Chromium |
| CSS支持 | 现代 (Blink) — 接近 Chromium 对等 | 完整的 CSS3/flexbox/grid |
| 依赖关系 | 多个软件包 | 单一 NuGet |
| 捆绑要求 | 是(文档 SDK 或 Essential Studio) | 否 |
| 聚焦 PDF | 范围广泛; 作为较大捆绑的一部分 | 窄; 以 PDF 为重点 |
IronPDF 通过将其 PDF 功能作为独立产品提供,提供了一种更有针对性的方法。 这种差异对成本考虑和集成的难易程度都有很大影响。
对于采用现代 .NET 的团队,IronPDF 的独立许可和 HTML/CSS 优先的方法提供了灵活性,无需捆绑依赖。
开始之前
前提条件
- .NET 环境: .NET Framework 4.6.2+ 或 .NET Core 3.1+ / .NET 5/6/7/8/9+
- NuGet 访问权限:能够安装 NuGet 包
- IronPDF 许可证:请从ironpdf.com获取您的许可证密钥。
NuGet 软件包变更
# RemoveSyncfusionpackages
dotnet remove package Syncfusion.Pdf.Net.Core
dotnet remove package Syncfusion.HtmlToPdfConverter.Net.Windows
dotnet remove package Syncfusion.Licensing
# Install IronPDF
dotnet add package IronPdf
# RemoveSyncfusionpackages
dotnet remove package Syncfusion.Pdf.Net.Core
dotnet remove package Syncfusion.HtmlToPdfConverter.Net.Windows
dotnet remove package Syncfusion.Licensing
# Install IronPDF
dotnet add package IronPdf
许可配置
Syncfusion:
// Must register before anySyncfusioncalls
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("YOUR-SYNCFUSION-KEY");
// Must register before anySyncfusioncalls
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("YOUR-SYNCFUSION-KEY");
' Must register before any Syncfusion calls
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("YOUR-SYNCFUSION-KEY")
IronPDF:
// One-time at startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
// One-time at startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
' One-time at startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
完整的 API 参考
命名空间变更
// Before:SyncfusionPDF
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
using Syncfusion.HtmlConverter;
using Syncfusion.Drawing;
// After: IronPDF
using IronPdf;
using IronPdf.Rendering;
// Before:SyncfusionPDF
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
using Syncfusion.HtmlConverter;
using Syncfusion.Drawing;
// After: IronPDF
using IronPdf;
using IronPdf.Rendering;
Imports IronPdf
Imports IronPdf.Rendering
核心 API 映射
| Syncfusion | IronPDF |
|---|---|
PdfDocument |
ChromePdfRenderer |
PdfLoadedDocument |
PdfDocument.FromFile() |
HtmlToPdfConverter |
ChromePdfRenderer |
graphics.DrawString() |
HTML 文本元素 |
graphics.DrawImage() |
<img> 标签 |
PdfGrid |
HTML <table> |
PdfStandardFont |
CSS font-family |
PdfBrushes.Black |
CSS color: black |
document.Security |
pdf.SecuritySettings |
PdfTextExtractor |
pdf.ExtractAllText() |
ImportPageRange() |
PdfDocument.Merge() |
document.Save(stream) |
pdf.SaveAs(path) |
document.Close(true) |
不需要 |
代码迁移示例
示例 1:HTML/URL 到 PDF 的转换
之前(Syncfusion PDF):
// NuGet: Install-Package Syncfusion.Pdf.Net.Core
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using System.IO;
class Program
{
static void Main()
{
// Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
// Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.example.com");
// Save the document
FileStream fileStream = new FileStream("Output.pdf", FileMode.Create);
document.Save(fileStream);
document.Close(true);
fileStream.Close();
}
}
// NuGet: Install-Package Syncfusion.Pdf.Net.Core
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using System.IO;
class Program
{
static void Main()
{
// Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
// Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.example.com");
// Save the document
FileStream fileStream = new FileStream("Output.pdf", FileMode.Create);
document.Save(fileStream);
document.Close(true);
fileStream.Close();
}
}
Imports Syncfusion.HtmlConverter
Imports Syncfusion.Pdf
Imports System.IO
Module Program
Sub Main()
' Initialize HTML to PDF converter
Dim htmlConverter As New HtmlToPdfConverter()
' Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert("https://www.example.com")
' Save the document
Dim fileStream As New FileStream("Output.pdf", FileMode.Create)
document.Save(fileStream)
document.Close(True)
fileStream.Close()
End Sub
End Module
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
// Create a PDF from a URL
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://www.example.com");
// Save the PDF
pdf.SaveAs("Output.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
// Create a PDF from a URL
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://www.example.com");
// Save the PDF
pdf.SaveAs("Output.pdf");
}
}
Imports IronPdf
Class Program
Shared Sub Main()
' Create a PDF from a URL
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderUrlAsPdf("https://www.example.com")
' Save the PDF
pdf.SaveAs("Output.pdf")
End Sub
End Class
本例展示了 API 的基本差异。Syncfusion PDF需要一个 HtmlToPdfConverter 实例,调用 Convert() 返回一个 PdfDocument,然后手动创建一个 FileStream,保存并关闭文档和流。
IronPDF 使用 ChromePdfRenderer 和 RenderUrlAsPdf() 仅需三行代码。 无需 FileStream 管理,无需 Close() 调用—IronPDF 自动处理清理。 请参阅 HTML 转 PDF 文档,了解全面的示例。
示例 2:从文本创建 PDF.
之前(Syncfusion PDF):
// NuGet: Install-Package Syncfusion.Pdf.Net.Core
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Drawing;
using System.IO;
class Program
{
static void Main()
{
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a page
PdfPage page = document.Pages.Add();
// Create a font
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
// Draw text
page.Graphics.DrawString("Hello, World!", font, PdfBrushes.Black, new PointF(10, 10));
// Save the document
FileStream fileStream = new FileStream("Output.pdf", FileMode.Create);
document.Save(fileStream);
document.Close(true);
fileStream.Close();
}
}
// NuGet: Install-Package Syncfusion.Pdf.Net.Core
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Drawing;
using System.IO;
class Program
{
static void Main()
{
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a page
PdfPage page = document.Pages.Add();
// Create a font
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
// Draw text
page.Graphics.DrawString("Hello, World!", font, PdfBrushes.Black, new PointF(10, 10));
// Save the document
FileStream fileStream = new FileStream("Output.pdf", FileMode.Create);
document.Save(fileStream);
document.Close(true);
fileStream.Close();
}
}
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Drawing
Imports System.IO
Module Program
Sub Main()
' Create a new PDF document
Dim document As New PdfDocument()
' Add a page
Dim page As PdfPage = document.Pages.Add()
' Create a font
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 12)
' Draw text
page.Graphics.DrawString("Hello, World!", font, PdfBrushes.Black, New PointF(10, 10))
' Save the document
Dim fileStream As New FileStream("Output.pdf", FileMode.Create)
document.Save(fileStream)
document.Close(True)
fileStream.Close()
End Sub
End Module
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Rendering;
class Program
{
static void Main()
{
// Create a PDF from HTML string
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello, World!</h1>");
// Save the document
pdf.SaveAs("Output.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Rendering;
class Program
{
static void Main()
{
// Create a PDF from HTML string
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello, World!</h1>");
// Save the document
pdf.SaveAs("Output.pdf");
}
}
Imports IronPdf
Imports IronPdf.Rendering
Class Program
Shared Sub Main()
' Create a PDF from HTML string
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Hello, World!</h1>")
' Save the document
pdf.SaveAs("Output.pdf")
End Sub
End Class
Syncfusion PDF 使用基于坐标的图形模型。 您创建一个 PdfDocument,添加一个 PdfPage,然后用 PdfFontFamily.Helvetica 创建一个 PdfFont,再用明确的坐标 (new PointF(10, 10))、字体和刷子 (PdfBrushes.Black) 调用 page.Graphics.DrawString()。 最后,您需要管理 FileStream 的创建和释放。
IronPDF 采用 HTML/CSS 优先的方法。 Instead of coordinates, you write <h1>Hello, World!</h1> and let CSS handle positioning, fonts, and colors. 这种方法更简单、更易于维护,并能充分利用开发人员已有的技能。 在我们的教程中了解更多信息。
示例 3:合并 PDF 文档
之前(Syncfusion PDF):
// NuGet: Install-Package Syncfusion.Pdf.Net.Core
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
using System.IO;
class Program
{
static void Main()
{
// Load the first PDF document
FileStream stream1 = new FileStream("Document1.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument1 = new PdfLoadedDocument(stream1);
// Load the second PDF document
FileStream stream2 = new FileStream("Document2.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument2 = new PdfLoadedDocument(stream2);
// Merge the documents
PdfDocument finalDocument = new PdfDocument();
finalDocument.ImportPageRange(loadedDocument1, 0, loadedDocument1.Pages.Count - 1);
finalDocument.ImportPageRange(loadedDocument2, 0, loadedDocument2.Pages.Count - 1);
// Save the merged document
FileStream outputStream = new FileStream("Merged.pdf", FileMode.Create);
finalDocument.Save(outputStream);
// Close all documents
finalDocument.Close(true);
loadedDocument1.Close(true);
loadedDocument2.Close(true);
stream1.Close();
stream2.Close();
outputStream.Close();
}
}
// NuGet: Install-Package Syncfusion.Pdf.Net.Core
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
using System.IO;
class Program
{
static void Main()
{
// Load the first PDF document
FileStream stream1 = new FileStream("Document1.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument1 = new PdfLoadedDocument(stream1);
// Load the second PDF document
FileStream stream2 = new FileStream("Document2.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument2 = new PdfLoadedDocument(stream2);
// Merge the documents
PdfDocument finalDocument = new PdfDocument();
finalDocument.ImportPageRange(loadedDocument1, 0, loadedDocument1.Pages.Count - 1);
finalDocument.ImportPageRange(loadedDocument2, 0, loadedDocument2.Pages.Count - 1);
// Save the merged document
FileStream outputStream = new FileStream("Merged.pdf", FileMode.Create);
finalDocument.Save(outputStream);
// Close all documents
finalDocument.Close(true);
loadedDocument1.Close(true);
loadedDocument2.Close(true);
stream1.Close();
stream2.Close();
outputStream.Close();
}
}
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing
Imports System.IO
Class Program
Shared Sub Main()
' Load the first PDF document
Dim stream1 As New FileStream("Document1.pdf", FileMode.Open, FileAccess.Read)
Dim loadedDocument1 As New PdfLoadedDocument(stream1)
' Load the second PDF document
Dim stream2 As New FileStream("Document2.pdf", FileMode.Open, FileAccess.Read)
Dim loadedDocument2 As New PdfLoadedDocument(stream2)
' Merge the documents
Dim finalDocument As New PdfDocument()
finalDocument.ImportPageRange(loadedDocument1, 0, loadedDocument1.Pages.Count - 1)
finalDocument.ImportPageRange(loadedDocument2, 0, loadedDocument2.Pages.Count - 1)
' Save the merged document
Dim outputStream As New FileStream("Merged.pdf", FileMode.Create)
finalDocument.Save(outputStream)
' Close all documents
finalDocument.Close(True)
loadedDocument1.Close(True)
loadedDocument2.Close(True)
stream1.Close()
stream2.Close()
outputStream.Close()
End Sub
End Class
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Load PDF documents
var pdf1 = PdfDocument.FromFile("Document1.pdf");
var pdf2 = PdfDocument.FromFile("Document2.pdf");
// Merge PDFs
var merged = PdfDocument.Merge(new List<PdfDocument> { pdf1, pdf2 });
// Save the merged document
merged.SaveAs("Merged.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Load PDF documents
var pdf1 = PdfDocument.FromFile("Document1.pdf");
var pdf2 = PdfDocument.FromFile("Document2.pdf");
// Merge PDFs
var merged = PdfDocument.Merge(new List<PdfDocument> { pdf1, pdf2 });
// Save the merged document
merged.SaveAs("Merged.pdf");
}
}
Imports IronPdf
Imports System.Collections.Generic
Class Program
Shared Sub Main()
' Load PDF documents
Dim pdf1 = PdfDocument.FromFile("Document1.pdf")
Dim pdf2 = PdfDocument.FromFile("Document2.pdf")
' Merge PDFs
Dim merged = PdfDocument.Merge(New List(Of PdfDocument) From {pdf1, pdf2})
' Save the merged document
merged.SaveAs("Merged.pdf")
End Sub
End Class
合并 PDF 文件的效果对比鲜明。Syncfusion PDF需要为每个输入文档创建 FileStream 对象,将它们加载为 PdfLoadedDocument,创建一个新的 PdfDocument,用每个源的起始和结束索引调用 ImportPageRange(),创建一个输出 FileStream,然后关闭 六个独立的对象 (outputStream)。
IronPDF 使用 PdfDocument.FromFile() 来加载每个 PDF,并有一个静态 PdfDocument.Merge() 方法接受一个文档列表。 无需流式管理,无需手动计算页面范围,无需千钧一发。
API 理念的主要差异
基于坐标与 HTML/CSS 优先
Syncfusion PDF 使用从传统 PDF 库继承的基于坐标的图形模型:
// Syncfusion: 手册 positioning
page.Graphics.DrawString("Text", font, PdfBrushes.Black, new PointF(100, 200));
page.Graphics.DrawRectangle(brush, new RectangleF(50, 50, 200, 100));
// Syncfusion: 手册 positioning
page.Graphics.DrawString("Text", font, PdfBrushes.Black, new PointF(100, 200));
page.Graphics.DrawRectangle(brush, new RectangleF(50, 50, 200, 100));
' Syncfusion: 手册 positioning
page.Graphics.DrawString("Text", font, PdfBrushes.Black, New PointF(100, 200))
page.Graphics.DrawRectangle(brush, New RectangleF(50, 50, 200, 100))
IronPDF 使用 HTML/CSS 进行排版:
// IronPDF: CSS-based positioning
var html = @"
<div style='margin: 50px; padding: 20px; border: 1px solid black;'>
<p style='color: black;'>Text</p>
</div>";
var pdf = renderer.RenderHtmlAsPdf(html);
// IronPDF: CSS-based positioning
var html = @"
<div style='margin: 50px; padding: 20px; border: 1px solid black;'>
<p style='color: black;'>Text</p>
</div>";
var pdf = renderer.RenderHtmlAsPdf(html);
' IronPDF: CSS-based positioning
Dim html As String = "
<div style='margin: 50px; padding: 20px; border: 1px solid black;'>
<p style='color: black;'>Text</p>
</div>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
对于网络开发人员来说,HTML/CSS 方法更直观、更易于维护,而且在不同页面尺寸下都能产生一致的效果。
流管理与自动清理
Syncfusion PDF 需要明确的流和文档处理:
// Syncfusion: 手册 cleanup
FileStream fileStream = new FileStream("Output.pdf", FileMode.Create);
document.Save(fileStream);
document.Close(true);
fileStream.Close();
// Syncfusion: 手册 cleanup
FileStream fileStream = new FileStream("Output.pdf", FileMode.Create);
document.Save(fileStream);
document.Close(true);
fileStream.Close();
' Syncfusion: 手册 cleanup
Dim fileStream As New FileStream("Output.pdf", FileMode.Create)
document.Save(fileStream)
document.Close(True)
fileStream.Close()
IronPDF 会自动处理清理工作:
// IronPDF:自动翻译cleanup
pdf.SaveAs("Output.pdf");
// IronPDF:自动翻译cleanup
pdf.SaveAs("Output.pdf");
功能对比
| 特征 | Syncfusion PDF | IronPDF |
|---|---|---|
| 购买 | 否(文档 SDK 或 Essential Studio) | 是 |
| 许可 | 有社区限制的商业翻译 | 简化商业 |
| HTML 引擎 | Blink (平台特定的 NuGets) | 捆绑的 Chromium |
| CSS3 支持。 | 现代通过 Blink | 完整(flexbox、网格) |
| API 风格 | 基于坐标的图形 + HTML 转换器 | HTML/CSS 优先 |
| 流管理 | 手册 | 自动翻译 |
| 依赖关系 | 多个软件包 | 单一 NuGet |
| 部署复杂性 | 潜在的复杂性 | 简单明了 |
迁移清单
迁移前
- 清点代码库中所有Syncfusion PDF的使用情况
- 文档许可成本和部署要求
- 识别
PdfGraphics和HtmlToPdfConverter的用法 - 从ironpdf.com获取IronPDF许可证密钥
代码更新
- 移除Syncfusion包(
Syncfusion.Pdf.Net.Core,Syncfusion.HtmlToPdfConverter.Net.Windows,Syncfusion.Licensing) - 安装
IronPdfNuGet 包 - 更新命名空间导入(
using Syncfusion.Pdf;→using IronPdf;) - 用
IronPdf.License.LicenseKey = "..."替换Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense() - 用
ChromePdfRenderer.RenderUrlAsPdf()或RenderHtmlAsPdf()替换HtmlToPdfConverter.Convert() - 用
ChromePdfRenderer.RenderHtmlAsPdf()替换PdfDocument+Pages.Add()+Graphics.DrawString() - 用
PdfDocument.FromFile()替换PdfLoadedDocument - 用
PdfDocument.Merge()替换ImportPageRange() - 用
pdf.SaveAs(path)替换document.Save(stream) - 移除所有
document.Close(true)和stream.Close()调用 - 用 HTML
<table>元素替换PdfGrid - 用CSS
font-family替换PdfStandardFont - 用 CSS
color属性替换PdfBrushes
测试
- PDF 输出的视觉比较
- 验证 CSS 渲染改进(flexbox、grid 现在可以正常工作)
- 测试文本提取
- 测试合并和拆分
- 性能比较

