跳至页脚内容
迁移指南

如何用 C# 从 IronPDFium 迁移到 IronPDF

从PDFium .NET 包装器迁移到IronPDF,将您的 .NET PDF 工作流从一个具有原生二进制依赖项的渲染为中心的引擎转移到一个处理创建、操作和渲染且无平台特定复杂性的综合PDF 解决方案。 本指南提供了一个逐步迁移路径,消除了原生依赖项管理,同时增加了PDFium本身没有提供的功能。

为何从PDFium包装器迁移到IronPDF

了解PDFium .NET 包装器

PDFium 是Google 的BSD-3-Clause C++ PDF 渲染和解析引擎—相同的引擎为Chrome 内置的PDF 查看器提供动力。 没有Google 提供的官方.NET 绑定; 相反,.NET 生态系统通过社区包装器使用PDFium。 四个最常被引用的NuGet包是PdfiumViewer (pvginkel; Apache 2.0; 2019年8月归档,最后版本2.13.0发布于2017年11月,.NET Framework 2.0+),PdfiumViewer.Updated (一个移植到.NET Core / .NET 6的维护分支),PDFiumCore (Dtronix; .NET Standard 2.1 P/Invoke绑定),和Pdfium.Net.SDK (Patagames; 商业,永久许可证SDK)。

它们的API各不相同,但共享一个共同特点:PDFium 本身是一个渲染和解析引擎,而不是HTML 到PDF 或文档创建引擎。PDFium 没有HTML 解析器,因此没有包装器可以自行将HTML 转换为PDF。 本指南使用PdfiumViewer作为"以前"代码片段的代表性封装器,因为其API被最广泛引用; 迁移方法对于这四者中的任何一个都是一样的。

PDFium 包装器的关键限制

  1. 渲染优先: PDFium 没有HTML 解析器,因此没有包装器可以从HTML、URL 或任意图像创建PDF。

  2. 开源包装器中的有限操作:PdfiumViewer/PDFiumCore 暴露了查看和每页文本提取; 在 Patagames Pdfium.Net.SDK 中合并 / 分割 / 表单编辑是部分支持的,在免费包装器中则几乎不存在。

  3. 本地二进制依赖项: 每个RID需要平台特定的PDFium二进制文件 (pdfium.dll / .so / .dylib)。

  4. 部署复杂性: 每个平台必须捆绑并管理 x86, x64, 和运行时文件夹中的本地二进制文件。

  5. 基本文本提取: PdfiumViewer通过GetPdfText(int)暴露每页的原始文本; 不暴露布局 / 格式元数据。

  6. 无 HTML 到 PDF: PDFium 没有HTML 解析器; 网页内容无法直接转换。

  7. 无内置页眉/页脚: 无高级页面覆盖API。

  8. 无内置水印: 无印章原语。

  9. 表单: 在免费包装器中是只读的或不存在的; 在Patagames Pdfium.Net.SDK中可用。

  10. 安全性: 免费包装器不暴露加密 / 权限; 在Patagames Pdfium.Net.SDK中可用。

PDFium 包装器与IronPDF的比较

方面 PDFium .NET 包装器 IronPDF
主要关注点 渲染/查看 完整的 PDF 解决方案
渲染保真度 高保真渲染 高,尤其是 HTML/CSS/JS。
从 HTML 创建 PDF None 是 (HTML, URL, 图像)
PDF 操作 免费包装器:没有; Patagames:部分 是 (合并, 分割, 编辑)
HTML 到 PDF None 是(Chromium 引擎)
水印 非内置
页眉/页脚 非内置
表格填写 免费包装器:没有; Patagames:是
安全性 免费包装器:没有; Patagames:是
本地依赖关系 要求 捆绑 / 由NuGet 管理
跨平台 手动本地二进制管理 自动翻译
易于部署 因本地依赖性而复杂 更简单; 减少依赖复杂性

对于目标为现代.NET 的团队,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 whichever PDFium wrapper you used:
#  PdfiumViewer           (Apache 2.0 - archived 2019, .NET Framework only)
#   PdfiumViewer.Updated    (community fork, .NET Core / .NET 6)
#   PDFiumCore              (Dtronix - .NET Standard 2.1 P/Invoke bindings)
#   Pdfium.Net.SDK          (Patagames - commercial, perpetual license)
dotnet remove package PdfiumViewer
dotnet remove package PdfiumViewer.Updated
dotnet remove package PDFiumCore
dotnet remove package Pdfium.Net.SDK

