如何使用 C# 將 Syncfusion PDF 遷移到 IronPDF
從 融合 PDF 框架遷移到 IronPDF,會將您的 PDF 產生工作流程從大型套件中捆綁的基於座標的圖形 API 轉變為具有現代 Chromium 渲染功能的獨立 HTML/CSS 優先庫。 本指南提供了一條完整的、逐步的遷移路徑,消除了僅限套件的許可、複雜的部署要求和基於座標的定位。
為什麼要從 融合 PDF 遷移到 IronPDF
了解 融合 PDF 框架
Syncfusion PDF 框架是一個綜合性的函式庫,它提供了使用 C# 建立、編輯和保護 PDF 文件的各種功能。 它是 融合 Essential Studio 的一部分,該工作室包含跨多個平台的 1000 多個元件。
然而,它最大的缺點之一是它不能作為獨立產品購買; 開發人員必須購買整套 融合 元件。 對於只對 PDF 功能感興趣的團隊來說,這項要求可能會很麻煩,尤其是因為該軟體包可能包含他們專案中不需要的工具。
捆綁許可問題
Syncfusion 的授權模式為只需要 PDF 功能的團隊帶來了巨大的挑戰:
1.僅限套件購買:無法單獨購買 PDF 庫-必須購買整個 Essential Studio 套件。 2.社群授權限制:免費版要求收入低於 100 萬美元且開發者人數少於 5 人。 3.複雜的部署許可:針對 Web、桌面和伺服器部署採用不同的許可證 4.需每年續訂:訂閱模式,按年收費 5.依開發人員定價:成本與團隊規模呈線性關係 6.套件臃腫:包含 1000 多個您可能不需要的組件
融合 PDF 與 IronPDF 的比較
| 方面 | 融合 PDF | IronPDF |
|---|---|---|
| 購買模式 | 限套裝包 | 獨立版 |
| 授權 | 複雜層級 | 每個開發者的簡單 |
| 社區界限 | 預算低於 100 萬美元,開發人員少於 5 人。 | 免費試用,之後需購買許可證 |
| 部署 | 多種許可證類型 | 一張許可證涵蓋所有 |
| API 風格 | 基於座標的圖形 | HTML/CSS優先 |
| HTML 支援 | 需要 BlinkBinaries | 天然鉻 |
| CSS 支援 | 有限的 | 完整的 CSS3/flexbox/grid |
| 依賴關係 | 多個包裹 | 單一 NuGet |
| 套件要求 | 是的(整套套房) | 不 |
| 聚焦PDF | 廣闊; 較大套房的一部分 | 狹窄的; 以PDF為重點 |
IronPDF 提供了一種更專注的方法,將其 PDF 功能作為獨立產品提供。 這種差異會對成本考量和整合難易度產生重大影響。
對於計劃在 2025 年和 2026 年採用 .NET 10 和 C# 14 的團隊而言,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 融合 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 融合 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許可證配置
融合:
// Must register before any 融合 calls
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("YOUR-SYNCFUSION-KEY");// Must register before any 融合 calls
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("YOUR-SYNCFUSION-KEY");IronPDF:
// One-time at startup
IronPdf.License.LicenseKey = "YOUR-IRONPDF-KEY";// One-time at startup
IronPdf.License.LicenseKey = "YOUR-IRONPDF-KEY";完整 API 參考
命名空間變更
// Before: 融合 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: 融合 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;核心 API 映射
| 融合 | IronPDF | 筆記 |
|---|---|---|
PdfDocument | ChromePdfRenderer | 建立PDF文件 |
PdfLoadedDocument | PdfDocument.FromFile() | 載入PDF文件 |
HtmlToPdfConverter | ChromePdfRenderer | HTML轉換 |
graphics.DrawString() | HTML 文字元素 | <p>,<h1> |
graphics.DrawImage() | <img>標籤 | HTML影像 |
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();
}
}(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");
}
}這個例子展示了 API 的根本區別。 融合 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();
}
}(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");
}
}Syncfusion PDF 使用基於座標的圖形模型。 您建立了一個PdfDocument ,新增了一個PdfPage ,建立了一個PdfFont ,字體為PdfFontFamily.Helvetica ,然後使用明確座標( 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();
}
}(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");
}
}合併PDF文件的差異非常顯著。 融合 PDF 需要為每個輸入文件建立FileStream對象,將其載入為PdfLoadedDocument ,建立一個新的PdfDocument ,使用每個來源的起始和結束索引呼叫ImportPageRange() ,建立一個輸出FileStream ,然後關閉六個單獨的物件loadedDocument1 finalDocument 、 loadedDocument2 stream1 stream2 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));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);HTML/CSS 方法對 Web 開發人員來說更直觀,更容易維護,並且在不同大小的頁面上都能產生一致的結果。
流管理與自動清理
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();IronPDF會自動處理清理工作:
// IronPDF: 自動的 cleanup
pdf.SaveAs("Output.pdf");// IronPDF: 自動的 cleanup
pdf.SaveAs("Output.pdf");功能對比
| 特徵 | 融合 PDF | IronPDF |
|---|---|---|
| 單獨購買 | 不(僅限套房) | 是的 |
| 授權 | 商業活動需遵守社區限制 | 簡化的商業 |
| HTML 轉 PDF | 需要 BlinkBinaries | 天然鉻 |
| CSS3 支持 | 有限的 | 完整(flexbox,grid) |
| API 風格 | 基於座標的圖形 | HTML/CSS優先 |
| 串流管理 | 手動的 | 自動的 |
| 依賴關係 | 多個包裹 | 單一 NuGet |
| 部署複雜性 | 可能很複雜 | 直截了當 |
遷移清單
遷移前
- 清點程式碼庫中所有 融合 PDF 的使用情況
- 文件許可成本和部署要求
- 辨識
PdfGrid、PdfGraphics和HtmlToPdfConverter的用法 - 從ironpdf.com取得 IronPDF 許可證金鑰
程式碼更新
- 移除 融合 套件(
Syncfusion.Pdf.Net.Core、Syncfusion.HtmlToPdfConverter.Net.Windows、Syncfusion.Licensing) 安裝IronPdfNuGet 套件 - 更新命名空間導入(
using Syncfusion.Pdf;→using IronPdf;) - 將
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense()替換為IronPdf.License.LicenseKey = "..." - 將
HtmlToPdfConverter.Convert()替換為ChromePdfRenderer.RenderUrlAsPdf()或RenderHtmlAsPdf() - 將
PdfDocument+Pages.Add()+Graphics.DrawString()替換為ChromePdfRenderer.RenderHtmlAsPdf() - 將
PdfLoadedDocument替換為PdfDocument.FromFile() - 將
ImportPageRange()替換為PdfDocument.Merge() - 將
document.Save(stream)替換為pdf.SaveAs(path) - 刪除所有
document.Close(true)和stream.Close()調用 - 將
PdfGrid替換為 HTML<table>元素 - 將
PdfStandardFont替換為 CSSfont-family - 將
PdfBrushes替換為 CSScolor屬性
測試
- PDF 輸出的視覺比較
- 驗證 CSS 渲染改進(flexbox、grid 現在可以正常運作)
- 測試文字擷取
- 測試合併和拆分
- 效能比較






