跳至頁尾內容
移民指南

如何在 C# 中將 Pdfium 遷移到 IronPDF

從 Pdfium.NET 遷移到 IronPDF,會將您的 .NET PDF 工作流程從以渲染為中心的程式庫(具有本機二進位依賴項)轉變為全面的 PDF 解決方案,該解決方案可處理建立、操作和渲染,而無需平台特定的複雜性。 本指南提供了一個完整的、逐步的遷移路徑,消除了原生依賴項管理,同時增加了 Pdfium 無法提供的功能。

為什麼要從 Pdfium 遷移到 IronPDF

了解 Pdfium.NET

Pdfium.NET 是 Google PDFium 庫的 .NET 封裝庫,以其在渲染 PDF 文件方面的效率和速度而聞名。 它已成為開發人員深入研究 C# 應用程式中 PDF 渲染細節的重要程式庫,可在 .NET 環境中提供 PDF 內容的高保真複製。

然而,儘管 Pdfium.NET 在渲染方面表現出色,但它在建立和操作 PDF 文件方面的功能卻很有限。 它主要面向那些需要精確顯示 PDF 內容,而不太注重修改或建立新 PDF 的應用程式場景。

鈀的關鍵限制

1.僅渲染焦點:無法從 HTML、圖像或以程式設計方式建立 PDF。 Pdfium 的功能僅限於檢視和渲染 PDF 檔案。

2.不支援 PDF 操作:無法合併、拆分或修改 PDF 內容。 PDF 合併功能目前不支援,您需要使用 iTextSharp 或 PdfSharp 等其他函式庫。

3.本地二進位依賴項:需要特定於平台的 PDFium 二進位。 開發人員需要管理原生 PDFium 二進位文件,這增加了部署和分發過程中的複雜性。

4.部署複雜性:必須將每個平台的本機 DLL 打包和管理,並包含 x86、x64 和運行時資料夾。

5.有限文字擷取:基本文字擷取,不進行格式化。 文本提取需要使用 Pdfium.NET 進行額外操作。

6.不支援 HTML 轉 PDF:無法將網頁內容轉換為 PDF。 Pdfium.NET 本身並不支援 HTML 到 PDF 的轉換。

7.無頁首/頁尾:無法新增頁碼或重複內容。

8.無浮水印:無法在有疊加層的文件上加蓋水印。

9.不支援表單:無法填寫或讀取 PDF 表單。

10.無安全功能:無法加密或密碼保護 PDF 檔案。

鈀與鐵PDF比較

方面Pdfium.NETIronPDF
主要關注點渲染/檢視完整的 PDF 解決方案
渲染保真度高保真渲染高,尤其適用於 HTML/CSS/JS
PDF 建立✓(HTML、URL、圖片)
PDF 處理✓(合併、拆分、編輯)
HTML 轉 PDF✓(鉻引擎)
水印
頁首/頁尾
表格填寫
安全
本地依賴項必需的無(完全託管)
跨平台複雜的設定自動的
易於部署受原生依賴項影響而變得複雜更輕鬆; 依賴性複雜度降低

對於計劃在 2025 年和 2026 年採用 .NET 10 和 C# 14 的團隊,IronPDF 提供了一個完全託管的基礎,消除了原生二進位管理,同時增加了全面的 PDF 創建和操作功能。


開始之前

先決條件

  1. .NET 環境: .NET Framework 4.6.2+ 或 .NET Core 3.1+ / .NET 5/6/7/8/9+
  2. NuGet 存取權限:能夠安裝 NuGet 套件
  3. IronPDF 許可證:請從ironpdf.com取得您的許可證密鑰。

NuGet 套件變更

# Remove Pdfium packages
dotnet remove package Pdfium.NET
dotnet remove package Pdfium.Net.SDK
dotnet remove package PdfiumViewer

# Install IronPDF
dotnet add package IronPdf
# Remove Pdfium packages
dotnet remove package Pdfium.NET
dotnet remove package Pdfium.Net.SDK
dotnet remove package PdfiumViewer

# Install IronPDF
dotnet add package IronPdf
SHELL

許可證配置

// Add at application startup (Program.cs or Startup.cs)
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
// Add at application startup (Program.cs or Startup.cs)
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
$vbLabelText   $csharpLabel

確定鈀的使用情況

# Find Pdfium usage
grep -r "Pdfium\|PdfDocument\.Load\|\.Render\(" --include="*.cs" .