# Install IronPDF
dotnet add package IronPdf
# Remove whichever PDFium wrapper you used:
#  PdfiumViewer           (Apache 2.0 - archived 2019, .NET Framework only)
#   PdfiumViewer.Updated    (community fork, .NET Core / .NET 6)
#   PDFiumCore              (Dtronix - .NET Standard 2.1 P/Invoke bindings)
#   Pdfium.Net.SDK          (Patagames - commercial, perpetual license)
dotnet remove package PdfiumViewer
dotnet remove package PdfiumViewer.Updated
dotnet remove package PDFiumCore
dotnet remove package Pdfium.Net.SDK

# 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";
' Add at application startup (Program.vb or Startup.vb)
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
$vbLabelText   $csharpLabel

识别 PDFium 包装器的使用

# Find PDFium wrapper usage
grep -rE "PdfiumViewer|PDFiumCore|Patagames\.Pdf|PdfDocument\.Load|\.Render\(" --include="*.cs" .

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

# Find platform-specific code
grep -rE "#if.*64|WIN32|WIN64|LINUX|OSX" --include="*.cs" .
# Find PDFium wrapper usage
grep -rE "PdfiumViewer|PDFiumCore|Patagames\.Pdf|PdfDocument\.Load|\.Render\(" --include="*.cs" .

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

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

完整的 API 参考

命名空间变更

// PDFium wrappers (use the one you installed)
using PdfiumViewer;             //PdfiumViewer/PdfiumViewer.Updated
using PDFiumCore;               // Dtronix PDFiumCore (P/Invoke bindings)
using Patagames.Pdf;            // Patagames Pdfium.Net.SDK
using Patagames.Pdf.Net;        // Patagames Pdfium.Net.SDK

// IronPDF
using IronPdf;
using IronPdf.Rendering;
using IronPdf.Editing;
// PDFium wrappers (use the one you installed)
using PdfiumViewer;             //PdfiumViewer/PdfiumViewer.Updated
using PDFiumCore;               // Dtronix PDFiumCore (P/Invoke bindings)
using Patagames.Pdf;            // Patagames Pdfium.Net.SDK
using Patagames.Pdf.Net;        // Patagames Pdfium.Net.SDK

// IronPDF
using IronPdf;
using IronPdf.Rendering;
using IronPdf.Editing;
Imports PdfiumViewer 'PdfiumViewer/PdfiumViewer.Updated
Imports PDFiumCore ' Dtronix PDFiumCore (P/Invoke bindings)
Imports Patagames.Pdf ' Patagames Pdfium.Net.SDK
Imports Patagames.Pdf.Net ' Patagames Pdfium.Net.SDK

Imports IronPdf
Imports IronPdf.Rendering
Imports IronPdf.Editing
$vbLabelText   $csharpLabel

核心类映射

PdfiumViewer IronPDF 备注
PdfiumViewer.PdfDocument IronPdf.PdfDocument 不同命名空间中相同的简单名称
PdfRenderer (WinForms控件) (不适用) PdfiumViewer 也提供 UI 控件;IronPDF是无头的
(不可用)_ ChromePdfRenderer HTML / URL → PDF
(不可用)_ HtmlHeaderFooter 页眉/页脚

文档加载映射

PdfiumViewer IronPDF 备注
PdfDocument.Load(path) PdfDocument.FromFile(path) 从文件加载
PdfDocument.Load(stream) PdfDocument.FromStream(stream) 从流加载
(用 MemoryStream 包装字节) PdfDocument.FromBinaryData(bytes) 从字节加载

文档属性映射

PdfiumViewer IronPDF 备注
document.PageCount document.PageCount 相同
document.PageSizes (IList<SizeF>) document.Pages[index].Width / Height PdfiumViewer 暴露尺寸; pages are not first-class objects
document.PageSizes[i].Width document.Pages[i].Width 每页宽度以点数为单位

文本提取映射

PdfiumViewer IronPDF 备注
document.GetPdfText(pageIndex) document.Pages[index].Text 每页
(手动循环) document.ExtractAllText() 所有页面

保存文档映射

