如何用 C# 从 XFINIUM.PDF 迁移到 IronPDF
XFINIUM.PDF是一个跨平台商业PDF库,由O2 Solutions开发,提供工具来使用C#程序化地创建和编辑PDF。 它提供两个版本 - 生成器(PDF生成和编辑)和查看器(生成器功能加渲染/显示)。 该库在NuGet上以Xfinium.Pdf.NetCore分发。 其基于坐标的图形编程模型可能会给构建文档密集型应用的团队造成麻烦,因为大部分布局工作涉及到使用像素坐标手动定位元素。
这本指南提供了从XFINIUM.PDF迁移到IronPDF的路径,包含逐步的说明、代码比较和实际的例子,供评估此转换的.NET开发人员使用。
为什么要从 XFINIUM.PDF 迁移
XFINIUM.PDF是一个低级别的PDF库,依赖于基于坐标的图形编程,这意味着开发人员直接将大多数元素定位在页面上。 开发团队考虑迁移的常见原因包括:
没有原生的HTML引擎:XFINIUM.PDF没有内置的HTML到PDF转换器。 供应商提供了一个支持有限标签集(p, font, b, i, u, ul, li)的示例XHTML遍历器,并且不解析CSS或执行JavaScript,这可能无法满足需要完整HTML到PDF功能的项目。
基于坐标的API:对于大多数元素放置,需要使用像DrawString("text", font, brush, 50, 100)这样的像素坐标进行手动定位。
手动字体管理:必须使用类似PdfBrush的类明确创建和管理字体对象。
不支持 CSS 样式:不支持现代网页样式。 颜色、字体和布局通过编程方法调用来处理。
不使用 JavaScript 渲染:仅显示静态内容。 XFINIUM.PDF无法渲染动态网页内容或执行JavaScript。
复杂的文本布局:对于简单的单行文本之外的任何内容,通常需要手动测量和换行计算。
小型社区:相比主流解决方案,社区提供的示例和教程更少,可能会使新用户更难入门。
核心问题:图形 API 与 HTML 的对比
XFINIUM.PDF 迫使您像图形程序员而非文档设计师那样思考:
// XFINIUM.PDF: Position every element manually
page.Graphics.DrawString("Invoice", titleFont, titleBrush, 50, 50);
page.Graphics.DrawString("Customer:", labelFont, brush, 50, 80);
page.Graphics.DrawString(customer.Name, valueFont, brush, 120, 80);
// ... hundreds of lines for a simple document
// XFINIUM.PDF: Position every element manually
page.Graphics.DrawString("Invoice", titleFont, titleBrush, 50, 50);
page.Graphics.DrawString("Customer:", labelFont, brush, 50, 80);
page.Graphics.DrawString(customer.Name, valueFont, brush, 120, 80);
// ... hundreds of lines for a simple document
' XFINIUM.PDF: Position every element manually
page.Graphics.DrawString("Invoice", titleFont, titleBrush, 50, 50)
page.Graphics.DrawString("Customer:", labelFont, brush, 50, 80)
page.Graphics.DrawString(customer.Name, valueFont, brush, 120, 80)
' ... hundreds of lines for a simple document
IronPDF 使用熟悉的 HTML/CSS:
// IronPDF: Declarative HTML
var html = @"<h1>Invoice</h1><p><b>Customer:</b> " + customer.Name + "</p>";
var pdf = renderer.RenderHtmlAsPdf(html);
// IronPDF: Declarative HTML
var html = @"<h1>Invoice</h1><p><b>Customer:</b> " + customer.Name + "</p>";
var pdf = renderer.RenderHtmlAsPdf(html);
' IronPDF: Declarative HTML
Dim html As String = "<h1>Invoice</h1><p><b>Customer:</b> " & customer.Name & "</p>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
IronPDF与 XFINIUM.PDF:功能比较
了解架构差异有助于技术决策者评估迁移投资:
| 特征 | XFINIUM.PDF | IronPDF |
|---|---|---|
| HTML 到 PDF | 没有原生引擎;仅示例XHTML转换器(标签有限,无CSS/JS) | 使用Chromium进行完整的HTML到PDF转换(CSS3 + JavaScript) |
| 社区与支持 | 社区规模较小,可用在线资源较少 | 拥有大量文档和教程的大型社区 |
| 许可 | 基于开发人员的商业许可 | 商业翻译 |
| 跨平台支持 | 强大的跨平台能力 | 同时支持跨平台操作 |
| CSS支持 | 否 | 完整的 CSS3 |
| JavaScript语言 | 否 | 完整的 ES2024 |
| Flexbox/网格 | 否 | 是 |
| 自动排版 | 否 | 是 |
| 自动分页 | 否 | 是 |
| 手册定位 | 要求 | 可选项(CSS 定位) |
| 学习曲线 | 高(坐标系) | 低级(HTML/CSS) |
| 代码准确性 | 极高 | low |
快速入门:XFINIUM.PDF 向IronPDF迁移
迁移工作可以通过以下基本步骤立即开始。
步骤 1:替换 NuGet 软件包
删除XFINIUM.PDF(实际的NuGet ID是特定于平台的):
# Remove XFINIUM.PDF
dotnet remove package Xfinium.Pdf.NetStandard
# or: dotnet remove package Xfinium.Pdf.NetCore
# Remove XFINIUM.PDF
dotnet remove package Xfinium.Pdf.NetStandard
# or: dotnet remove package Xfinium.Pdf.NetCore
安装 IronPDF:
# Install IronPDF
dotnet add package IronPdf
# Install IronPDF
dotnet add package IronPdf
步骤 2:更新命名空间
将XFINIUM.PDF命名空间替换为IronPDF命名空间:
// Before (XFINIUM.PDF)
using Xfinium.Pdf;
using Xfinium.Pdf.Graphics;
using Xfinium.Pdf.Content;
using Xfinium.Pdf.FlowDocument;
// After (IronPDF)
using IronPdf;
// Before (XFINIUM.PDF)
using Xfinium.Pdf;
using Xfinium.Pdf.Graphics;
using Xfinium.Pdf.Content;
using Xfinium.Pdf.FlowDocument;
// After (IronPDF)
using IronPdf;
Imports IronPdf
步骤 3:初始化许可证
在应用程序启动时添加许可证初始化:
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
代码迁移示例
将HTML转换为PDF
最基本的操作揭示了这些 .NET PDF 库之间的复杂性差异。
XFINIUM.PDF 方法:
// NuGet: Install-Package Xfinium.Pdf.NetStandard (or .NetCore)
// XFINIUM.PDF has no native HTML-to-PDF engine. Flow content accepts
// styled text, headings, tables and images - not HTML. The vendor sample
// converter parses limited XHTML (p, font, b, i, u, ul, li) into
// PdfFormattedContent; full CSS/JS rendering is not supported.
using Xfinium.Pdf;
using Xfinium.Pdf.FlowDocument;
using Xfinium.Pdf.Graphics;
using System.IO;
class Program
{
static void Main()
{
PdfFlowDocument flowDocument = new PdfFlowDocument();
flowDocument.AddContent(new PdfFlowHeadingContent(
"Hello World",
new PdfStandardFont(PdfStandardFontFace.HelveticaBold, 18)));
flowDocument.AddContent(new PdfFlowTextContent(
"This is a PDF generated from text content (not HTML).",
new PdfStandardFont(PdfStandardFontFace.Helvetica, 12)));
using (FileStream fs = new FileStream("output.pdf", FileMode.Create))
{
flowDocument.Save(fs);
}
}
}
// NuGet: Install-Package Xfinium.Pdf.NetStandard (or .NetCore)
// XFINIUM.PDF has no native HTML-to-PDF engine. Flow content accepts
// styled text, headings, tables and images - not HTML. The vendor sample
// converter parses limited XHTML (p, font, b, i, u, ul, li) into
// PdfFormattedContent; full CSS/JS rendering is not supported.
using Xfinium.Pdf;
using Xfinium.Pdf.FlowDocument;
using Xfinium.Pdf.Graphics;
using System.IO;
class Program
{
static void Main()
{
PdfFlowDocument flowDocument = new PdfFlowDocument();
flowDocument.AddContent(new PdfFlowHeadingContent(
"Hello World",
new PdfStandardFont(PdfStandardFontFace.HelveticaBold, 18)));
flowDocument.AddContent(new PdfFlowTextContent(
"This is a PDF generated from text content (not HTML).",
new PdfStandardFont(PdfStandardFontFace.Helvetica, 12)));
using (FileStream fs = new FileStream("output.pdf", FileMode.Create))
{
flowDocument.Save(fs);
}
}
}
Imports Xfinium.Pdf
Imports Xfinium.Pdf.FlowDocument
Imports Xfinium.Pdf.Graphics
Imports System.IO
Module Program
Sub Main()
Dim flowDocument As New PdfFlowDocument()
flowDocument.AddContent(New PdfFlowHeadingContent(
"Hello World",
New PdfStandardFont(PdfStandardFontFace.HelveticaBold, 18)))
flowDocument.AddContent(New PdfFlowTextContent(
"This is a PDF generated from text content (not HTML).",
New PdfStandardFont(PdfStandardFontFace.Helvetica, 12)))
Using fs As New FileStream("output.pdf", FileMode.Create)
flowDocument.Save(fs)
End Using
End Sub
End Module
IronPDF 方法:
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string html = "<html><body><h1>Hello World</h1><p>This is a PDF from HTML.</p></body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string html = "<html><body><h1>Hello World</h1><p>This is a PDF from HTML.</p></body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
}
}
Imports IronPdf
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim html As String = "<html><body><h1>Hello World</h1><p>This is a PDF from HTML.</p></body></html>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("output.pdf")
End Sub
End Class
XFINIUM.PDF需要创建一个FileStream保存。 流文档API接受类型化内容对象,而不是HTML标记。IronPDF将此简化为三行:创建一个渲染器,渲染HTML并保存。
有关 HTML 转 PDF 的高级应用场景,请参阅 HTML 转 PDF 指南。
合并多个 PDF 文件
PDF 合并清楚地展示了 API 复杂性的差异。
XFINIUM.PDF 方法:
// NuGet: Install-Package Xfinium.Pdf.NetStandard (or .NetCore)
using Xfinium.Pdf;
using System.IO;
class Program
{
static void Main()
{
PdfFixedDocument output = new PdfFixedDocument();
FileStream file1 = File.OpenRead("document1.pdf");
PdfFixedDocument pdf1 = new PdfFixedDocument(file1);
FileStream file2 = File.OpenRead("document2.pdf");
PdfFixedDocument pdf2 = new PdfFixedDocument(file2);
for (int i = 0; i < pdf1.Pages.Count; i++)
{
output.Pages.Add(pdf1.Pages[i]);
}
for (int i = 0; i < pdf2.Pages.Count; i++)
{
output.Pages.Add(pdf2.Pages[i]);
}
output.Save("merged.pdf");
file1.Close();
file2.Close();
}
}
// NuGet: Install-Package Xfinium.Pdf.NetStandard (or .NetCore)
using Xfinium.Pdf;
using System.IO;
class Program
{
static void Main()
{
PdfFixedDocument output = new PdfFixedDocument();
FileStream file1 = File.OpenRead("document1.pdf");
PdfFixedDocument pdf1 = new PdfFixedDocument(file1);
FileStream file2 = File.OpenRead("document2.pdf");
PdfFixedDocument pdf2 = new PdfFixedDocument(file2);
for (int i = 0; i < pdf1.Pages.Count; i++)
{
output.Pages.Add(pdf1.Pages[i]);
}
for (int i = 0; i < pdf2.Pages.Count; i++)
{
output.Pages.Add(pdf2.Pages[i]);
}
output.Save("merged.pdf");
file1.Close();
file2.Close();
}
}
Imports Xfinium.Pdf
Imports System.IO
Module Program
Sub Main()
Dim output As New PdfFixedDocument()
Dim file1 As FileStream = File.OpenRead("document1.pdf")
Dim pdf1 As New PdfFixedDocument(file1)
Dim file2 As FileStream = File.OpenRead("document2.pdf")
Dim pdf2 As New PdfFixedDocument(file2)
For i As Integer = 0 To pdf1.Pages.Count - 1
output.Pages.Add(pdf1.Pages(i))
Next
For i As Integer = 0 To pdf2.Pages.Count - 1
output.Pages.Add(pdf2.Pages(i))
Next
output.Save("merged.pdf")
file1.Close()
file2.Close()
End Sub
End Module
IronPDF 方法:
// NuGet: Install-Package IronPdf
using IronPdf;
using System.Collections.Generic;
class Program
{
static void Main()
{
var pdf1 = PdfDocument.FromFile("document1.pdf");
var pdf2 = PdfDocument.FromFile("document2.pdf");
var merged = PdfDocument.Merge(pdf1, pdf2);
merged.SaveAs("merged.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System.Collections.Generic;
class Program
{
static void Main()
{
var pdf1 = PdfDocument.FromFile("document1.pdf");
var pdf2 = PdfDocument.FromFile("document2.pdf");
var merged = PdfDocument.Merge(pdf1, pdf2);
merged.SaveAs("merged.pdf");
}
}
Imports IronPdf
Imports System.Collections.Generic
Class Program
Shared Sub Main()
Dim pdf1 = PdfDocument.FromFile("document1.pdf")
Dim pdf2 = PdfDocument.FromFile("document2.pdf")
Dim merged = PdfDocument.Merge(pdf1, pdf2)
merged.SaveAs("merged.pdf")
End Sub
End Class
XFINIUM.PDF 需要创建输出文档、打开文件流、加载每个文档、手动迭代页面并逐一添加、保存,然后关闭文件流。 IronPDF提供了一个单一的PdfDocument.Merge()方法,内部处理所有复杂性。
探索 IronPDF合并文档,了解更多合并选项。
使用文本和图像创建 PDF 文件
混合内容的文档显示了基本范式的差异。
XFINIUM.PDF 方法:
// NuGet: Install-Package Xfinium.Pdf.NetStandard (or .NetCore)
using Xfinium.Pdf;
using Xfinium.Pdf.Graphics;
using System.IO;
class Program
{
static void Main()
{
PdfFixedDocument document = new PdfFixedDocument();
PdfPage page = document.Pages.Add();
PdfStandardFont font = new PdfStandardFont(PdfStandardFontFace.Helvetica, 24);
PdfBrush brush = new PdfBrush(new PdfRgbColor(0, 0, 0));
page.Graphics.DrawString("Sample PDF Document", font, brush, 50, 50);
using (FileStream imageStream = File.OpenRead("image.jpg"))
{
PdfJpegImage image = new PdfJpegImage(imageStream);
page.Graphics.DrawImage(image, 50, 100, 200, 150);
}
document.Save("output.pdf");
}
}
// NuGet: Install-Package Xfinium.Pdf.NetStandard (or .NetCore)
using Xfinium.Pdf;
using Xfinium.Pdf.Graphics;
using System.IO;
class Program
{
static void Main()
{
PdfFixedDocument document = new PdfFixedDocument();
PdfPage page = document.Pages.Add();
PdfStandardFont font = new PdfStandardFont(PdfStandardFontFace.Helvetica, 24);
PdfBrush brush = new PdfBrush(new PdfRgbColor(0, 0, 0));
page.Graphics.DrawString("Sample PDF Document", font, brush, 50, 50);
using (FileStream imageStream = File.OpenRead("image.jpg"))
{
PdfJpegImage image = new PdfJpegImage(imageStream);
page.Graphics.DrawImage(image, 50, 100, 200, 150);
}
document.Save("output.pdf");
}
}
Imports Xfinium.Pdf
Imports Xfinium.Pdf.Graphics
Imports System.IO
Class Program
Shared Sub Main()
Dim document As New PdfFixedDocument()
Dim page As PdfPage = document.Pages.Add()
Dim font As New PdfStandardFont(PdfStandardFontFace.Helvetica, 24)
Dim brush As New PdfBrush(New PdfRgbColor(0, 0, 0))
page.Graphics.DrawString("Sample PDF Document", font, brush, 50, 50)
Using imageStream As FileStream = File.OpenRead("image.jpg")
Dim image As New PdfJpegImage(imageStream)
page.Graphics.DrawImage(image, 50, 100, 200, 150)
End Using
document.Save("output.pdf")
End Sub
End Class
IronPDF 方法:
// NuGet: Install-Package IronPdf
using IronPdf;
using System.IO;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string imageBase64 = Convert.ToBase64String(File.ReadAllBytes("image.jpg"));
string html = $@"
<html>
<body>
<h1>Sample PDF Document</h1>
<img src='data:image/jpeg;base64,{imageBase64}' width='200' height='150' />
</body>
</html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System.IO;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string imageBase64 = Convert.ToBase64String(File.ReadAllBytes("image.jpg"));
string html = $@"
<html>
<body>
<h1>Sample PDF Document</h1>
<img src='data:image/jpeg;base64,{imageBase64}' width='200' height='150' />
</body>
</html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
}
}
Imports IronPdf
Imports System.IO
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim imageBase64 As String = Convert.ToBase64String(File.ReadAllBytes("image.jpg"))
Dim html As String = $"
<html>
<body>
<h1>Sample PDF Document</h1>
<img src='data:image/jpeg;base64,{imageBase64}' width='200' height='150' />
</body>
</html>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("output.pdf")
End Sub
End Class
XFINIUM.PDF需要创建一个文档,添加一个页面,创建字体和画刷对象,在特定坐标位置绘制文本,打开一个图像流,创建一个PdfJpegImage,在具有尺寸的坐标位置绘制图像,关闭流并保存。IronPDF使用标准 HTML,内嵌 base64 图像--与网络开发人员日常使用的方法相同。
XFINIUM.PDF API 到IronPDF映射参考
这种映射通过显示直接的 API 对应关系来加速迁移:
| XFINIUM.PDF | IronPDF |
|---|---|
PdfFixedDocument |
ChromePdfRenderer |
PdfPage |
自动翻译 |
page.Graphics.DrawString() |
HTML 文本元素 |
page.Graphics.DrawImage() |
<img> 标签 |
page.Graphics.DrawLine() |
CSS border 或 <hr> |
page.Graphics.DrawRectangle() |
CSS border 在 |
PdfStandardFont |
CSS font-family |
PdfRgbColor |
CSS color |
PdfBrush |
CSS 属性 |
PdfJpegImage |
含base64的标签 |
document.Save(stream) |
pdf.SaveAs() 或 pdf.BinaryData |
PdfFlowDocument |
RenderHtmlAsPdf() |
| 供应商样本XHTML转换器 | RenderHtmlAsPdf() |
常见迁移问题和解决方案
问题 1:基于坐标的布局
XFINIUM.PDF:所有内容都需要精确的 X、Y 坐标和手动定位。
解决方案: 使用 HTML/CSS 流程布局。 需要绝对定位时,请使用 CSS:
.positioned-element {
position: absolute;
top: 100px;
left: 50px;
}
问题 2:字体对象管理
XFINIUM.PDF: 为每种字体创建PdfUnicodeTrueTypeFont对象。
解决方案:使用 CSS 字体-family-字体自动处理:
<style>
body { font-family: Arial, sans-serif; }
h1 { font-family: 'Times New Roman', serif; font-size: 24px; }
</style>
<style>
body { font-family: Arial, sans-serif; }
h1 { font-family: 'Times New Roman', serif; font-size: 24px; }
</style>
问题 3:颜色处理
XFINIUM.PDF: 为颜色创建PdfBrush对象。
解决方案:使用标准 CSS 颜色:
.header { color: navy; background-color: #f5f5f5; }
.warning { color: rgb(255, 0, 0); }
.info { color: rgba(0, 0, 255, 0.8); }
问题 4:手动分页
XFINIUM.PDF: 跟踪 Y 位置并在内容溢出时手动创建新页面。
解决方案:IronPDF可处理自动分页。 如需明确控制,请使用 CSS:
.section { page-break-after: always; }
.keep-together { page-break-inside: avoid; }
问题 5:图像加载
XFINIUM.PDF: 打开文件流,创建PdfJpegImage对象,在坐标处绘制,关闭流。
解决方案: 使用HTML 标签与文件路径或base64数据:
<img src="image.jpg" width="200" height="150" />
<img src="data:image/jpeg;base64,..." />
<img src="image.jpg" width="200" height="150" />
<img src="data:image/jpeg;base64,..." />
XFINIUM.PDF 迁移清单
迁移前任务
审核您的代码库,确定所有 XFINIUM.PDF 的使用情况:
grep -r "using Xfinium.Pdf" --include="*.cs" .
grep -r "Graphics.DrawString\|Graphics.DrawImage\|Graphics.DrawLine" --include="*.cs" .
grep -r "using Xfinium.Pdf" --include="*.cs" .
grep -r "Graphics.DrawString\|Graphics.DrawImage\|Graphics.DrawLine" --include="*.cs" .
记录基于坐标的布局并注明所有 X、Y 定位值。 识别字体和颜色对象(PdfStandardFont, PdfRgbColor, PdfBrush)。 使用PdfFixedDocument.Pages.Add()映射合并的PDF工作流。
代码更新任务
1.删除 Xfinium.Pdf NuGet 软件包
- 安装IronPDF NuGet包
- 将命名空间导入从
IronPdf - 将
DrawString()调用转换为HTML文本元素 - 将
DrawImage()调用转换为HTML标签 - 将
DrawLine()转换为CSS边框 - 用CSS
PdfStandardFont - 用CSS颜色替换
PdfBrush - 用
PdfDocument.Merge()替换页面循环合并 10.在启动时添加IronPDF许可证初始化功能
迁移后测试
迁移后,验证这些方面:
- 比较视觉输出,确保外观符合预期
- 使用新的 HTML/CSS 方法验证文本渲染
- 使用 CSS 检查图像定位
- 测试分页符是否按预期出现
- 验证 PDF 安全设置是否正确应用
- 在所有目标平台上进行测试
迁移到IronPDF的主要优势
从 XFINIUM.PDF 迁移到IronPDF有几个关键优势:
基于 HTML 的内容创建: Web 开发人员可以利用现有的 HTML 和 CSS 技能。 无需学习基于坐标的绘图 API 或管理字体和画笔对象。
自动布局:文本换行、分页和流式布局自动完成。 无需手动计算元素位置或分页符。
现代 CSS 支持:完全支持 CSS3,包括 Flexbox 和 Grid 布局。 响应式设计可直接翻译成 PDF。
简化的PDF操作: 使用类似PdfDocument.Merge()的单方法调用替换复杂的页面迭代循环。
积极开发:IronPDF的定期更新与当前.NET版本和C#语言发布保持同步。
丰富的文档:与 XFINIUM.PDF 较小的生态系统相比,拥有庞大的社区、全面的文档、教程和支持资源。

