如何在 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 | 低 |
| 文字擷取 | 非常低 |
| 新增文字 | 語言 |
| 表格 | 低 |
| 圖片 | 低 |
| 水印 | 低 |
| 密碼保護 | 語言 |
| 表格欄位 | 語言 |
範式轉移
GemBox PDF 遷移的最大變化是從基於座標的佈局轉變為HTML/CSS 佈局:
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 文件
- 暢享無段落限制的無限內容!