PdfiumViewer IronPDF 备注
document.Save(stream) document.SaveAs(path) PdfiumViewer 接受一个流;IronPDF接受一个路径
(不可用)_ document.Stream 访问原始流
(不可用)_ document.BinaryData 获取字节

页面渲染映射

PdfiumViewer IronPDF 备注
document.Render(page, width, height, dpiX, dpiY, flags) pdf.RasterizeToImageFiles(path, DPI) 光栅化
手动循环document.Render(i, ...) pdf.RasterizeToImageFiles("page_*.png") 批量渲染所有页面
手动缩放数学 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 中提取文本

之前 (PdfiumViewer):

// NuGet: Install-PackagePdfiumViewer (Apache 2.0; archived Aug 2019, .NET Framework 2.0+)
//        or Install-Package PdfiumViewer.Updated  (maintained fork, .NET Core / .NET 6)
//        or Install-Package PDFiumCore             (Dtronix, .NET Standard 2.1 P/Invoke)
//        or Install-Package Pdfium.Net.SDK         (Patagames, commercial perpetual license)
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++)
            {
                // PdfiumViewer's text extraction is per-page raw text via GetPdfText(int).
                // No layout / format metadata is exposed.
                string pageText = document.GetPdfText(i);
                text.AppendLine(pageText);
            }

            Console.WriteLine(text.ToString());
        }
    }
}
// NuGet: Install-PackagePdfiumViewer (Apache 2.0; archived Aug 2019, .NET Framework 2.0+)
//        or Install-Package PdfiumViewer.Updated  (maintained fork, .NET Core / .NET 6)
//        or Install-Package PDFiumCore             (Dtronix, .NET Standard 2.1 P/Invoke)
//        or Install-Package Pdfium.Net.SDK         (Patagames, commercial perpetual license)
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++)
            {
                // PdfiumViewer's text extraction is per-page raw text via GetPdfText(int).
                // No layout / format metadata is exposed.
                string pageText = document.GetPdfText(i);
                text.AppendLine(pageText);
            }

            Console.WriteLine(text.ToString());
        }
    }
}
Imports PdfiumViewer
Imports System
Imports System.IO
Imports System.Text

Module Program
    Sub Main()
        Dim pdfPath As String = "document.pdf"

        Using document = PdfDocument.Load(pdfPath)
            Dim text As New StringBuilder()

            For i As Integer = 0 To document.PageCount - 1
                ' PdfiumViewer's text extraction is per-page raw text via GetPdfText(int).
                ' No layout / format metadata is exposed.
                Dim pageText As String = document.GetPdfText(i)
                text.AppendLine(pageText)
            Next

            Console.WriteLine(text.ToString())
        End Using
    End Sub
End Module
$vbLabelText   $csharpLabel

After (IronPDF):

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

class Program
{
    static void Main()
    {
        IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";

        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()
    {
        IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";

        string pdfPath = "document.pdf";

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

        Console.WriteLine(text);
    }
}
Imports IronPdf
Imports System

Class Program
    Shared Sub Main()
        IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"

        Dim pdfPath As String = "document.pdf"

        Dim pdf = PdfDocument.FromFile(pdfPath)
        Dim text As String = pdf.ExtractAllText()

        Console.WriteLine(text)
    End Sub
End Class
$vbLabelText   $csharpLabel

这里的差异非常大。 PdfiumViewer需要手动循环遍历每一页,使用using语句以便正确处理——返回的文本是没有布局/格式元数据的每页原始文本。

IronPDF将此简化为几行:使用ExtractAllText()提取,然后输出。 ExtractAllText()方法可以自动处理所有页面。 如果您需要逐页提取,可以使用pdf.Pages[index].Text。 有关其他选项,请参阅文本提取文档

示例 2:PDF 合并

之前 (PdfiumViewer):

// NuGet: Install-PackagePdfiumViewer (representative open-source PDFium wrapper)
//PdfiumViewerand PDFiumCore do not expose PDF merge APIs - PDFium itself
// is a rendering / parsing engine, not a document-authoring engine.
// (Patagames Pdfium.Net.SDK exposes some document-edit operations but is commercial.)
using PdfiumViewer;
using System;
using System.IO;
using System.Collections.Generic;

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

        // To merge PDFs from a PDFium wrapper you must reach for another library
        // (PdfSharp, iText, IronPDF) or, with Patagames Pdfium.Net.SDK, use its
        // document-edit APIs.

