跳過到頁腳內容
遷移指南

如何在 C# 中從 XFINIUM.PDF 轉移到 IronPDF

從 XFINIUM.PDF 移植到 IronPDF:完整的 C# 開發人員指南。

XFINIUM.PDF 是一個跨平台的 PDF 函式庫,提供全面的工具以 C# 程式化的方式建立與編輯 PDF。 雖然它提供了兩個版本-Generator 和 Viewer,但資料庫對於基於座標的圖形程式設計的依賴,為建立文件繁重應用程式的開發團隊帶來了重大的挑戰。 每個元素都必須使用像素坐標來手動定位,讓原本簡單的文件變成複雜的繪圖練習。

本指南提供了從 XFINIUM.PDF 到IronPDF的完整轉換路徑,為評估此轉換的專業 .NET 開發人員提供了分步說明、程式碼比較以及實用範例。

為何要從 XFINIUM.PDF 遷移?

XFINIUM.PDF 是一個低階 PDF 函式庫,它依賴於基於座標的圖形編程,迫使開發人員手動定位頁面上的每個元素。 隨著需求的變化,這種方法也成為維護的惡夢。 開發團隊考慮遷移的主要原因包括

No HTML Support:XFINIUM.PDF 無法直接將 HTML/CSS 轉換為 PDF。 它著重於使用低階繪圖原素進行程式化 PDF 創作,對於需要大量 HTML-to-PDF 功能的專案而言,這可能並不足夠。

基於座標的 API:頁面上的每個元素都需要使用像素座標進行手動定位,例如 DrawString("text", font, brush, 50, 100)

手動字型管理:必須使用<編碼>PdfStandardFont</編碼和<編碼>PdfBrush</編碼等類別明確地建立和管理字型物件。

No CSS Styling:不支援現代網頁造型。 顏色、字型和版面必須透過程式化的方法呼叫來手動處理。

No JavaScript Rendering:僅提供靜態內容。 XFINIUM.PDF 無法渲染動態網頁內容或執行 JavaScript。

複雜的文字排版:除了簡單的單行文字外,還需要手動進行文字測量和包裝計算。

有限的社群資源:與主流解決方案相比,社群提供的資源(例如範例和教學)較為缺乏,這可能會讓新使用者較難上手。

核心問題:圖形 API vs 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
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

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);
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

IronPDFvs XFINIUM.PDF:功能比較

了解架構上的差異有助於技術決策者評估遷移投資:

特點XFINIUM.PDFIronPDF
HTML至PDF有限的 HTML 支援,著重於程式化 PDF 建立全面支援 HTML 至 PDF 的完整轉換
社群與支援社群規模較小,可用的線上資源較少擁有大量文件和教學的大型社群
執照基於開發人員的商業授權商業的
跨平台支援強大的跨平台能力同時支援跨平台作業
CSS 支援完整的 CSS3
JavaScript完整的 ES2024
Flexbox/網格
自動排版
自動分頁
手冊定位要求可選 (CSS 定位)
學習曲線高(坐標系統)低 (HTML/CSS)
程式語言非常高

快速入門:XFINIUM.PDF 到IronPDF的轉換。

只要完成這些基本步驟,就可以立即開始遷移。

步驟 1:取代 NuGet 套件

移除 XFINIUM.PDF:

# Remove XFINIUM.PDF
dotnet remove package Xfinium.Pdf
# Remove XFINIUM.PDF
dotnet remove package Xfinium.Pdf
SHELL

安裝 IronPDF:

# Install IronPDF
dotnet add package IronPdf
# Install IronPDF
dotnet add package IronPdf
SHELL

步驟 2:更新命名空間

用 IronPdf 命名空間取代 XFINIUM.PDF 命名空間:

// 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;
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

步驟 3:初始化授權

在應用程式啟動時加入授權初始化:

IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
$vbLabelText   $csharpLabel

程式碼遷移範例

將 HTML 轉換為 PDF

最基本的操作揭示了這些 .NET PDF 函式庫的複雜性差異。

XFINIUM.PDF 方法:

// NuGet: Install-Package Xfinium.Pdf
using Xfinium.Pdf;
using Xfinium.Pdf.Actions;
using Xfinium.Pdf.FlowDocument;
using System.IO;