# Find native binary references
grep -r "pdfium\.dll\|pdfium\.so\|pdfium\.dylib" --include="*.csproj" --include="*.config" .

# Find platform-specific code
grep -r "#if.*64\|WIN32\|WIN64\|LINUX\|OSX" --include="*.cs" .
# Find Pdfium usage
grep -r "Pdfium\|PdfDocument\.Load\|\.Render\(" --include="*.cs" .

# Find native binary references
grep -r "pdfium\.dll\|pdfium\.so\|pdfium\.dylib" --include="*.csproj" --include="*.config" .

# Find platform-specific code
grep -r "#if.*64\|WIN32\|WIN64\|LINUX\|OSX" --include="*.cs" .
SHELL

完整 API 參考

命名空間變更

// Pdfium.NET
using Pdfium;
using Pdfium.Net;
using PdfiumViewer;

// IronPDF
using IronPdf;
using IronPdf.Rendering;
using IronPdf.Editing;
// Pdfium.NET
using Pdfium;
using Pdfium.Net;
using PdfiumViewer;

// IronPDF
using IronPdf;
using IronPdf.Rendering;
using IronPdf.Editing;
$vbLabelText   $csharpLabel

核心類別映射

Pdfium.NETIronPDF筆記
PdfDocumentPdfDocument同名不同功能
PdfPagePdfPage類似介面
PdfPageCollectionPdfPageCollection類似介面
(無法使用)ChromePdfRendererPDF 建立
(無法使用)HtmlHeaderFooter頁首/頁尾

文件載入映射

Pdfium.NETIronPDF筆記
PdfDocument.Load(path)PdfDocument.FromFile(path)從檔案載入
PdfDocument.Load(stream)PdfDocument.FromStream(stream)從串流中載入
PdfDocument.Load(bytes)PdfDocument.FromBinaryData(bytes)從位元組加載
new PdfDocument(path)PdfDocument.FromFile(path)建構器模式

文檔屬性映射

Pdfium.NETIronPDF筆記
document.PageCountdocument.PageCount相同的
document.Pagesdocument.Pages類似系列
document.Pages[index]document.Pages[index]零基
document.GetPageSize(index)document.Pages[index].Width/Height直接屬性

文字擷取映射

Pdfium.NETIronPDF筆記
document.GetPdfText(pageIndex)document.Pages[index].Text每頁
(手動循環)document.ExtractAllText()一次顯示所有頁面
page.GetTextBounds()page.Text簡化版

儲存文件映射

Pdfium.NETIronPDF筆記
document.Save(path)document.SaveAs(path)不同的方法名稱
document.Save(stream)document.Stream訪問串流
(無法使用)document.BinaryData取得位元組

頁面渲染映射

Pdfium.NETIronPDF筆記
page.Render(width, height)pdf.RasterizeToImageFiles(path, dpi)柵格化
page.Render(width, height, flags)DPI參數品質管制
document.Render(index, width, height)pdf.RasterizeToImageFiles()批次渲染
page.RenderToScale(scale)DPI: 72 * scale縮放比例到 DPI 的轉換

Pdfium 中不提供新功能

IronPDF 功能描述
ChromePdfRenderer.RenderHtmlAsPdf()從 HTML 創建
ChromePdfRenderer.RenderUrlAsPdf()從 URL 建立
ChromePdfRenderer.RenderHtmlFileAsPdf()從 HTML 檔案創建
PdfDocument.Merge()合併PDF文件
pdf.CopyPages()提取頁面
pdf.RemovePages()刪除頁面
pdf.InsertPdf()在指定位置插入 PDF
pdf.ApplyWatermark()添加浮水印
pdf.AddHtmlHeaders()新增標題
pdf.AddHtmlFooters()新增頁腳
pdf.SecuritySettings密碼保護
pdf.SignWithDigitalSignature()數位簽名
pdf.Form表格填寫

程式碼遷移範例

範例 1:從 PDF 中提取文本

之前(Pdfium):

// NuGet: Install-Package PdfiumViewer
using PdfiumViewer;
using System;
using System.IO;
using System.Text;

class Program
{
    static void Main()
    {
        string pdfPath = "document.pdf";

        using (var document = PdfDocument.Load(pdfPath))
        {
            StringBuilder text = new StringBuilder();

            for (int i = 0; i < document.PageCount; i++)
            {
                // Note: PdfiumViewer has limited text extraction capabilities
                // Text extraction requires additional work with Pdfium.NET
                string pageText = document.GetPdfText(i);
                text.AppendLine(pageText);
            }

            Console.WriteLine(text.ToString());
        }
    }
}
// NuGet: Install-Package PdfiumViewer
using PdfiumViewer;
using System;
using System.IO;
using System.Text;

