如何使用 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 PDF Framework遷移到IronPDF,將您的PDF生成工作流程從一個大型套件中以坐標為基礎的圖形API轉變為以HTML/CSS為優先、使用現代Chromium渲染的獨立程式庫。 此指南提供完整的逐步遷移路徑,消除僅限套件的授權、複雜的部署要求以及以坐標為基礎的定位。
為什麼從Syncfusion PDF遷移到IronPDF
了解Syncfusion PDF Framework
Syncfusion PDF Framework是一個全面的程式庫,提供使用C#建立、編輯和保護PDF文件的廣泛功能。 它作為Syncfusion's Essential Studio的一部分提供,其中包括多個平台上的上千個組件。
PDF程式庫不能作為單一組件購買。 Syncfusion現提供一個"Document SDK"層級,將PDF、Word、Excel和PowerPoint程式庫與完整的Essential Studio套件一起捆綁,沒有UI組件。需要僅PDF功能的團隊至少必須選擇Document SDK,當其他文件程式庫未使用時,這可能會顯得繁瑣。
捆綁授權問題
Syncfusion的授權模式對僅需要PDF功能的團隊造成顯著挑戰:
- 無法按程式庫購買:PDF被捆綁至Document SDK(PDF + Word + Excel + PowerPoint)或完整的Essential Studio中 — 無法單獨授權
- 社區授權限制:免費層級要求年收入小於100萬美元,開發者不超過5人,且總員工不超過10人,外部資本終生限制為300萬美元
- 複雜的部署授權:網頁、桌面、伺服器部署需不同授權
- 需要年度續約:訂閱模式,需支付年度費用(最短1年期)
- 按開發者定價:費用隨團隊規模線性增長
- 捆綁冗餘: Document SDK即使您只需要PDF,也會引入Word/Excel/PowerPoint的依賴; 完整的Essential Studio更增加1000+ UI組件
Syncfusion PDF與IronPDF比較
| 方面 | Syncfusion PDF | IronPDF |
|---|---|---|
| 購買模式 | Document 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 |
| 捆綁要求 | 是(Document 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包變更
# Remove Syncfusion packages
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
# Remove Syncfusion packages
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 any Syncfusion calls
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("YOUR-SYNCFUSION-KEY");
// Must register before any Syncfusion calls
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: Syncfusion PDF
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: Syncfusion PDF
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
之後(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需要一個FileStream,保存並關閉文件和流。
IronPDF使用RenderUrlAsPdf(), 僅需三行程式碼。 無需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
之後(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使用坐標為基礎的圖形模型。 您建立一個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
之後(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,然後關閉六個獨立物件 (finalDocument, loadedDocument1, loadedDocument2, stream1, stream2, outputStream)。
IronPDF使用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");
' IronPDF:自動cleanup
pdf.SaveAs("Output.pdf")
功能比較
| 功能 | Syncfusion PDF | IronPDF |
|---|---|---|
| 獨立購買 | 否(Document SDK或Essential Studio) | 是 |
| 授權 | 商業授權附社區限制 | 簡化的商業 |
| HTML引擎 | Blink(平台專用NuGets) | 捆綁Chromium |
| CSS3支援 | 通過Blink現代 | 完整(flexbox, grid) |
| API風格 | 以坐標為基礎的圖形 + HTML轉換器 | HTML/CSS優先 |
| 流管理 | 手動 | 自動 |
| 依賴關係 | 多個套件 | 單一NuGet |
| 部署複雜性 | 潛在複雜 | 簡單明了 |
遷移檢查表
遷移前
- 編目所有程式碼庫中的Syncfusion PDF使用
- 記錄授權成本和部署要求
- 確認
PdfGrid,PdfGraphics, 和HtmlToPdfConverter的使用 - 獲取IronPDF授權金鑰來自ironpdf.com
程式碼更新
- 移除Syncfusion套件 (
Syncfusion.Pdf.Net.Core,Syncfusion.HtmlToPdfConverter.Net.Windows,Syncfusion.Licensing) - 安裝
IronPdfNuGet套件 - 更新命名空間導入 (
using Syncfusion.Pdf;→using IronPdf;) - 替換
IronPdf.License.LicenseKey = "..." - 替換
RenderHtmlAsPdf() - 替換
PdfDocument+Pages.Add()+ChromePdfRenderer.RenderHtmlAsPdf() - 替換
PdfDocument.FromFile() - 替換
PdfDocument.Merge() - 替換
pdf.SaveAs(path) - 移除所有
stream.Close()調用 - 替換
PdfGrid為HTML<table>元素 - 替換
PdfStandardFont為CSSfont-family - 替換
PdfBrushes為CSScolor屬性
測試
- PDF輸出的視覺比較
- 驗證CSS渲染改進(flexbox、grid現在可用)
- 測試文字提取
- 測試合併和分割
- 性能比較