class Program
{
    static void Main()
    {
        PdfFixedDocument document = new PdfFixedDocument();
        PdfFlowDocument flowDocument = new PdfFlowDocument();

        string html = "<html><body><h1>Hello World</h1><p>This is a PDF from HTML.</p></body></html>";

        PdfFlowContent content = new PdfFlowContent();
        content.AppendHtml(html);
        flowDocument.AddContent(content);

        flowDocument.RenderDocument(document);
        document.Save("output.pdf");
    }
}
// NuGet: Install-Package Xfinium.Pdf
using Xfinium.Pdf;
using Xfinium.Pdf.Actions;
using Xfinium.Pdf.FlowDocument;
using System.IO;

class Program
{
    static void Main()
    {
        PdfFixedDocument document = new PdfFixedDocument();
        PdfFlowDocument flowDocument = new PdfFlowDocument();

        string html = "<html><body><h1>Hello World</h1><p>This is a PDF from HTML.</p></body></html>";

        PdfFlowContent content = new PdfFlowContent();
        content.AppendHtml(html);
        flowDocument.AddContent(content);

        flowDocument.RenderDocument(document);
        document.Save("output.pdf");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

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");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

XFINIUM.PDF需要創建一個PdfFixedDocument、一個PdfFlowDocument、一個PdfFlowContent物件、呼叫AppendHtml()、新增內容到flow文件、渲染到固定文件,最後儲存。IronPDF將此簡化為三行:建立渲染器、渲染 HTML、儲存。

如需進階的 HTML 至IronPDF情境,請參閱 HTML 至 PDF 轉換指南

合併多個 PDF 文件

PDF 合併可清楚展示 API 複雜性的差異。

XFINIUM.PDF 方法:

// NuGet: Install-Package Xfinium.Pdf
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
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();
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

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");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

XFINIUM.PDF 需要建立輸出文件、開啟檔案串流、載入每個文件、手動反覆瀏覽頁面並逐一新增、儲存,然後關閉串流。IronPDF提供了單一的 PdfDocument.Merge() 方法,可在內部處理所有複雜的問題。

探索 IronPDF合併文件,以獲得其他合併選項。

使用文字和圖片建立 PDF。

內容混雜的文件顯示出基本範式的差異。

XFINIUM.PDF 方法:

// NuGet: Install-Package Xfinium.Pdf
using Xfinium.Pdf;
using Xfinium.Pdf.Graphics;
using Xfinium.Pdf.Core;
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(PdfRgbColor.Black);

        page.Graphics.DrawString("Sample PDF Document", font, brush, 50, 50);

        FileStream imageStream = File.OpenRead("image.jpg");
        PdfJpegImage image = new PdfJpegImage(imageStream);
        page.Graphics.DrawImage(image, 50, 100, 200, 150);
        imageStream.Close();

        document.Save("output.pdf");
    }
}
// NuGet: Install-Package Xfinium.Pdf
using Xfinium.Pdf;
using Xfinium.Pdf.Graphics;
using Xfinium.Pdf.Core;
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(PdfRgbColor.Black);

        page.Graphics.DrawString("Sample PDF Document", font, brush, 50, 50);

        FileStream imageStream = File.OpenRead("image.jpg");
        PdfJpegImage image = new PdfJpegImage(imageStream);
        page.Graphics.DrawImage(image, 50, 100, 200, 150);
        imageStream.Close();

        document.Save("output.pdf");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

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");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

XFINIUM.PDF 需要建立文件、新增頁面、建立字型和筆刷物件、在特定座標繪畫文字、開啟影像串流、建立<代碼>PdfJpegImage</代碼、在有尺寸的座標繪畫影像、關閉串流並儲存。IronPDF使用內嵌 base64 圖片的標準 HTML - 與網頁開發人員日常使用的方法相同。

XFINIUM.PDF API 到IronPDF的映射參考

此對應可透過顯示直接的 API 對應關係來加速遷移:

XFINIUM.PDFIronPDF筆記
<編碼>PDFFixedDocument</編碼<代碼>ChromePdfRenderer</代碼建立呈現器,而非文件
PdfPage自動化從 HTML 內容建立的頁面
page.Graphics.DrawString()頁面。HTML 文字元素<p><h1><span>等。
page.Graphics.DrawImage()頁面。<img> 標籤HTML 影像
page.Graphics.DrawLine()頁面。CSS 邊框<hr>HTML/CSS 行
page.Graphics.DrawRectangle()頁面。CSS <div> 上的 邊框HTML 方塊
<編碼>PdfStandardFont</編碼CSS font-family不需要字型物件
<編碼>PdfRgbColor</編碼CSS 顏色標準 CSS 顏色
<編碼>PdfBrush</編碼CSS 特性背景、顏色等
<代碼>PdfJpegImage</代碼使用 base64 的 <img> 標籤或檔案路徑
document.Save(stream)pdf.SaveAs()pdf.BinaryData多種輸出選項
<編碼>PdfFlowDocument</編碼<代碼>RenderHtmlAsPdf()</代碼完整的 HTML 支援
<編碼>PdfFlowContent.AppendHtml()</編碼<代碼>RenderHtmlAsPdf()</代碼直接 HTML 渲染