        Console.WriteLine("PDF merging is not supported byPdfiumViewer/PDFiumCore.");
    }
}
// NuGet: Install-PackagePdfiumViewer (representative open-source PDFium wrapper)
//PdfiumViewerand PDFiumCore do not expose PDF merge APIs - PDFium itself
// is a rendering / parsing engine, not a document-authoring engine.
// (Patagames Pdfium.Net.SDK exposes some document-edit operations but is commercial.)
using PdfiumViewer;
using System;
using System.IO;
using System.Collections.Generic;

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

        // To merge PDFs from a PDFium wrapper you must reach for another library
        // (PdfSharp, iText, IronPDF) or, with Patagames Pdfium.Net.SDK, use its
        // document-edit APIs.

        Console.WriteLine("PDF merging is not supported byPdfiumViewer/PDFiumCore.");
    }
}
Imports PdfiumViewer
Imports System
Imports System.IO
Imports System.Collections.Generic

Module Program
    Sub Main()
        Dim pdfFiles As New List(Of String) From {
            "document1.pdf",
            "document2.pdf",
            "document3.pdf"
        }

        ' To merge PDFs from a PDFium wrapper you must reach for another library
        ' (PdfSharp, iText, IronPDF) or, with Patagames Pdfium.Net.SDK, use its
        ' document-edit APIs.

        Console.WriteLine("PDF merging is not supported by PdfiumViewer/PDFiumCore.")
    End Sub
End Module
$vbLabelText   $csharpLabel

After (IronPDF):

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