class Program
{
    static void Main()
    {
        string pdfPath = "document.pdf";

        using (var document = PdfDocument.Load(pdfPath))
        {
            StringBuilder text = new StringBuilder();

            for (int i = 0; i < document.PageCount; i++)
            {
                // Note: PdfiumViewer has limited text extraction capabilities
                // Text extraction requires additional work with Pdfium.NET
                string pageText = document.GetPdfText(i);
                text.AppendLine(pageText);
            }

            Console.WriteLine(text.ToString());
        }
    }
}
$vbLabelText   $csharpLabel

(IronPDF 之後):

// NuGet: Install-Package IronPdf
using IronPdf;
using System;

class Program
{
    static void Main()
    {
        string pdfPath = "document.pdf";

        var pdf = PdfDocument.FromFile(pdfPath);
        string text = pdf.ExtractAllText();

        Console.WriteLine(text);
    }
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;

class Program
{
    static void Main()
    {
        string pdfPath = "document.pdf";

        var pdf = PdfDocument.FromFile(pdfPath);
        string text = pdf.ExtractAllText();

        Console.WriteLine(text);
    }
}
$vbLabelText   $csharpLabel

這裡的差異非常顯著。 Pdfium 需要手動遍歷每一頁,使用GetPdfText(pageIndex)建立StringBuilder並管理using語句以進行正確處置。 程式碼指出"PdfiumViewer 的文本提取功能有限",並且"文本提取需要額外的工作"。

IronPDF 將此操作簡化為三行程式碼:使用PdfDocument.FromFile()加載,使用ExtractAllText()提取,然後輸出。 ExtractAllText()方法具有更進階的文字擷取功能,可自動處理所有頁面。 如果需要逐頁擷取,可以使用pdf.Pages[index].Text 。 有關其他選項,請參閱文字擷取文件

範例 2:PDF 合併

之前(Pdfium):

// NuGet: Install-Package PdfiumViewer
using PdfiumViewer;
using System;
using System.IO;
using System.Collections.Generic;

// Note: PdfiumViewer does not have native PDF merging functionality
// You would need to use additional libraries or implement custom logic
class Program
{
    static void Main()
    {
        List<string> pdfFiles = new List<string> 
        { 
            "document1.pdf", 
            "document2.pdf", 
            "document3.pdf" 
        };

        // PdfiumViewer is primarily for rendering/viewing
        // PDF merging is not natively supported
        // You would need to use another library like iTextSharp or PdfSharp

        Console.WriteLine("PDF merging not natively supported in PdfiumViewer");
    }
}
// NuGet: Install-Package PdfiumViewer
using PdfiumViewer;
using System;
using System.IO;
using System.Collections.Generic;

// Note: PdfiumViewer does not have native PDF merging functionality
// You would need to use additional libraries or implement custom logic
class Program
{
    static void Main()
    {
        List<string> pdfFiles = new List<string> 
        { 
            "document1.pdf", 
            "document2.pdf", 
            "document3.pdf" 
        };

        // PdfiumViewer is primarily for rendering/viewing
        // PDF merging is not natively supported
        // You would need to use another library like iTextSharp or PdfSharp

        Console.WriteLine("PDF merging not natively supported in PdfiumViewer");
    }
}
$vbLabelText   $csharpLabel

(IronPDF 之後):

// NuGet: Install-Package IronPdf
using IronPdf;
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<string> pdfFiles = new List<string> 
        { 
            "document1.pdf", 
            "document2.pdf", 
            "document3.pdf" 
        };

        var pdf = PdfDocument.Merge(pdfFiles);
        pdf.SaveAs("merged.pdf");

        Console.WriteLine("PDFs merged successfully");
    }
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<string> pdfFiles = new List<string> 
        { 
            "document1.pdf", 
            "document2.pdf", 
            "document3.pdf" 
        };

        var pdf = PdfDocument.Merge(pdfFiles);
        pdf.SaveAs("merged.pdf");

        Console.WriteLine("PDFs merged successfully");
    }
}
$vbLabelText   $csharpLabel

這個例子凸顯了一個根本性的功能缺陷。 Pdfium 無法合併 PDF 文件——程式碼明確指出"PdfiumViewer 本身不支援 PDF 合併",並且"您需要使用其他庫,例如 iTextSharp 或 PdfSharp"。