常見的遷移問題與解決方案

問題 1:基於座標的佈局

XFINIUM.PDF:所有內容都需要精確的 X、Y 座標與手動定位。

解決方案: 使用 HTML/CSS 流程排版。 需要絕對定位時,請使用 CSS:

.positioned-element {
    position: absolute;
    top: 100px;
    left: 50px;
}

第 2 期:字型物件管理

XFINIUM.PDF:為每個字型建立<編碼>PdfStandardFont</編碼或 PdfUnicodeTrueTypeFont 物件。

解決方案:使用 CSS font-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>
HTML

第 3 期:顏色處理

XFINIUM.PDF:為顏色建立<編碼>PdfRgbColor</編碼和<編碼>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</代碼物件、繪製座標、關閉串流。

解決方案:使用包含檔案路徑或 base64 資料的 HTML <img> 標籤:

<img src="image.jpg" width="200" height="150" />
<!-- or -->
<img src="data:image/jpeg;base64,..." />
<img src="image.jpg" width="200" height="150" />
<!-- or -->
<img src="data:image/jpeg;base64,..." />
HTML

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" .
SHELL

記錄基於座標的佈局,並註明所有 X、Y 定位值。 識別字型和顏色物件(PdfStandardFontPdfRgbColorPdfBrush)。 使用 PdfFixedDocument.Pages.Add() 映射合併的 PDF 工作流程。

程式碼更新任務

1.移除 Xfinium.Pdf NuGet 套件 2.安裝 IronPdf NuGet 套件 3.更新命名空間匯入,從 Xfinium.PdfIronPdf 4.將 DrawString() 呼叫轉換為 HTML 文字元素 5.將 DrawImage() 呼叫轉換為 HTML <img> 標籤 6.將 DrawRectangle()DrawLine() 轉換為 CSS 邊框 7.將<編碼>PdfStandardFont</編碼替換為 CSS font-family 8.將<編碼>PdfRgbColor</編碼和<編碼>PdfBrush</編碼替換為 CSS 顏色 9.使用 PdfDocument.Merge() 取代頁面循環合併 10.在啟動時增加 IronPdf 授權初始化功能

後遷移測試

轉移後,驗證這些方面:

  • 比較視覺輸出,以確保外觀符合預期
  • 使用新的 HTML/CSS 方法驗證文字呈現
  • 使用 CSS 檢查影像定位
  • 測試分頁是否如預期發生
  • 驗證 PDF 安全設定是否正確套用
  • 在所有目標平台上進行測試

遷移到IronPDF的主要優點。

從 XFINIUM.PDF 轉移到IronPDF提供了幾個關鍵的優勢:

以 HTML 為基礎的內容創作:網頁開發人員可以利用現有的 HTML 和 CSS 技能。 不需要學習基於座標的繪圖 API 或管理字型和筆刷物件。

自動排版:自動進行文字包裝、分頁和流程排版。 不可手動計算元素位置或分頁。

Modern CSS 支援:完整的 CSS3,包括 Flexbox 和網格佈局。 回應式設計可直接翻譯為 PDF。

簡化的 PDF 作業:單一方法呼叫常見的作業,例如 PdfDocument.Merge() 取代複雜的頁面迭代循環。

主動開發:隨著 .NET 10 和 C# 14 的採用增加至 2026 年,IronPDF 的定期更新可確保與目前和未來的 .NET 版本相容。

廣泛的文件:相較於 XFINIUM.PDF 較小的生態系統,XFINIUM.PDF 擁有大型社群,提供全面的文件、教學和支援資源。

結論

XFINIUM.PDF 提供全面的 PDF 操作工具,具有強大的跨平台功能。 然而,其基於坐標的圖形編程方法為文件生成任務製造了不必要的複雜性。 每個元素都需要手動定位、建立字型物件以及明確的顏色管理。

IronPDF 將 PDF 生成從圖形編程練習轉變為熟悉的 HTML/CSS 開發。 轉換路徑很直接:取代 NuGet 套件、將繪圖指令轉換為 HTML 元素,並利用 CSS 進行造型和排版。

立即使用 免費試用 IronPDF 開始您的遷移,體驗基於 HTML 的簡易文件生成。

如需全面的實施指導,請探索 IronPDF 文件教學

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。