跳至页脚内容
迁移指南

如何用 C# 从 Syncfusion PDF 迁移到 IronPDF

从Syncfusion PDF迁移到 IronPDF:完整的 C# 迁移指南

从Syncfusion PDFFramework 迁移到IronPDF可将您的 PDF 生成工作流程从捆绑在大型套件中的基于坐标的图形 API 转变为具有现代 Chromium 渲染功能的独立、HTML/CSS 优先库。 本指南提供了一个完整的、循序渐进的迁移路径,消除了仅适用于套件的许可、复杂的部署要求和基于坐标的定位。

为什么要从Syncfusion PDF迁移到 IronPDF.

了解Syncfusion PDF框架

Syncfusion PDF Framework 是一个综合库,可为使用 C# 创建、编辑和保护 PDF 文档提供广泛的功能。 它是SyncfusionEssential Studio 的一部分,其中包括跨越多个平台的一千多个组件。

不过,它的一个最大缺点是不能作为独立产品购买; 开发人员必须购买整套Syncfusion组件。 对于只对 PDF 功能感兴趣的团队来说,这一要求可能会很麻烦,尤其是因为该捆绑包可能包括对他们的项目来说不必要的工具。

捆绑许可问题

Syncfusion 的 License 模式给只需要 PDF 功能的团队带来了巨大的挑战:

1.仅购买软件包:不能单独购买 PDF 库,必须购买整个 Essential Studio 2.社区许可限制:免费层需要 <$1M 收入和 <5 名开发人员 3.复杂部署许可:针对网络、桌面和服务器部署的不同许可 4.需要每年续订:按年付费的订阅模式 5.按开发人员定价:成本与团队规模成线性关系 6.Suite Bloat:包含 1000 多个您可能不需要的组件

Syncfusion PDF与IronPDF对比

方面Syncfusion PDFIronPDF
购买模式仅套件捆绑包单机版
许可复杂的层级针对每个开发人员的简单翻译
社区限制<$1M AND <5 开发人员免费试用,然后授权
部署多种许可证类型一个许可证涵盖所有
API 风格基于坐标的图形HTML/CSS 优先
HTML 支持需要 BlinkBinaries本地 Chromium
CSS支持有限的完整的 CSS3/flexbox/grid
依赖关系多个软件包单一 NuGet
套件要求是(整套)
聚焦 PDF范围广泛; 大型套件的一部分窄; 以 PDF 为重点

IronPDF 通过将其 PDF 功能作为独立产品提供,提供了一种更有针对性的方法。 这种差异对成本考虑和集成的难易程度都有很大影响。

对于计划在 2025 年和 2026 年之前采用 .NET 10 和 C# 14 的团队来说,IronPDF 的独立 License 和 HTML/CSS 优先方法提供了灵活性,而无需依赖套件。


开始之前

前提条件

1..NET环境:.NET Framework 4.6.2+ 或 .NET Core 3.1+ / .NET 5/6/7/8/9+ 2.NuGet 访问:安装 NuGet 软件包的能力 3.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
SHELL

许可配置

Syncfusion:

// Must register before anySyncfusioncalls
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("YOUR-SYNCFUSION-KEY");
// Must register before anySyncfusioncalls
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("YOUR-SYNCFUSION-KEY");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

IronPDF:

// One-time at startup
IronPdf.License.LicenseKey = "YOUR-IRONPDF-KEY";
// One-time at startup
IronPdf.License.LicenseKey = "YOUR-IRONPDF-KEY";
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

完整的 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;
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

核心 API 映射

SyncfusionIronPDF备注
<代码>PDF 文档</代码<代码>ChromePdfRenderer</代码创建 PDF
<代码>PDFLoadedDocument</代码<代码>PdfDocument.FromFile()</代码加载 PDF
<代码>HtmlToPdfConverter</代码<代码>ChromePdfRenderer</代码HTML 转换
<代码>graphics.DrawString()</代码HTML 文本元素<代码>

</代码>, <代码>

</代码

<代码>graphics.DrawImage()</代码<img> 标记HTML 图像
<代码>PdfGrid</代码HTML <table>表格
<代码>PDF 标准字体</代码CSS font-family字体
<代码>PdfBrushes.Black</代码CSS color: black颜色
<代码>document.安全性</代码<代码>pdf.SecuritySettings</代码安全性
<代码>PDFTextExtractor</代码<代码>pdf.ExtractAllText()</代码文本提取
<代码>ImportPageRange()</代码<代码>PdfDocument.Merge()</代码合并
<代码>document.Save(流)</代码<代码>pdf.SaveAs(路径)</代码节约
<代码>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();
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

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");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

本例展示了 API 的基本差异。Syncfusion PDF需要一个<代码>HtmlToPdfConverter</代码实例,调用 Convert() 返回一个<代码>PDF 文档</代码,然后手动创建一个 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();
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

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");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Syncfusion PDF 使用基于坐标的图形模型。 您创建了一个<代码>PDF 文档</代码文件,添加了一个 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();
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

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");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