IronPDF 提供原生合併功能,其靜態方法PdfDocument.Merge()可直接接受檔案路徑清單。 結果是一個新的PdfDocument ,您可以使用SaveAs()將其儲存。 了解更多關於合併和拆分PDF的資訊

範例 3:HTML 轉 PDF

之前(Pdfium):

// NuGet: Install-Package PdfiumViewer
using PdfiumViewer;
using System.IO;
using System.Drawing.Printing;

// Note: PdfiumViewer is primarily for viewing/rendering PDFs, not creating them from HTML
// For HTML to PDF with Pdfium.NET, you would need additional libraries
// This example shows a limitation of Pdfium.NET
class Program
{
    static void Main()
    {
        // Pdfium.NET does not have native HTML to PDF conversion
        // You would need to use a separate library to convert HTML to PDF
        // then use Pdfium for manipulation
        string htmlContent = "<h1>Hello World</h1>";

        // This functionality is not directly available in Pdfium.NET
        Console.WriteLine("HTML to PDF conversion not natively supported in Pdfium.NET");
    }
}
// NuGet: Install-Package PdfiumViewer
using PdfiumViewer;
using System.IO;
using System.Drawing.Printing;

// Note: PdfiumViewer is primarily for viewing/rendering PDFs, not creating them from HTML
// For HTML to PDF with Pdfium.NET, you would need additional libraries
// This example shows a limitation of Pdfium.NET
class Program
{
    static void Main()
    {
        // Pdfium.NET does not have native HTML to PDF conversion
        // You would need to use a separate library to convert HTML to PDF
        // then use Pdfium for manipulation
        string htmlContent = "<h1>Hello World</h1>";

        // This functionality is not directly available in Pdfium.NET
        Console.WriteLine("HTML to PDF conversion not natively supported in Pdfium.NET");
    }
}
$vbLabelText   $csharpLabel

(IronPDF 之後):

// NuGet: Install-Package IronPdf
using IronPdf;
using System;