class Program
{
    static void Main()
    {
        IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";

        List<string> pdfFiles = new List<string>
        {
            "document1.pdf",
            "document2.pdf",
            "document3.pdf"
        };

        // PdfDocument.Merge accepts an IEnumerable<PdfDocument>, so load each file first.
        var docs = pdfFiles.Select(path => PdfDocument.FromFile(path)).ToList();
        var merged = PdfDocument.Merge(docs);
        merged.SaveAs("merged.pdf");

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

class Program
{
    static void Main()
    {
        IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";

        List<string> pdfFiles = new List<string>
        {
            "document1.pdf",
            "document2.pdf",
            "document3.pdf"
        };

        // PdfDocument.Merge accepts an IEnumerable<PdfDocument>, so load each file first.
        var docs = pdfFiles.Select(path => PdfDocument.FromFile(path)).ToList();
        var merged = PdfDocument.Merge(docs);
        merged.SaveAs("merged.pdf");

        Console.WriteLine("PDFs merged successfully");
    }
}
Imports IronPdf
Imports System
Imports System.Collections.Generic
Imports System.Linq

Module Program
    Sub Main()
        IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"

        Dim pdfFiles As New List(Of String) From {
            "document1.pdf",
            "document2.pdf",
            "document3.pdf"
        }

        ' PdfDocument.Merge accepts an IEnumerable(Of PdfDocument), so load each file first.
        Dim docs = pdfFiles.Select(Function(path) PdfDocument.FromFile(path)).ToList()
        Dim merged = PdfDocument.Merge(docs)
        merged.SaveAs("merged.pdf")

        Console.WriteLine("PDFs merged successfully")
    End Sub
End Module
$vbLabelText   $csharpLabel

此示例突显了能力差距。免费PDFium包装器(PdfiumViewer,PDFiumCore)不暴露合并API;Patagames Pdfium.Net.SDK 暴露了一些文档编辑操作,但它是商业的。

IronPDF通过静态PdfDocument.Merge()方法提供原生合并,该方法接受一个IEnumerable<PdfDocument>——先用PdfDocument.FromFile()加载每个输入文件,然后合并生成的文档。 结果是一个新的PdfDocument,您可以用SaveAs()保存。 了解有关 合并和拆分 PDF 的更多信息。

示例 3:HTML 到 PDF 的转换

之前 (PdfiumViewer):

// NuGet: Install-PackagePdfiumViewer (representative PDFium wrapper)
// PDFium is a PDF rendering / parsing engine. It has NO HTML parser, so no
// PDFium .NET wrapper (PdfiumViewer, PdfiumViewer.Updated, PDFiumCore,
// Pdfium.Net.SDK) can convert HTML to PDF on its own.
using PdfiumViewer;
using System;
using System.IO;
using System.Drawing.Printing;

class Program
{
    static void Main()
    {
        // PDFium has no native HTML-to-PDF capability.
        // Produce the PDF with another engine (wkhtmltopdf, headless Chromium,
        // IronPDF, etc.) and then load it with PdfDocument.Load(...) for rendering.
        string htmlContent = "<h1>Hello World</h1>";

        Console.WriteLine("HTML to PDF conversion is not supported by PDFium.");
    }
}
// NuGet: Install-PackagePdfiumViewer (representative PDFium wrapper)
// PDFium is a PDF rendering / parsing engine. It has NO HTML parser, so no
// PDFium .NET wrapper (PdfiumViewer, PdfiumViewer.Updated, PDFiumCore,
// Pdfium.Net.SDK) can convert HTML to PDF on its own.
using PdfiumViewer;
using System;
using System.IO;
using System.Drawing.Printing;

class Program
{
    static void Main()
    {
        // PDFium has no native HTML-to-PDF capability.
        // Produce the PDF with another engine (wkhtmltopdf, headless Chromium,
        // IronPDF, etc.) and then load it with PdfDocument.Load(...) for rendering.
        string htmlContent = "<h1>Hello World</h1>";

        Console.WriteLine("HTML to PDF conversion is not supported by PDFium.");
    }
}
Imports PdfiumViewer
Imports System
Imports System.IO
Imports System.Drawing.Printing

Module Program
    Sub Main()
        ' PDFium has no native HTML-to-PDF capability.
        ' Produce the PDF with another engine (wkhtmltopdf, headless Chromium,
        ' IronPDF, etc.) and then load it with PdfDocument.Load(...) for rendering.
        Dim htmlContent As String = "<h1>Hello World</h1>"

        Console.WriteLine("HTML to PDF conversion is not supported by PDFium.")
    End Sub
End Module
$vbLabelText   $csharpLabel

After (IronPDF):

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

class Program
{
    static void Main()
    {
        IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";

        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()
    {
        IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";

        var renderer = new ChromePdfRenderer();
        string htmlContent = "<h1>Hello World</h1>";

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

        Console.WriteLine("PDF created successfully");
    }
}
Imports IronPdf
Imports System

Module Program
    Sub Main()
        IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"

        Dim renderer As New ChromePdfRenderer()
        Dim htmlContent As String = "<h1>Hello World</h1>"

        Dim pdf = renderer.RenderHtmlAsPdf(htmlContent)
        pdf.SaveAs("output.pdf")

        Console.WriteLine("PDF created successfully")
    End Sub
End Module
$vbLabelText   $csharpLabel

本例展示了最显著的能力差异。 PDFium 没有HTML 解析器,因此无法将其上的.NET包装器自行转换为HTML。

IronPDF通过ChromePdfRenderer提供原生HTML到PDF转换,内部使用一个Chromium引擎以准确渲染HTML、CSS和JavaScript。 RenderHtmlAsPdf()方法可以直接将HTML字符串转换为PDF文档。 IronPDF还可以使用RenderHtmlFileAsPdf()渲染HTML文件。 请参阅 HTML 转 PDF 文档,了解全面的示例。


本地依赖关系移除

从 PDFium 包装器迁移到IronPDF的一个最显著的好处是消除了本地二进制管理。

之前(PDFium 包装器)— 复杂部署

MyApp/
├─── bin/
│ ├─── MyApp.dll
│   ├── PdfiumViewer.dll      # or PDFiumCore.dll / Patagames.Pdf.dll
│ ├── x86/
│ │ └── pdfium.dll
│ └── x64/
│ └── pdfium.dll
运行时
│ ├─── win-x86/native/
│ │ └── pdfium.dll
│   ├── win-x64/native/
│ │ └── pdfium.dll
│   ├── linux-x64/native/
│   │   └── libpdfium.so
│   └── osx-x64/native/
│       └── libpdfium.dylib

后 (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 转换

PdfiumViewer的Render(...)需要明确的dpiX/dpiY,而PDFium的底层渲染是基于比例的 (1.0 = 72 DPI)。IronPDF直接使用DPI:

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

文档加载方法更改

// PdfiumViewer
PdfDocument.Load(path)

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

// IronPDF
PdfDocument.FromFile(path)
' PdfiumViewer
PdfDocument.Load(path)

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

保存方法更改

//PdfiumViewer(takes a Stream)
document.Save(stream)

//IronPDF(takes a path)
pdf.SaveAs(path)
//PdfiumViewer(takes a Stream)
document.Save(stream)

//IronPDF(takes a path)
pdf.SaveAs(path)
' PdfiumViewer (takes a Stream)
document.Save(stream)

' IronPDF (takes a path)
pdf.SaveAs(path)
$vbLabelText   $csharpLabel

处置模式简化

// PdfiumViewer: explicit disposal of document and the rendered Image
using (var document = PdfDocument.Load(path))
using (var bitmap = document.Render(0, 1024, 768, 96, 96, PdfRenderFlags.None))
{
    bitmap.Save("output.png");
}

// IronPDF: Simplified
var pdf = PdfDocument.FromFile(path);
pdf.RasterizeToImageFiles("output.png");
// PdfiumViewer: explicit disposal of document and the rendered Image
using (var document = PdfDocument.Load(path))
using (var bitmap = document.Render(0, 1024, 768, 96, 96, PdfRenderFlags.None))
{
    bitmap.Save("output.png");
}

// IronPDF: Simplified
var pdf = PdfDocument.FromFile(path);
pdf.RasterizeToImageFiles("output.png");
Imports PdfiumViewer

' PdfiumViewer: explicit disposal of document and the rendered Image
Using document = PdfDocument.Load(path)
    Using bitmap = document.Render(0, 1024, 768, 96, 96, PdfRenderFlags.None)
        bitmap.Save("output.png")
    End Using
End Using

' IronPDF: Simplified
Dim pdf = PdfDocument.FromFile(path)
pdf.RasterizeToImageFiles("output.png")
$vbLabelText   $csharpLabel

删除特定平台的代码

// PDFium wrapper: required platform detection / RID-specific binaries
#if WIN64
    // Load x64 pdfium.dll
#else
    // Load x86 pdfium.dll
#endif

// IronPDF: Remove all platform-specific code
// Just use the API directly
// PDFium wrapper: required platform detection / RID-specific binaries
#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 .NET 包装器 IronPDF
加载 PDF
渲染为图像
提取文本 是(基本,每页) 是(逻辑 / 可视顺序)
页面信息
从 HTML 创建 None
从 URL 创建 None
合并 PDF 免费包装器:没有;Patagames:部分
拆分 PDF 免费包装器:没有;Patagames:部分
添加水印 非内置
页眉/页脚 非内置
表格填写 免费包装器:没有;Patagames:是
数字签名 免费包装器:没有;Patagames:是
密码保护 免费包装器:没有;Patagames:是
本地依赖性 要求 捆绑 / 由NuGet 管理
跨平台 复杂(每RID 本地二进制) 自动翻译
内存管理 手册处理 简化

迁移清单

迁移前

  • 确定使用哪个PDFium包装器 (PdfiumViewer / PdfiumViewer.Updated / PDFiumCore / Pdfium.Net.SDK)
  • 记录当前渲染尺寸 / DPI / 使用的缩放
  • 列出项目中的原生二进制位置(每个 RID)
  • 检查平台特定的加载代码
  • 确定PDF创建需求(目前是否使用不同的工具?)
  • 审查转换处置模式
  • 获取IronPDF许可证密钥

软件包变更

  • 移除PDFium封装NuGet包:PdfiumViewer, PdfiumViewer.Updated, PDFiumCore, 或 Pdfium.Net.SDK
  • x86/x64/runtimes/文件夹中删除原生pdfium.dll / .so / .dylib二进制文件
  • 移除平台特定的条件编译
  • 更新 .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、合并、水印、安全)
  • 更新文档

请注意PDFium 是 Google LLC 的一个项目,发布于 BSD-3-Clause。 PdfiumViewer, PDFiumCore 和 Pdfium.Net.SDK 是各自所有者的商标 (pvginkel, Dtronix 和 Patagames Software Ltd, 分别是)。 本网站与 Google、Chromium 项目或任何包装器作者无关联、未获其认可或支持。 所有产品名称、徽标和品牌均为各自所有者的财产。 比较仅供参考,反映撰写时公开可用的信息。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

钢铁支援团队

我们每周 5 天,每天 24 小时在线。
聊天
电子邮件
打电话给我