合并 PDF 文件的效果对比鲜明。Syncfusion PDF要求为每个输入文档创建 FileStream 对象,将其加载为<代码>PDFLoadedDocument</代码,创建一个新的<代码>PDF 文档</代码,调用<代码>ImportPageRange()</代码并为每个源设置开始和结束索引、创建输出 FileStream 并关闭 6 个独立对象finalDocument, loadedDocument1, 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));
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

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);
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

对于网络开发人员来说,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();
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

IronPdf 会自动处理清理工作:

// IronPDF:自动翻译cleanup
pdf.SaveAs("Output.pdf");
// IronPDF:自动翻译cleanup
pdf.SaveAs("Output.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

功能对比

特征Syncfusion PDFIronPDF
购买否(仅限套件)
许可有社区限制的商业翻译简化商业
HTML 到 PDF需要 BlinkBinaries本地 Chromium
CSS3 支持有限的完整(flexbox、网格)
API 风格基于坐标的图形HTML/CSS 优先
流管理手册自动翻译
依赖关系多个软件包单一 NuGet
部署复杂性潜在的复杂性简单明了

迁移清单

迁移前

  • [列出代码库中所有使用Syncfusion PDF的内容
  • [ ] 记录许可成本和部署要求
  • [ ] 识别 PdfGrid, PdfGraphics, 和<代码>HtmlToPdfConverter</代码的用法。
  • [ ] 从 ironpdf.com 获取IronPDF许可证密钥

代码更新

  • [ ] 删除Syncfusion软件包(Syncfusion.Pdf.Net.Core, Syncfusion.HtmlToPdfConverter.Net.Windows, Syncfusion.Licensing)
  • [ ] 安装 IronPdf NuGet 软件包
  • [ ] 更新命名空间导入(使用 Syncfusion.Pdf;使用 IronPdf;)。
  • [ ] 将Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense()替换为IronPdf.License.LicenseKey = "..."
  • [将 HtmlToPdfConverter.Convert() 替换为 ChromePdfRenderer.RenderUrlAsPdf()RenderHtmlAsPdf()
  • [ ] 使用 ChromePdfRenderer.RenderHtmlAsPdf() 替换<代码>PDF 文档</代码+ Pages.Add() + Graphics.DrawString()
  • [ ] 将<代码>PDFLoadedDocument</代码替换为 PdfDocument.FromFile()
  • [ ] 将<代码>ImportPageRange()</代码替换为<代码>PdfDocument.Merge()</代码。
  • [ ] 将<代码>document.Save(流)</代码替换为<代码>pdf.SaveAs(路径)</代码。
  • [ ] 删除所有<代码>document.Close(true)</代码和 stream.Close() 调用
  • [ ] 将<代码>PdfGrid</代码替换为HTML <table>元素
  • [ ] 将<代码>PDF 标准字体</代码替换为 CSS font-family
  • [ ] 将 PdfBrushes 替换为 CSS color 属性

测试

  • [ ] PDF 输出的可视化比较
  • [ ] 验证 CSS 渲染的改进(flexbox、grid 现在可以工作了)
  • [ ] 测试文本提取
  • [ ] 测试合并和拆分
  • [ ] 性能比较

结论

在Syncfusion PDFFramework 和IronPDF之间做出选择,需要了解具体项目的需求以及每个库所带来的更广泛的工具集。Syncfusion提供了强大的一体化解决方案,作为大型套件的一部分,非常适合那些不仅需要 PDF 功能的企业。 另一方面,IronPdf 提供了一个重点突出、易于投入的选项,非常适合优先考虑直接 PDF 集成而无需额外组件的项目。

本次迁移的主要变化有 1.许可:仅限套装软件 → 独立购买 2.API 风格:基于坐标的图形 → HTML/CSS 优先 3.HTML 转换:<代码>HtmlToPdfConverter.Convert()</代码> → <代码>ChromePdfRenderer.RenderUrlAsPdf()</代码 4.文本渲染:使用字体/笔刷的 Graphics.DrawString() → 使用 CSS 的 HTML 元素 5.加载 PDF:<代码>PdfLoadedDocument</代码> + <代码>FileStream</代码> → <代码>PdfDocument.FromFile()</代码 6.合并 PDF 文件:<代码>ImportPageRange()</代码>与手动页面索引 → <代码>PdfDocument.Merge()</代码 7.保存document.Save(stream) + 流管理 → pdf.SaveAs(path) 8.清理:手动调用文档和流上的 Close() → 自动 9.表格:带有单元格操作的<代码>PdfGrid</代码→ 带有 CSS 的 HTML <table> 10.字体PdfStandardFont(PdfFontFamily.Helvetica, 12) → CSS font-family: Helvetica; font-size: 12pt;

总之,企业必须权衡成本、许可条款和具体项目要求,以确定哪个库最符合其目标。 如需了解用 C# 处理 PDF 的深入教程和丰富见解,公司可利用IronPDF的在线指导。

探索完整的IronPDF文档教程API参考,加快您的Syncfusion PDF迁移。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。