如何用 C# 从 GemBox PDF 迁移到 IronPDF
从GemBox PDF迁移到IronPDF可将您的 .NET PDF 工作流程从基于坐标的程序化文档构建转变为基于 HTML/CSS 的现代渲染。 本指南为专业的 .NET 开发人员提供了一个全面的、循序渐进的迁移路径,消除了段落限制,简化了文档创建。
为什么要从GemBox PDF迁移到 IronPDF.
GemBox PDF挑战
GemBox PDF 是一个功能强大的 .NET PDF 组件,但它有很大的局限性,影响了实际开发:
1.免费版 20 段限制:免费版限制您只能写 20 段,表格单元格也计入此限制。 一个简单的 10 行 5 列表格需要使用 50 个 "段落",这使得免费版本甚至无法用于基本的商业文档。
2.不支持 HTML 转 PDF:GemBox PDF需要通过编程方式构建文档。 您必须计算坐标并手动定位每个元素--没有简单的 "渲染此 HTML "功能。
3.基于坐标的布局:与 HTML/CSS 的布局自然流畅不同,GemBox PDF 要求您计算每个文本元素、图像和形状的精确 X/Y 位置。
4.功能集有限:与全面的 PDF 库相比,GemBox PDF 专注于基本操作——读取、写入、合并、拆分——而没有 HTML 渲染或现代 CSS 支持等高级功能。
5.仅限程序化:任何设计变更都需要代码变更。 想要调整行距? 重新计算坐标。 想要不同的字体大小?调整其下方所有 Y 位置。
6.表格单元格计数:段落限制不仅计算可见段落,还会计算表格单元格。这使得免费版对于包含表格的文档几乎毫无用处。
7.设计学习曲线:开发人员必须以坐标而非文档流程来思考,这使得像"添加段落"这样的简单任务变得异常复杂。
GemBox PDF与IronPDF对比
| 方面 | GemBox PDF | IronPDF |
|---|---|---|
| 免费版本限制 | 20 个段落(包括表格单元格) | 仅限水印,无内容限制 |
| HTML 转 PDF | 不支持 | 完整的 Chromium 引擎 |
| 排版方法 | 基于坐标的手动翻译 | HTML/CSS 流程布局 |
| 表格 | 计入段落限制 | 无限制,使用 HTML 表格 |
| 现代 CSS | 不适用 | Flexbox、网格、CSS3 动画 |
| JavaScript 支持 | 不适用 | 全面执行 JavaScript |
| 设计变更 | 重新计算坐标 | 编辑 HTML/CSS |
| 学习曲线 | PDF 坐标系 | HTML/CSS(熟悉网络) |
对于计划在 2025 年和 2026 年之前采用 .NET 10 和 C# 14 的团队来说,IronPDF 提供了一个面向未来的基础,利用熟悉的网络技术生成 PDF。
迁移复杂性评估
按功能估算的工作量
| 特征 | 迁移复杂性 |
|---|---|
| 加载/保存 PDF | 极低 |
| 合并 PDF | 极低 |
| 拆分 PDF | low |
| 文本提取 | 极低 |
| 添加文本 | 语言 |
| 表格 | low |
| 图片 | low |
| 水印 | low |
| 密码保护 | 语言 |
| 表格字段 | 语言 |
范式转换
GemBox PDF 迁移的最大变化是从基于坐标的布局转变为HTML/CSS 布局:
GemBox PDF: 在位置 (100, 700) 处绘制文本" GemBox PDF:"在位置 (100, 700) 处绘制文本"。
IronPDF: "使用 CSS 样式渲染 HTML
对于熟悉网络技术的开发人员来说,这种模式的转变通常比较容易,但需要以不同的思维方式来看待 PDF。
开始之前
前提条件
- .NET 版本:IronPDF支持 .NET Framework 4.6.2+ 和 .NET Core 2.0+ / .NET 5/6/7/8/9+ 2.许可证密钥:从ironpdf.com获取您的IronPDF许可证密钥。 3.备份:创建一个用于迁移工作的分支
- HTML/CSS知识:具备基本知识会有帮助,但并非必需。
识别所有GemBox PDF使用情况
# Find allGemBox PDFreferences
grep -r "GemBox\.Pdf\|PdfDocument\|PdfPage\|PdfFormattedText\|ComponentInfo\.SetLicense" --include="*.cs" .
# Find package references
grep -r "GemBox\.Pdf" --include="*.csproj" .
# Find allGemBox PDFreferences
grep -r "GemBox\.Pdf\|PdfDocument\|PdfPage\|PdfFormattedText\|ComponentInfo\.SetLicense" --include="*.cs" .
# Find package references
grep -r "GemBox\.Pdf" --include="*.csproj" .
NuGet 软件包变更
# Remove GemBox PDF
dotnet remove package GemBox.Pdf
# Install IronPDF
dotnet add package IronPdf
# Remove GemBox PDF
dotnet remove package GemBox.Pdf
# Install IronPDF
dotnet add package IronPdf
快速启动迁移
步骤 1:更新许可配置
之前(GemBox PDF):
// Must call before anyGemBox PDFoperations
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
// Or for professional:
ComponentInfo.SetLicense("YOUR-PROFESSIONAL-LICENSE");
// Must call before anyGemBox PDFoperations
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
// Or for professional:
ComponentInfo.SetLicense("YOUR-PROFESSIONAL-LICENSE");
' Must call before any GemBox PDF operations
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
' Or for professional:
ComponentInfo.SetLicense("YOUR-PROFESSIONAL-LICENSE")
After (IronPDF):
// Set once at application startup
IronPdf.License.LicenseKey = "YOUR-IRONPDF-LICENSE-KEY";
// Or in appsettings.json:
// { "IronPdf.License.LicenseKey": "YOUR-LICENSE-KEY" }
// Set once at application startup
IronPdf.License.LicenseKey = "YOUR-IRONPDF-LICENSE-KEY";
// Or in appsettings.json:
// { "IronPdf.License.LicenseKey": "YOUR-LICENSE-KEY" }
' Set once at application startup
IronPdf.License.LicenseKey = "YOUR-IRONPDF-LICENSE-KEY"
' Or in appsettings.json:
' { "IronPdf.License.LicenseKey": "YOUR-LICENSE-KEY" }
步骤 2:更新名称空间导入
// Before (GemBox PDF)
using GemBox.Pdf;
using GemBox.Pdf.Content;
// After (IronPDF)
using IronPdf;
using IronPdf.Editing;
// Before (GemBox PDF)
using GemBox.Pdf;
using GemBox.Pdf.Content;
// After (IronPDF)
using IronPdf;
using IronPdf.Editing;
Imports IronPdf
Imports IronPdf.Editing
步骤 3:基本转换模式
之前(GemBox PDF):
using GemBox.Pdf;
using GemBox.Pdf.Content;
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
using (var document = new PdfDocument())
{
var page = document.Pages.Add();
var formattedText = new PdfFormattedText()
{
Text = "Hello World",
FontSize = 24
};
page.Content.DrawText(formattedText, new PdfPoint(100, 700));
document.Save("output.pdf");
}
using GemBox.Pdf;
using GemBox.Pdf.Content;
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
using (var document = new PdfDocument())
{
var page = document.Pages.Add();
var formattedText = new PdfFormattedText()
{
Text = "Hello World",
FontSize = 24
};
page.Content.DrawText(formattedText, new PdfPoint(100, 700));
document.Save("output.pdf");
}
Imports GemBox.Pdf
Imports GemBox.Pdf.Content
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Using document As New PdfDocument()
Dim page = document.Pages.Add()
Dim formattedText As New PdfFormattedText() With {
.Text = "Hello World",
.FontSize = 24
}
page.Content.DrawText(formattedText, New PdfPoint(100, 700))
document.Save("output.pdf")
End Using
After (IronPDF):
using IronPdf;
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1 style='font-size:24px;'>Hello World</h1>");
pdf.SaveAs("output.pdf");
using IronPdf;
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1 style='font-size:24px;'>Hello World</h1>");
pdf.SaveAs("output.pdf");
Imports IronPdf
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1 style='font-size:24px;'>Hello World</h1>")
pdf.SaveAs("output.pdf")
关键差异:
- 无需进行坐标计算
- 用 HTML/CSS 代替编程布局
- 无段落限制
- 更简单、更易读的代码
完整的 API 参考
命名空间映射
| GemBox PDF | IronPDF |
|---|---|
GemBox.Pdf |
IronPdf |
GemBox.Pdf.Content |
IronPdf(内容为 HTML) |
GemBox.Pdf.Security |
IronPdf(安全设置) |
GemBox.Pdf.Forms |
IronPdf.Forms |
核心类映射
| GemBox PDF | IronPDF | 说明 |
|---|---|---|
PdfDocument |
PdfDocument |
主要 PDF 文档类别 |
PdfPage |
PdfDocument.Pages[i] |
页面表示 |
PdfContent |
不适用(使用 HTML) | 页面内容 |
PdfFormattedText |
不适用(使用 HTML) | 格式化文本 |
PdfPoint |
不适用(使用 CSS 定位) | 坐标定位 |
ComponentInfo.SetLicense() |
IronPdf.License.LicenseKey |
许可证管理 |
文档操作
| GemBox PDF | IronPDF |
|---|---|
new PdfDocument() |
new PdfDocument() |
PdfDocument.Load(path) |
PdfDocument.FromFile(path) |
PdfDocument.Load(stream) |
PdfDocument.FromStream(stream) |
document.Save(path) |
pdf.SaveAs(path) |
document.Save(stream) |
pdf.Stream 或 pdf.BinaryData |
页面操作
| GemBox PDF | IronPDF |
|---|---|
document.Pages.Add() |
通过 HTML 渲染创建 |
document.Pages.Count |
pdf.PageCount |
document.Pages[index] |
pdf.Pages[index] |
document.Pages.AddClone(pages) |
PdfDocument.Merge() |
文本和内容操作
| GemBox PDF | IronPDF |
|---|---|
new PdfFormattedText() |
HTML 字符串 |
formattedText.FontSize = 12 |
CSS font-size: 12pt |
formattedText.Font = ... |
CSS font-family: ... |
page.Content.DrawText(text, point) |
renderer.RenderHtmlAsPdf(html) |
page.Content.GetText() |
pdf.ExtractTextFromPage(i) |
代码迁移示例
示例 1:HTML 到 PDF 的转换
之前(GemBox PDF):
// NuGet: Install-Package GemBox.Pdf
using GemBox.Pdf;
using GemBox.Pdf.Content;
class Program
{
static void Main()
{
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
var document = PdfDocument.Load("input.html");
document.Save("output.pdf");
}
}
// NuGet: Install-Package GemBox.Pdf
using GemBox.Pdf;
using GemBox.Pdf.Content;
class Program
{
static void Main()
{
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
var document = PdfDocument.Load("input.html");
document.Save("output.pdf");
}
}
Imports GemBox.Pdf
Imports GemBox.Pdf.Content
Module Program
Sub Main()
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Dim document = PdfDocument.Load("input.html")
document.Save("output.pdf")
End Sub
End Module
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
pdf.SaveAs("output.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
pdf.SaveAs("output.pdf");
}
}
Imports IronPdf
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")
pdf.SaveAs("output.pdf")
End Sub
End Class
IronPDF 的 ChromePdfRenderer 使用现代 Chromium 引擎进行精确的 HTML/CSS/JavaScript 渲染。 与GemBox PDF对 HTML 的有限支持不同,IronPDF 可以呈现任何 HTML 内容,并完全支持 CSS3 和 JavaScript。 请参阅 HTML to PDF 文档,了解更多渲染选项。
示例 2:合并 PDF 文件
之前(GemBox PDF):
// NuGet: Install-Package GemBox.Pdf
using GemBox.Pdf;
using System.Linq;
class Program
{
static void Main()
{
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
using (var document = new PdfDocument())
{
var source1 = PdfDocument.Load("document1.pdf");
var source2 = PdfDocument.Load("document2.pdf");
document.Pages.AddClone(source1.Pages);
document.Pages.AddClone(source2.Pages);
document.Save("merged.pdf");
}
}
}
// NuGet: Install-Package GemBox.Pdf
using GemBox.Pdf;
using System.Linq;
class Program
{
static void Main()
{
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
using (var document = new PdfDocument())
{
var source1 = PdfDocument.Load("document1.pdf");
var source2 = PdfDocument.Load("document2.pdf");
document.Pages.AddClone(source1.Pages);
document.Pages.AddClone(source2.Pages);
document.Save("merged.pdf");
}
}
}
Imports GemBox.Pdf
Imports System.Linq
Module Program
Sub Main()
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Using document As New PdfDocument()
Dim source1 = PdfDocument.Load("document1.pdf")
Dim source2 = PdfDocument.Load("document2.pdf")
document.Pages.AddClone(source1.Pages)
document.Pages.AddClone(source2.Pages)
document.Save("merged.pdf")
End Using
End Sub
End Module
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
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;
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
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
IronPDF 的静态 Merge 方法简化了操作——无需创建空文档并单独克隆页面。 了解有关 合并和拆分 PDF 的更多信息。
示例 3:在 PDF 中添加文本
之前(GemBox PDF):
// NuGet: Install-Package GemBox.Pdf
using GemBox.Pdf;
using GemBox.Pdf.Content;
class Program
{
static void Main()
{
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
using (var document = new PdfDocument())
{
var page = document.Pages.Add();
var formattedText = new PdfFormattedText()
{
Text = "Hello World",
FontSize = 24
};
page.Content.DrawText(formattedText, new PdfPoint(100, 700));
document.Save("output.pdf");
}
}
}
// NuGet: Install-Package GemBox.Pdf
using GemBox.Pdf;
using GemBox.Pdf.Content;
class Program
{
static void Main()
{
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
using (var document = new PdfDocument())
{
var page = document.Pages.Add();
var formattedText = new PdfFormattedText()
{
Text = "Hello World",
FontSize = 24
};
page.Content.DrawText(formattedText, new PdfPoint(100, 700));
document.Save("output.pdf");
}
}
}
Imports GemBox.Pdf
Imports GemBox.Pdf.Content
Module Program
Sub Main()
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Using document As New PdfDocument()
Dim page = document.Pages.Add()
Dim formattedText As New PdfFormattedText() With {
.Text = "Hello World",
.FontSize = 24
}
page.Content.DrawText(formattedText, New PdfPoint(100, 700))
document.Save("output.pdf")
End Using
End Sub
End Module
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Editing;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<p>Original Content</p>");
var stamper = new TextStamper()
{
Text = "Hello World",
FontSize = 24,
HorizontalOffset = 100,
VerticalOffset = 700
};
pdf.ApplyStamp(stamper);
pdf.SaveAs("output.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Editing;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<p>Original Content</p>");
var stamper = new TextStamper()
{
Text = "Hello World",
FontSize = 24,
HorizontalOffset = 100,
VerticalOffset = 700
};
pdf.ApplyStamp(stamper);
pdf.SaveAs("output.pdf");
}
}
Imports IronPdf
Imports IronPdf.Editing
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<p>Original Content</p>")
Dim stamper = New TextStamper() With {
.Text = "Hello World",
.FontSize = 24,
.HorizontalOffset = 100,
.VerticalOffset = 700
}
pdf.ApplyStamp(stamper)
pdf.SaveAs("output.pdf")
End Sub
End Class
IronPDF 提供了 TextStamper 类,用于向现有 PDF 添加文本,该类提供精确的定位控制。 对于新文档,只需将文本包含在 HTML 模板中即可。 有关其他选项,请参阅冲压文档。
示例 4:创建表格(最大的改进!)
之前(GemBox PDF)--每个单元格均计入 20 段限制:
using GemBox.Pdf;
using GemBox.Pdf.Content;
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
using (var document = new PdfDocument())
{
var page = document.Pages.Add();
double y = 700;
double[] xPositions = { 50, 200, 300, 400 };
// Headers (4 paragraphs)
var headers = new[] { "Product", "Price", "Qty", "Total" };
for (int i = 0; i < headers.Length; i++)
{
var text = new PdfFormattedText { Text = headers[i], FontSize = 12 };
page.Content.DrawText(text, new PdfPoint(xPositions[i], y));
}
y -= 20;
// Data rows (4 paragraphs per row!)
// Can only add a few rows before hitting 20-paragraph limit!
document.Save("products.pdf");
}
using GemBox.Pdf;
using GemBox.Pdf.Content;
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
using (var document = new PdfDocument())
{
var page = document.Pages.Add();
double y = 700;
double[] xPositions = { 50, 200, 300, 400 };
// Headers (4 paragraphs)
var headers = new[] { "Product", "Price", "Qty", "Total" };
for (int i = 0; i < headers.Length; i++)
{
var text = new PdfFormattedText { Text = headers[i], FontSize = 12 };
page.Content.DrawText(text, new PdfPoint(xPositions[i], y));
}
y -= 20;
// Data rows (4 paragraphs per row!)
// Can only add a few rows before hitting 20-paragraph limit!
document.Save("products.pdf");
}
Imports GemBox.Pdf
Imports GemBox.Pdf.Content
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Using document As New PdfDocument()
Dim page = document.Pages.Add()
Dim y As Double = 700
Dim xPositions As Double() = {50, 200, 300, 400}
' Headers (4 paragraphs)
Dim headers = {"Product", "Price", "Qty", "Total"}
For i As Integer = 0 To headers.Length - 1
Dim text As New PdfFormattedText With {.Text = headers(i), .FontSize = 12}
page.Content.DrawText(text, New PdfPoint(xPositions(i), y))
Next
y -= 20
' Data rows (4 paragraphs per row!)
' Can only add a few rows before hitting 20-paragraph limit!
document.Save("products.pdf")
End Using
后 (IronPDF) - 无限制,适当的 HTML 表格:
using IronPdf;
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var html = @"
<html>
<head>
<style>
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #4CAF50; color: white; }
tr:nth-child(even) { background-color: #f2f2f2; }
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>Product</th>
<th>Price</th>
<th>Qty</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr><td>Widget A</td><td>$19.99</td><td>5</td><td>$99.95</td></tr>
<tr><td>Widget B</td><td>$29.99</td><td>3</td><td>$89.97</td></tr>
</tbody>
</table>
</body>
</html>";
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("products.pdf");
using IronPdf;
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var html = @"
<html>
<head>
<style>
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #4CAF50; color: white; }
tr:nth-child(even) { background-color: #f2f2f2; }
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>Product</th>
<th>Price</th>
<th>Qty</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr><td>Widget A</td><td>$19.99</td><td>5</td><td>$99.95</td></tr>
<tr><td>Widget B</td><td>$29.99</td><td>3</td><td>$89.97</td></tr>
</tbody>
</table>
</body>
</html>";
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("products.pdf");
Imports IronPdf
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim html As String = "
<html>
<head>
<style>
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #4CAF50; color: white; }
tr:nth-child(even) { background-color: #f2f2f2; }
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>Product</th>
<th>Price</th>
<th>Qty</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr><td>Widget A</td><td>$19.99</td><td>5</td><td>$99.95</td></tr>
<tr><td>Widget B</td><td>$29.99</td><td>3</td><td>$89.97</td></tr>
</tbody>
</table>
</body>
</html>"
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("products.pdf")
这是GemBox PDF迁移中最重要的改进。 在GemBox PDF的免费版本中无法实现的表格,在IronPDF中可以完美运行,并完全支持 CSS 样式。
关键迁移说明
坐标到 CSS 定位
如果您需要像素完美定位(类似于GemBox PDF的坐标系统),请使用 CSS 绝对定位:
<div style="position:absolute; left:50px; top:750px; font-size:24px;">
Text positioned at specific coordinates
</div>
<div style="position:absolute; left:50px; top:750px; font-size:24px;">
Text positioned at specific coordinates
</div>
页面索引
GemBox PDF 和IronPDF都使用 0 索引页面,因此这方面的迁移工作非常简单:
// GemBox PDF
var page = document.Pages[0];
// IronPDF
var page = pdf.Pages[0];
// GemBox PDF
var page = document.Pages[0];
// IronPDF
var page = pdf.Pages[0];
' GemBox PDF
Dim page = document.Pages(0)
' IronPDF
Dim page = pdf.Pages(0)
安全设置
// GemBox PDF
document.SaveOptions.SetPasswordEncryption(userPassword, ownerPassword);
// IronPDF
pdf.SecuritySettings.UserPassword = "userPassword";
pdf.SecuritySettings.OwnerPassword = "ownerPassword";
// GemBox PDF
document.SaveOptions.SetPasswordEncryption(userPassword, ownerPassword);
// IronPDF
pdf.SecuritySettings.UserPassword = "userPassword";
pdf.SecuritySettings.OwnerPassword = "ownerPassword";
' GemBox PDF
document.SaveOptions.SetPasswordEncryption(userPassword, ownerPassword)
' IronPDF
pdf.SecuritySettings.UserPassword = "userPassword"
pdf.SecuritySettings.OwnerPassword = "ownerPassword"
故障排除
问题 1:未找到 PdfFormattedText
问题:IronPDF中不存在 PdfFormattedText。
解决方案:使用 HTML 和 CSS 样式:
// GemBox PDF
var text = new PdfFormattedText { Text = "Hello", FontSize = 24 };
// IronPDF
var html = "<p style='font-size:24px;'>Hello</p>";
var pdf = renderer.RenderHtmlAsPdf(html);
// GemBox PDF
var text = new PdfFormattedText { Text = "Hello", FontSize = 24 };
// IronPDF
var html = "<p style='font-size:24px;'>Hello</p>";
var pdf = renderer.RenderHtmlAsPdf(html);
' GemBox PDF
Dim text As New PdfFormattedText With {.Text = "Hello", .FontSize = 24}
' IronPDF
Dim html As String = "<p style='font-size:24px;'>Hello</p>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
问题 2:未找到 DrawText 方法
问题: page.Content.DrawText() 不可用。
解决方案:通过 HTML 渲染创建内容或使用图章:
// For new documents - render HTML
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Content</h1>");
// For existing documents - use stampers
var stamper = new TextStamper() { Text = "Added Text" };
pdf.ApplyStamp(stamper);
// For new documents - render HTML
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Content</h1>");
// For existing documents - use stampers
var stamper = new TextStamper() { Text = "Added Text" };
pdf.ApplyStamp(stamper);
Imports System
' For new documents - render HTML
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Content</h1>")
' For existing documents - use stampers
Dim stamper As New TextStamper() With {.Text = "Added Text"}
pdf.ApplyStamp(stamper)
问题 3:文档加载差异
问题:未找到 PdfDocument.Load()。
解决方案:使用 PdfDocument.FromFile() 或 FromStream():
// GemBox PDF
var doc = PdfDocument.Load("input.pdf");
// IronPDF
var pdf = PdfDocument.FromFile("input.pdf");
// GemBox PDF
var doc = PdfDocument.Load("input.pdf");
// IronPDF
var pdf = PdfDocument.FromFile("input.pdf");
Imports GemBox.Pdf
Imports IronPdf
Dim doc = PdfDocument.Load("input.pdf")
Dim pdf = PdfDocument.FromFile("input.pdf")
问题 4:保存方法的差异
问题: document.Save() 方法签名不同。
解决方案:使用 SaveAs():
// GemBox PDF
document.Save("output.pdf");
// IronPDF
pdf.SaveAs("output.pdf");
// GemBox PDF
document.Save("output.pdf");
// IronPDF
pdf.SaveAs("output.pdf");
' GemBox PDF
document.Save("output.pdf")
' IronPDF
pdf.SaveAs("output.pdf")
迁移清单
迁移前
- 清点代码库中所有GemBox PDF的使用情况
- 识别需要转换为 HTML 的基于坐标的布局
- 评估当前影响代码的段落限制
- 获取IronPDF许可证密钥
- 在版本控制系统中创建迁移分支
代码迁移
- 移除GemBox PDFNuGet 包:
dotnet remove package GemBox.Pdf - 安装 IronPdf NuGet 包:
dotnet add package IronPdf - 更新命名空间导入
- 将
ComponentInfo.SetLicense()替换为IronPdf.License.LicenseKey - 将
PdfDocument.Load()转换为PdfDocument.FromFile() - 将
document.Save()转换为pdf.SaveAs() - 将基于坐标的文本替换为 HTML 内容
- 将
PdfFormattedText转换为带有 CSS 样式的 HTML - 更新合并操作以使用
PdfDocument.Merge()
测试
- 核实所有文档是否正确生成
- 验证文档外观是否符合预期
- 测试表生成(之前受 20 段规则限制)
- 验证文本提取功能是否正常
- 测试合并和拆分操作
- 验证安全/加密功能
后迁移
- 移除GemBox PDF许可证密钥
- 更新文档
- 培训团队使用 HTML/CSS 方法制作 PDF 文件
- 畅享无段落限制的无限内容!

