如何使用 C# 在 PDF 中新增頁碼
將GemBox.Pdf遷移到IronPDF會將您的.NET PDF工作流程從基於坐標的程式化文件構建轉換為現代HTML/CSS渲染。 本指南提供了一個全方位的逐步遷移路徑,移除2頁免費模式限制,並簡化專業.NET開發者的文件建立。
為何從GemBox.Pdf遷移到IronPDF
GemBox.Pdf的挑戰
GemBox.Pdf是一個功能強大的.NET PDF組件,但在實際開發中有一些限制需要考量:
-
2頁免費模式限制:免費版本在載入或保存超過2頁的PDF時會拋出
FreeLimitReachedException,因此超過單頁收據或兩頁發票的任何內容都需要付費許可。 (Source: gemboxsoftware.com/pdf/free-version.) -
不支持HTML到PDF轉換:GemBox.Pdf無法渲染HTML——
PdfDocument.Load只能打開現有的PDF文件。 要將HTML轉換為PDF,您必須購買單獨的GemBox.Document產品(不同的SKU,不同的授權)。 -
基於坐標的佈局:GemBox.Pdf是一個低層次的PDF內容流API。 要放置文字,您需要計算PDF使用者空間單位中的X/Y並調用
page.Content.DrawText(formattedText, new PdfPoint(x, y))。 沒有流佈局。 -
Office到PDF需要其他SKU:Word→PDF需要GemBox.Document,Excel→PDF需要GemBox.Spreadsheet,每個都需單獨授權; GemBox.Bundle涵蓋了所有這些,但成本更高。
-
僅限程式化設計:每次設計變更都需要程式碼修改。 調整間距? 重新計算坐標。 改變字體大小?調整其下方的Y位置。
-
商業授權定價:單人開發者授權為890美元(續訂534美元),小團隊(10位開發者)為4,450美元,大團隊(50位開發者)為13,350美元。(來源:gemboxsoftware.com/pdf/pricing。)
- 設計學習曲線:開發者必須以坐標思考,而非文件流,使得簡單任務如"新增段落"變得相當複雜。
GemBox.Pdf與IronPDF比較
| 方面 | GemBox.Pdf | IronPDF |
|---|---|---|
| 免費版本限制 | 最多2頁(FreeLimitReachedException) | 僅水印,無頁面限制 |
| HTML到PDF | 不支持(需要GemBox.Document) | 完整的Chromium引擎 |
| Word/Excel → PDF | 獨立SKU(GemBox.Document / GemBox.Spreadsheet) | 通過HTML管道渲染 |
| 佈局方式 | 基於坐標,手動 | HTML/CSS流佈局 |
| 現代CSS | 不適用 | Flexbox, Grid, CSS3動畫 |
| JavaScript支持 | 不適用 | 完整的JavaScript執行 |
| 設計變更 | 重新計算坐標 | 編輯HTML/CSS |
| 學習曲線 | PDF坐標系統 | HTML/CSS(熟悉的網頁) |
IronPDF利用熟悉的網路技術生成現代.NET上的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+
- 授權金鑰:從ironpdf.com取得您的IronPDF授權金鑰
- 備份:建立一個遷移工作的分支
- HTML/CSS知識:基本了解有用但不是必需的
識別所有GemBox.Pdf使用
# Find all GemBox.Pdf references
grep -r "GemBox\.Pdf\|PdfDocument\|PdfPage\|PdfFormattedText\|ComponentInfo\.SetLicense" --include="*.cs" .
# Find package references
grep -r "GemBox\.Pdf" --include="*.csproj" .
# Find all GemBox.Pdf references
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 any GemBox.Pdf operations
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");
' Must call before any GemBox.Pdf operations
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
' Or for professional:
ComponentInfo.SetLicense("YOUR-PROFESSIONAL-LICENSE")
之後(IronPDF):
// Set once at application startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
// Or in appsettings.json:
// { "IronPdf.License.LicenseKey": "YOUR-LICENSE-KEY" }
// Set once at application startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
// Or in appsettings.json:
// { "IronPdf.License.LicenseKey": "YOUR-LICENSE-KEY" }
Imports IronPdf
' Set once at application startup
IronPdf.License.LicenseKey = "YOUR-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();
// PdfFormattedText has no Text property; use Append/AppendLine.
var formattedText = new PdfFormattedText();
formattedText.FontSize = 24;
formattedText.Append("Hello World");
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();
// PdfFormattedText has no Text property; use Append/AppendLine.
var formattedText = new PdfFormattedText();
formattedText.FontSize = 24;
formattedText.Append("Hello World");
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()
' PdfFormattedText has no Text property; use Append/AppendLine.
Dim formattedText As New PdfFormattedText()
formattedText.FontSize = 24
formattedText.Append("Hello World")
page.Content.DrawText(formattedText, New PdfPoint(100, 700))
document.Save("output.pdf")
End Using
之後(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代替程式化佈局
- 沒有2頁免費模式上限
- 更簡潔、更可讀的程式碼
完整API參考
命名空間映射
| GemBox.Pdf | IronPDF |
|---|---|
GemBox.Pdf |
IronPdf |
GemBox.Pdf.Content |
IronPdf (內容是HTML) |
GemBox.Pdf.Security |
IronPdf (SecuritySettings) |
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.BinaryData (返回byte[]) |
頁面操作
| 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.Append(text) |
包含在HTML中 |
formattedText.AppendLine(text) |
包含在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)—GemBox.Pdf不支持; 需要單獨的GemBox.Document SKU:
// NuGet: Install-Package GemBox.Document
// NOTE: GemBox.Pdf does NOT support HTML-to-PDF. PdfDocument.Load only opens
// existing PDF files. To convert HTML to PDF you must use the separate
// GemBox.Document product (different SKU, different license).
using GemBox.Document;
class Program
{
static void Main()
{
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
// GemBox.Document loads HTML and saves as PDF.
var document = DocumentModel.Load("input.html");
document.Save("output.pdf");
}
}
// NuGet: Install-Package GemBox.Document
// NOTE: GemBox.Pdf does NOT support HTML-to-PDF. PdfDocument.Load only opens
// existing PDF files. To convert HTML to PDF you must use the separate
// GemBox.Document product (different SKU, different license).
using GemBox.Document;
class Program
{
static void Main()
{
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
// GemBox.Document loads HTML and saves as PDF.
var document = DocumentModel.Load("input.html");
document.Save("output.pdf");
}
}
Imports GemBox.Document
Module Program
Sub Main()
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
' GemBox.Document loads HTML and saves as PDF.
Dim document = DocumentModel.Load("input.html")
document.Save("output.pdf")
End Sub
End Module
之後(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渲染,因此一個NuGet包直接涵蓋HTML到PDF—無需單獨的SKU。 請參閱HTML到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
之後(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();
// PdfFormattedText has no Text property; use Append/AppendLine.
var formattedText = new PdfFormattedText();
formattedText.FontSize = 24;
formattedText.Append("Hello World");
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();
// PdfFormattedText has no Text property; use Append/AppendLine.
var formattedText = new PdfFormattedText();
formattedText.FontSize = 24;
formattedText.Append("Hello World");
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()
' PdfFormattedText has no Text property; use Append/AppendLine.
Dim formattedText As New PdfFormattedText()
formattedText.FontSize = 24
formattedText.Append("Hello World")
page.Content.DrawText(formattedText, New PdfPoint(100, 700))
document.Save("output.pdf")
End Using
End Sub
End Module
之後(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
對於向現有PDF新增文字,IronPDF提供了TextStamper類,提供精確的定位控制。 對於新文件,只需在HTML模板中包含文字即可。 請參閱加蓋文件以獲取其他選項。
例4:建立表格(最大改進!)
之前(GemBox.Pdf)—基於坐標的佈局,免費模式下僅限於2頁:
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
var headers = new[] { "Product", "Price", "Qty", "Total" };
for (int i = 0; i < headers.Length; i++)
{
var text = new PdfFormattedText();
text.FontSize = 12;
text.Append(headers[i]);
page.Content.DrawText(text, new PdfPoint(xPositions[i], y));
}
y -= 20;
// Data rows — each row requires manual Y advancement,
// and free mode caps the saved file at 2 pages total.
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
var headers = new[] { "Product", "Price", "Qty", "Total" };
for (int i = 0; i < headers.Length; i++)
{
var text = new PdfFormattedText();
text.FontSize = 12;
text.Append(headers[i]);
page.Content.DrawText(text, new PdfPoint(xPositions[i], y));
}
y -= 20;
// Data rows — each row requires manual Y advancement,
// and free mode caps the saved file at 2 pages total.
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
Dim headers = New String() {"Product", "Price", "Qty", "Total"}
For i As Integer = 0 To headers.Length - 1
Dim text As New PdfFormattedText()
text.FontSize = 12
text.Append(headers(i))
page.Content.DrawText(text, New PdfPoint(xPositions(i), y))
Next
y -= 20
' Data rows — each row requires manual Y advancement,
' and free mode caps the saved file at 2 pages total.
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文件超過2頁限制的表格在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
var encryption = document.SaveOptions.SetPasswordEncryption();
encryption.DocumentOpenPassword = "userPassword";
encryption.PermissionsPassword = "ownerPassword";
// IronPDF
pdf.SecuritySettings.UserPassword = "userPassword";
pdf.SecuritySettings.OwnerPassword = "ownerPassword";
// GemBox.Pdf
var encryption = document.SaveOptions.SetPasswordEncryption();
encryption.DocumentOpenPassword = "userPassword";
encryption.PermissionsPassword = "ownerPassword";
// IronPDF
pdf.SecuritySettings.UserPassword = "userPassword";
pdf.SecuritySettings.OwnerPassword = "ownerPassword";
' GemBox.Pdf
Dim encryption = document.SaveOptions.SetPasswordEncryption()
encryption.DocumentOpenPassword = "userPassword"
encryption.PermissionsPassword = "ownerPassword"
' IronPDF
pdf.SecuritySettings.UserPassword = "userPassword"
pdf.SecuritySettings.OwnerPassword = "ownerPassword"
故障排除
問題1:找不到PdfFormattedText
問題:IronPDF中不存在PdfFormattedText。
解決方案:使用HTML與CSS樣式:
// GemBox.Pdf
var text = new PdfFormattedText();
text.FontSize = 24;
text.Append("Hello");
// IronPDF
var html = "<p style='font-size:24px;'>Hello</p>";
var pdf = renderer.RenderHtmlAsPdf(html);
// GemBox.Pdf
var text = new PdfFormattedText();
text.FontSize = 24;
text.Append("Hello");
// IronPDF
var html = "<p style='font-size:24px;'>Hello</p>";
var pdf = renderer.RenderHtmlAsPdf(html);
Imports GemBox.Pdf
Imports IronPDF
Dim text As New PdfFormattedText()
text.FontSize = 24
text.Append("Hello")
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()。
解決方案:使用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使用情況
- 確定需要HTML轉換的基於坐標的佈局
- 評估2頁免費模式限制對您程式碼的影響
- 獲得IronPDF授權金鑰
- 在版本控制中建立遷移分支
程式碼遷移
- 移除GemBox.Pdf NuGet包:
dotnet remove package GemBox.Pdf - 安裝IronPDF NuGet包:
dotnet add package IronPdf - 更新命名空間導入
- 將
IronPdf.License.LicenseKey - 將
PdfDocument.FromFile() - 將
pdf.SaveAs() - 用HTML內容替換基於坐標的文字
- 將
PdfFormattedText轉換為帶有CSS樣式的HTML - 更新合併操作以使用
PdfDocument.Merge()
測試
- 驗證所有文件是否正確生成
- 確認文件外觀與預期一致
- 測試多頁輸出(之前在免費模式下限於2頁)
- 驗證文字提取是否正常
- 測試合併和拆分操作
- 驗證安全性/加密功能
遷移後
- 移除GemBox.Pdf授權金鑰
- 更新文件
- 訓練團隊瞭解PDF的HTML/CSS方法
- 享受無限頁數且無免費模式上限!