class Program
{
    static void Main()
    {
        var renderer = new ChromePdfRenderer();
        string htmlContent = "<h1>Hello World</h1>";

        var pdf = renderer.RenderHtmlAsPdf(htmlContent);
        pdf.SaveAs("output.pdf");

        Console.WriteLine("PDF created successfully");
    }
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;

class Program
{
    static void Main()
    {
        var renderer = new ChromePdfRenderer();
        string htmlContent = "<h1>Hello World</h1>";

        var pdf = renderer.RenderHtmlAsPdf(htmlContent);
        pdf.SaveAs("output.pdf");

        Console.WriteLine("PDF created successfully");
    }
}
$vbLabelText   $csharpLabel

這個例子反映了能力上最顯著的差異。 Pdfium 明確指出"Pdfium.NET 本身不支援 HTML 到 PDF 的轉換",並且"您需要使用單獨的庫將 HTML 轉換為 PDF"。

IronPDF 透過ChromePdfRenderer提供原生 HTML 到 PDF 的轉換,該渲染器內部使用 Chromium 引擎來精確渲染 HTML、CSS 和 JavaScript。 RenderHtmlAsPdf()方法直接將 HTML 字串轉換為 PDF 文件。 IronPDF 也可以使用RenderUrlAsPdf()渲染 URL,使用RenderHtmlFileAsPdf()渲染 HTML 檔案。 請參閱HTML 轉 PDF 文件以取得完整範例。


移除原生依賴項

從 Pdfium 遷移到 IronPDF 的最大好處之一是消除了原生二進位檔案的管理。

之前(Pdfium)- 複雜部署

MyApp/
├── bin/
│ ├── MyApp.dll
│ ├── Pdfium.NET.dll
│ ├── x86/
│ │ └── pdfium.dll
│ └── x64/
│ └── pdfium.dll
├── 運行時/
│ ├── win-x86/native/
│ │ └── pdfium.dll
│ └── win-x64/native/
│ └── pdfium.dll

(IronPDF)之後 - 乾淨部署

MyApp/
├── bin/
│ ├── MyApp.dll
│ └── IronPdf.dll # 包含所有內容

移除本地二進位引用

# Delete native PDFium binaries
rm -rf x86/ x64/ runtimes/

# Remove from .csproj
# Delete any <Content Include="pdfium.dll" /> entries
# Delete any <None Include="x86/pdfium.dll" /> entries
# Delete native PDFium binaries
rm -rf x86/ x64/ runtimes/

# Remove from .csproj
# Delete any <Content Include="pdfium.dll" /> entries
# Delete any <None Include="x86/pdfium.dll" /> entries
SHELL

關鍵遷移說明

縮放比例到 DPI 的轉換

Pdfium 使用比例因子; IronPDF 使用 DPI:

// Formula: IronPDF DPI = 72 × Pdfium scale
// Pdfium scale 2.0 → IronPDF DPI 144
pdf.RasterizeToImageFiles("*.png", DPI: 144);
// Formula: IronPDF DPI = 72 × Pdfium scale
// Pdfium scale 2.0 → IronPDF DPI 144
pdf.RasterizeToImageFiles("*.png", DPI: 144);
$vbLabelText   $csharpLabel

文件載入方式變更

// Pdfium
PdfDocument.Load(path)

// IronPDF
PdfDocument.FromFile(path)
// Pdfium
PdfDocument.Load(path)

// IronPDF
PdfDocument.FromFile(path)
$vbLabelText   $csharpLabel

儲存方法更改

// Pdfium
document.Save(path)

// IronPDF
pdf.SaveAs(path)
// Pdfium
document.Save(path)

// IronPDF
pdf.SaveAs(path)
$vbLabelText   $csharpLabel

處置模式簡化

// Pdfium: 必需的 explicit disposal
using (var document = PdfDocument.Load(path))
using (var page = document.Pages[0])
using (var bitmap = page.Render(1024, 768))
{
    bitmap.Save("output.png");
}

// IronPDF: Simplified
var pdf = PdfDocument.FromFile(path);
pdf.RasterizeToImageFiles("output.png");
// Pdfium: 必需的 explicit disposal
using (var document = PdfDocument.Load(path))
using (var page = document.Pages[0])
using (var bitmap = page.Render(1024, 768))
{
    bitmap.Save("output.png");
}

// IronPDF: Simplified
var pdf = PdfDocument.FromFile(path);
pdf.RasterizeToImageFiles("output.png");
$vbLabelText   $csharpLabel

平台特定程式碼移除

// Pdfium: 必需的 platform detection
#if WIN64
    // Load x64 pdfium.dll
#else
    // Load x86 pdfium.dll
#endif

// IronPDF: Remove all platform-specific code
// Just use the API directly
// Pdfium: 必需的 platform detection
#if WIN64
    // Load x64 pdfium.dll
#else
    // Load x86 pdfium.dll
#endif

// IronPDF: Remove all platform-specific code
// Just use the API directly
$vbLabelText   $csharpLabel

功能對比總結

特徵Pdfium.NETIronPDF
載入PDF
渲染到影像
提取文字✓(基本)✓(進階)
頁面資訊
從 HTML 創建
從 URL 建立
合併PDF
拆分PDF
添加浮水印
頁首/頁尾
表格填寫
數位簽名
密碼保護
本地依賴項必需的沒有任何
跨平台複雜的自動的
記憶體管理人工處置簡化版

遷移清單

遷移前

  • 識別程式碼庫中所有 Pdfium 的使用情況
  • 記錄目前使用的渲染尺寸/比例
  • 列出專案中的本機二進位位置
  • 檢查平台特定的載入程式碼
  • 確定PDF創建需求(目前是否使用不同的工具?)
  • 審查轉換處置模式
  • 取得 IronPDF 許可證密鑰

軟體包變更

  • 移除Pdfium.NETPdfium.Net.SDKPdfiumViewer NuGet 套件
  • 從 x86/、x64/ 和 runtimes/ 資料夾中刪除本機 pdfium.dll 二進位文件
  • 移除平台特定的條件編譯
  • 更新 .csproj 檔案以移除原生二進位引用 安裝IronPdf NuGet 套件: dotnet add package IronPdf

程式碼更改

  • 在啟動時新增許可證金鑰配置
  • PdfDocument.Load()替換為PdfDocument.FromFile()
  • document.Save()替換為pdf.SaveAs()
  • document.GetPdfText(i)循環替換為pdf.ExtractAllText()
  • 將比例因子轉換為 DPI 值(DPI = 72 × 比例)
  • 簡化資源釋放模式(移除巢狀的 using 語句)
  • 刪除平台特定程式碼

移民後

  • 測試渲染輸出質量
  • 比較文字擷取結果
  • 測試跨平台部署
  • 新增功能(HTML 轉 PDF、合併、浮水印、安全)
  • 更新文檔

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。