IRONSOFTWAREHOME
影片

如何使用 C# 在 PDF 中新增頁碼

Curtis Chau
Curtis Chau
Updated: 2026年7月19日

將GemBox.Pdf遷移到IronPDF會將您的.NET PDF工作流程從基於坐標的程式化文件構建轉換為現代HTML/CSS渲染。 本指南提供了一個全方位的逐步遷移路徑,移除2頁免費模式限制,並簡化專業.NET開發者的文件建立。

為何從GemBox.Pdf遷移到IronPDF

GemBox.Pdf的挑戰

GemBox.Pdf是一個功能強大的.NET PDF組件,但在實際開發中有一些限制需要考量:

  1. **2頁免費模式限制:**免費版本在載入或保存超過2頁的PDF時會拋出FreeLimitReachedException,因此超過單頁收據或兩頁發票的任何內容都需要付費許可。 (Source: gemboxsoftware.com/pdf/free-version.)

  2. 不支持HTML到PDF轉換:GemBox.Pdf無法渲染HTML——PdfDocument.Load只能打開現有的PDF文件。 要將HTML轉換為PDF,您必須購買單獨的GemBox.Document產品(不同的SKU,不同的授權)。

  3. **基於坐標的佈局:**GemBox.Pdf是一個低層次的PDF內容流API。 要放置文字,您需要計算PDF使用者空間單位中的X/Y並調用page.Content.DrawText(formattedText, new PdfPoint(x, y))。 沒有流佈局。

  4. Office到PDF需要其他SKU:Word→PDF需要GemBox.Document,Excel→PDF需要GemBox.Spreadsheet,每個都需單獨授權; GemBox.Bundle涵蓋了所有這些,但成本更高。

  5. **僅限程式化設計:**每次設計變更都需要程式碼修改。 調整間距? 重新計算坐標。 改變字體大小?調整其下方的Y位置。

  6. **商業授權定價:**單人開發者授權為890美元(續訂534美元),小團隊(10位開發者)為4,450美元,大團隊(50位開發者)為13,350美元。(來源:gemboxsoftware.com/pdf/pricing。)

  7. **設計學習曲線:**開發者必須以坐標思考,而非文件流,使得簡單任務如"新增段落"變得相當複雜。

GemBox.Pdf與IronPDF比較

方面GemBox.PdfIronPDF
免費版本限制最多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"
Text

這一範式轉變對於熟悉網路技術的開發者而言普遍更簡單,但需要以不同的方式思考PDF。


開始之前

前提條件

  1. **.NET版本:**IronPDF支持.NET Framework 4.6.2+和.NET Core 2.0+/ .NET 5+
  2. **授權金鑰:**從ironpdf.com取得您的IronPDF授權金鑰
  3. **備份:**建立一個遷移工作的分支
  4. **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" .
SHELL

NuGet包變更

# Remove GemBox.Pdf
dotnet remove package GemBox.Pdf

# Install IronPDF
dotnet add package IronPdf
SHELL

快速開始遷移

步驟1:更新授權配置

之前(GemBox.Pdf):

// 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" }

步驟2:更新名稱空間的引入

// Before (GemBox.Pdf)
using GemBox.Pdf;
using GemBox.Pdf.Content;

// After (IronPDF)
using IronPdf;
using 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");
}

之後(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");

主要區別:

  • 不需要坐標計算
  • HTML/CSS代替程式化佈局
  • 沒有2頁免費模式上限
  • 更簡潔、更可讀的程式碼

完整API參考

命名空間映射

GemBox.PdfIronPDF
GemBox.PdfIronPdf
GemBox.Pdf.ContentIronPdf (內容是HTML)
GemBox.Pdf.SecurityIronPdf (SecuritySettings)
GemBox.Pdf.FormsIronPdf.Forms

核心類映射

GemBox.PdfIronPDF說明
PdfDocumentPdfDocument主PDF文件類
PdfPagePdfDocument.Pages[i]頁面表示
PdfContent不適用(使用HTML)頁面內容
PdfFormattedText不適用(使用HTML)格式化文字
PdfPoint不適用(使用CSS定位)坐標定位
ComponentInfo.SetLicense()IronPdf.License.LicenseKey授權管理

文件操作

GemBox.PdfIronPDF
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.PdfIronPDF
document.Pages.Add()通過HTML渲染建立
document.Pages.Countpdf.PageCount
document.Pages[index]pdf.Pages[index]
document.Pages.AddClone(pages)PdfDocument.Merge()

文字和內容操作

GemBox.PdfIronPDF
new PdfFormattedText()HTML字串
formattedText.Append(text)包含在HTML中
formattedText.AppendLine(text)包含在HTML中
formattedText.FontSize = 12CSS 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");
    }
}

之後(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");
    }
}

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");
        }
    }
}

之後(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");
    }
}

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");
        }
    }
}

之後(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");
    }
}

對於向現有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");
}

之後(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>
                <!-- Add hundreds or thousands of rows - no limit! -->
            </tbody>
        </table>
    </body>
    </html>";

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("products.pdf");
C#

這是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>
HTML

頁面索引

GemBox.Pdf和IronPDF都使用從0開始索引的頁面,使得該遷移的這方面簡單明瞭:

// GemBox.Pdf
var page = document.Pages[0];

// IronPDF
var 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";

故障排除

問題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);

問題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);

問題3:文件載入差異

**問題:**找不到PdfDocument.Load()

**解決方案:**使用FromStream()

// GemBox.Pdf
var doc = PdfDocument.Load("input.pdf");

// IronPDF
var pdf = PdfDocument.FromFile("input.pdf");

問題4:保存方法差異

問題:document.Save()方法簽名不同。

**解決方案:**使用SaveAs()

// 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方法
  • 享受無限頁數且無免費模式上限!

請注意: GemBox.Pdf是其各自所有者的註冊商標。 此網站未獲GemBox Software Ltd.附屬、認可或贊助。所有產品名稱、標誌和品牌均為其各自所有者的財產。 比較僅供參考,反映撰寫時公開可用的資訊。
Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

...
閱讀更多

相關文章

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預訂您的免費現場演示
Booking Badge

受到全球數百萬工程師的信任

Iron Software的客戶標誌
獲取您的無義務諮詢
填寫以下表格或電子郵件sales@ironsoftware.com
您的詳細資訊將始終保密
受到全球數百萬工程師的信任
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立