跳至页脚内容
迁移指南

如何用 C# 从 PdfPig 迁移到 IronPDF

从PdfPig迁移到IronPDF可将您的 PDF 功能从纯粹的阅读库扩展为一个全面的 PDF 解决方案,可处理创建、操作、文本提取和安全功能。 本指南提供了一个完整的、循序渐进的迁移路径,在保留现有提取工作流的同时,增加了 PDF 生成、HTML 转换、文档操作以及PdfPig无法提供的安全功能。

为何从PdfPig迁移到 IronPDF.

了解 PdfPig

PdfPig 是一个专门为 C# 设计的开源 PDF 阅读和提取库。 作为声誉卓著的 Apache PDFBox 项目的一个分支,该库允许开发人员非常精确地访问 PDF 内容。 虽然PdfPig在提取能力方面表现出色,但与市场上更全面的库相比,它的范围很大程度上受到了限制。

PdfPig 为开发人员提供了从 PDF 文件中提取文本、图像、表单数据和元数据的可靠工具。 因此,它适合主要侧重于文档分析和数据挖掘的应用程序。 然而,PdfPig 的功能从根本上来说仅限于解析现有文档。

仅限阅读的限制

PdfPig 专注于 PDF 阅读。 当您的应用程序需要扩展到提取之外时,PdfPig 将无能为力:

1.无法生成 PDF:无法从 HTML、URL 或通过编程方式创建 PDF。

2.不支持 HTML 转 PDF:PdfPig是一个 PDF 读取/解析库,而不是一个 PDF 生成库。 您需要使用不同的库将 HTML 转换为 PDF。

3.不支持文档操作:无法合并、拆分或修改 PDF 文件。

4.无安全功能:无法添加密码、加密或数字签名。

5.无水印/印章:无法向现有文档添加视觉叠加层。

6.无法填写表单:无法通过编程方式填写 PDF 表单。

PdfPig与IronPDF对比

特征 PdfPig IronPDF
许可 开源(Apache 2.0) 商业翻译
PDF阅读/提取 出色的 出色的
PDF 生成 有限的 综合性
HTML 到 PDF 不支持 支持
文本提取 出色的 出色的
PDF 操作 不支持 合并、拆分、旋转
水印 不支持 全面支持
安全/加密 不支持 全面支持
支持和文档 社区支持 专项支持
页面索引 1 基于 基于 0

IronPDF 支持创建、阅读、编辑和签署 PDF 的全套功能。 这种多功能性使开发人员能够自始至终管理 PDF 文件。 对于计划在 2025 年和 2026 年之前采用 .NET 10 和 C# 14 的团队,IronPDF 提供了一个完整的 PDF 生命周期解决方案,其功能超出了PdfPig的阅读功能。


开始之前

前提条件

  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 PdfPig
dotnet remove package PdfPig

# Install IronPDF
dotnet add package IronPdf
# Remove PdfPig
dotnet remove package PdfPig

# 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

识别PdfPig的用法

# FindPdfPigusage
grep -r "UglyToad\.PdfPig\|PdfDocument\.Open\|GetPages\(\)" --include="*.cs" .

# Find page index references (may need 1→0 conversion)
grep -r "GetPage(\|NumberOfPages" --include="*.cs" .
# FindPdfPigusage
grep -r "UglyToad\.PdfPig\|PdfDocument\.Open\|GetPages\(\)" --include="*.cs" .

# Find page index references (may need 1→0 conversion)
grep -r "GetPage(\|NumberOfPages" --include="*.cs" .
SHELL

完整的 API 参考

命名空间变更

// Before: PdfPig
using UglyToad.PdfPig;
using UglyToad.PdfPig.Content;
using UglyToad.PdfPig.DocumentLayoutAnalysis.WordExtractor;

// After: IronPDF
using IronPdf;
using IronPdf.Rendering;
// Before: PdfPig
using UglyToad.PdfPig;
using UglyToad.PdfPig.Content;
using UglyToad.PdfPig.DocumentLayoutAnalysis.WordExtractor;

// After: IronPDF
using IronPdf;
using IronPdf.Rendering;
$vbLabelText   $csharpLabel

文档加载映射

PdfPig IronPDF 备注
PdfDocument.Open(path) PdfDocument.FromFile(路径) 从文件加载
PdfDocument.Open(字节) PdfDocument.FromBinaryData(字节) 从字节加载
PdfDocument.Open(stream) PdfDocument.FromStream(流) 从流加载
使用(var doc = ...) var pdf = ... IronPdf 不需要使用

页面访问和属性映射

PdfPig IronPDF 备注
document.NumberOfPages pdf.PageCount 总页数
document.GetPages() pdf.Pages 页面收集
document.GetPage(1) pdf.Pages[0] 单页(注:以 1 为基础,以 0 为基础)

文本提取映射

PdfPig IronPDF 备注
page.Text pdf.Pages[i].Text 页面文本
page.GetWords() pdf.ExtractTextFromPage(i) 页面中的词语/文本
(manual loop) pdf.ExtractAllText() 一次性翻译所有文本

元数据访问映射

PdfPig IronPDF 备注
document.Information.Title pdf.MetaData.Title 文件标题
document.Information.Author pdf.MetaData.Author 作者
document.Information.Subject pdf.MetaData.Subject 翻译主题
document.Information.Creator pdf.MetaData.Creator 创作者
document.Information.Producer pdf.MetaData.Producer 制作人

PdfPig不具备的新功能

IronPdf 特点 说明
renderer.RenderHtmlAsPdf(html) HTML 到 PDF 的转换
renderer.RenderUrlAsPdf(url) URL 到 PDF 的转换
PdfDocument.Merge(pdfs) 合并多个 PDF
pdf.ApplyWatermark(html) 添加水印
pdf.SecuritySettings.UserPassword 密码保护
pdf.Sign(证书) 数字签名

代码迁移示例

示例 1:从 PDF 中提取文本

之前 (PdfPig):

// NuGet: Install-Package PdfPig
using UglyToad.PdfPig;
using System;
using System.Text;

class Program
{
    static void Main()
    {
        using (var document = PdfDocument.Open("input.pdf"))
        {
            var text = new StringBuilder();
            foreach (var page in document.GetPages())
            {
                text.AppendLine(page.Text);
            }
            Console.WriteLine(text.ToString());
        }
    }
}
// NuGet: Install-Package PdfPig
using UglyToad.PdfPig;
using System;
using System.Text;

class Program
{
    static void Main()
    {
        using (var document = PdfDocument.Open("input.pdf"))
        {
            var text = new StringBuilder();
            foreach (var page in document.GetPages())
            {
                text.AppendLine(page.Text);
            }
            Console.WriteLine(text.ToString());
        }
    }
}
$vbLabelText   $csharpLabel

After (IronPDF):

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

class Program
{
    static void Main()
    {
        var pdf = PdfDocument.FromFile("input.pdf");
        string text = pdf.ExtractAllText();
        Console.WriteLine(text);
    }
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;

class Program
{
    static void Main()
    {
        var pdf = PdfDocument.FromFile("input.pdf");
        string text = pdf.ExtractAllText();
        Console.WriteLine(text);
    }
}
$vbLabelText   $csharpLabel

PdfPig 和IronPDF都具有出色的文本提取功能。 关键区别在于代码模式。PdfPig需要使用 PdfDocument.Open()using 语句,使用 GetPages() 手动迭代页面,以及使用 StringBuilder 从每个page.Text属性中积累文本。

IronPdf 将此简化为一次调用:PdfDocument.FromFile() 加载文档,ExtractAllText() 一次性返回所有文本内容。 无需 using 语句,无需手动迭代,无需 StringBuilder。 有关其他选项,请参阅 文本提取文档

示例 2:HTML 到 PDF 的转换

之前 (PdfPig):

//PdfPigdoes not supportHTML 至 PDFconversion
//PdfPigis a PDF reading/parsing library, not a PDF generation library
// You would need to use a different library forHTML 至 PDFconversion
//PdfPigdoes not supportHTML 至 PDFconversion
//PdfPigis a PDF reading/parsing library, not a PDF generation library
// You would need to use a different library forHTML 至 PDFconversion
$vbLabelText   $csharpLabel

After (IronPDF):

// NuGet: Install-Package IronPdf
using IronPdf;

class Program
{
    static void Main()
    {
        var renderer = new ChromePdfRenderer();
        var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1><p>This is a PDF from HTML</p>");
        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><p>This is a PDF from HTML</p>");
        pdf.SaveAs("output.pdf");
    }
}
$vbLabelText   $csharpLabel

这个例子凸显了最重要的能力差距。PdfPig 明确指出它 "不支持 HTML 到 PDF 的转换",并且 "是一个 PDF 阅读/解析库,而不是一个 PDF 生成库"。如果你需要用PdfPig从 HTML 创建 PDF,你需要使用完全不同的库。

IronPDF 通过 ChromePdfRenderer 提供原生 HTML 到 PDF 的转换。 RenderHtmlAsPdf()方法接受 HTML 字符串,并使用内部的 Chromium 引擎将其转换为 PDF 文档,以准确呈现 HTML、CSS 和 JavaScript。 生成的 PdfDocument 可使用 SaveAs() 保存,或在保存前进一步处理。 请参阅 HTML 转 PDF 文档,了解全面的示例。

示例 3:读取 PDF 元数据

之前 (PdfPig):

// NuGet: Install-Package PdfPig
using UglyToad.PdfPig;
using System;

class Program
{
    static void Main()
    {
        using (var document = PdfDocument.Open("input.pdf"))
        {
            var info = document.Information;
            Console.WriteLine($"Title: {info.Title}");
            Console.WriteLine($"Author: {info.Author}");
            Console.WriteLine($"Subject: {info.Subject}");
            Console.WriteLine($"Creator: {info.Creator}");
            Console.WriteLine($"Producer: {info.Producer}");
            Console.WriteLine($"Number of Pages: {document.NumberOfPages}");
        }
    }
}
// NuGet: Install-Package PdfPig
using UglyToad.PdfPig;
using System;

class Program
{
    static void Main()
    {
        using (var document = PdfDocument.Open("input.pdf"))
        {
            var info = document.Information;
            Console.WriteLine($"Title: {info.Title}");
            Console.WriteLine($"Author: {info.Author}");
            Console.WriteLine($"Subject: {info.Subject}");
            Console.WriteLine($"Creator: {info.Creator}");
            Console.WriteLine($"Producer: {info.Producer}");
            Console.WriteLine($"Number of Pages: {document.NumberOfPages}");
        }
    }
}
$vbLabelText   $csharpLabel

After (IronPDF):

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

class Program
{
    static void Main()
    {
        var pdf = PdfDocument.FromFile("input.pdf");
        var info = pdf.MetaData;
        Console.WriteLine($"Title: {info.Title}");
        Console.WriteLine($"Author: {info.Author}");
        Console.WriteLine($"Subject: {info.Subject}");
        Console.WriteLine($"Creator: {info.Creator}");
        Console.WriteLine($"Producer: {info.Producer}");
        Console.WriteLine($"Number of Pages: {pdf.PageCount}");
    }
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;

class Program
{
    static void Main()
    {
        var pdf = PdfDocument.FromFile("input.pdf");
        var info = pdf.MetaData;
        Console.WriteLine($"Title: {info.Title}");
        Console.WriteLine($"Author: {info.Author}");
        Console.WriteLine($"Subject: {info.Subject}");
        Console.WriteLine($"Creator: {info.Creator}");
        Console.WriteLine($"Producer: {info.Producer}");
        Console.WriteLine($"Number of Pages: {pdf.PageCount}");
    }
}
$vbLabelText   $csharpLabel

这两个库都以几乎相同的模式提供元数据访问。PdfPig通过 document.Information 访问元数据,通过document.NumberOfPages访问页数。 IronPdf 使用 pdf.MetaData 表示元数据,使用pdf.PageCount表示页数。

迁移过程非常简单:将 PdfDocument.Open() 替换为 PdfDocument.FromFile() ;将 document.Information 替换为 pdf.MetaData ;将document.NumberOfPages替换为pdf.PageCount。 删除 using 语句包装器,因为IronPDF不需要它。


关键迁移说明

页面索引更改

PdfPig 使用基于 1 的索引; IronPdf 使用基于 0:

// PdfPig:1 基于indexing
var firstPage = document.GetPage(1);  // First page

// IronPDF:基于 0indexing
var firstPage = pdf.Pages[0];  // First page

// Migration helper
int pdfPigIndex = 1;
int ironPdfIndex = pdfPigIndex - 1;
// PdfPig:1 基于indexing
var firstPage = document.GetPage(1);  // First page

// IronPDF:基于 0indexing
var firstPage = pdf.Pages[0];  // First page

// Migration helper
int pdfPigIndex = 1;
int ironPdfIndex = pdfPigIndex - 1;
$vbLabelText   $csharpLabel

不要求使用说明

// PdfPig: Requires using for disposal
using (var document = PdfDocument.Open("input.pdf"))
{
    // ...
}

// IronPDF: No using required (but can use for cleanup)
var pdf = PdfDocument.FromFile("input.pdf");
// ...
// pdf.Dispose(); // Optional
// PdfPig: Requires using for disposal
using (var document = PdfDocument.Open("input.pdf"))
{
    // ...
}

// IronPDF: No using required (but can use for cleanup)
var pdf = PdfDocument.FromFile("input.pdf");
// ...
// pdf.Dispose(); // Optional
$vbLabelText   $csharpLabel

文档加载更改

// PdfPig: PdfDocument.Open()
using (var document = PdfDocument.Open("input.pdf"))

// IronPDF: PdfDocument.FromFile()
var pdf = PdfDocument.FromFile("input.pdf");
// PdfPig: PdfDocument.Open()
using (var document = PdfDocument.Open("input.pdf"))

// IronPDF: PdfDocument.FromFile()
var pdf = PdfDocument.FromFile("input.pdf");
$vbLabelText   $csharpLabel

元数据属性名称更改

// PdfPig: document.Information
var info = document.Information;

// IronPDF: pdf.MetaData
var info = pdf.MetaData;
// PdfPig: document.Information
var info = document.Information;

// IronPDF: pdf.MetaData
var info = pdf.MetaData;
$vbLabelText   $csharpLabel

页数属性更改

// PdfPig: document.NumberOfPages
Console.WriteLine($"Pages: {document.NumberOfPages}");

// IronPDF: pdf.PageCount
Console.WriteLine($"Pages: {pdf.PageCount}");
// PdfPig: document.NumberOfPages
Console.WriteLine($"Pages: {document.NumberOfPages}");

// IronPDF: pdf.PageCount
Console.WriteLine($"Pages: {pdf.PageCount}");
$vbLabelText   $csharpLabel

迁移后的新功能

迁移到IronPDF后,您将获得PdfPig无法提供的功能:

PDF 合并

var pdf1 = PdfDocument.FromFile("document1.pdf");
var pdf2 = PdfDocument.FromFile("document2.pdf");
var merged = PdfDocument.Merge(pdf1, pdf2);
merged.SaveAs("merged.pdf");
var pdf1 = PdfDocument.FromFile("document1.pdf");
var pdf2 = PdfDocument.FromFile("document2.pdf");
var merged = PdfDocument.Merge(pdf1, pdf2);
merged.SaveAs("merged.pdf");
$vbLabelText   $csharpLabel

水印

pdf.ApplyWatermark("<h2 style='color:red;'>CONFIDENTIAL</h2>");
pdf.ApplyWatermark("<h2 style='color:red;'>CONFIDENTIAL</h2>");
$vbLabelText   $csharpLabel

密码保护

pdf.SecuritySettings.UserPassword = "userpassword";
pdf.SecuritySettings.OwnerPassword = "ownerpassword";
pdf.SecuritySettings.UserPassword = "userpassword";
pdf.SecuritySettings.OwnerPassword = "ownerpassword";
$vbLabelText   $csharpLabel

数字签名

var signature = new PdfSignature("certificate.pfx", "password")
{
    SigningContact = "support@company.com",
    SigningReason = "Document Approval"
};
pdf.Sign(signature);
var signature = new PdfSignature("certificate.pfx", "password")
{
    SigningContact = "support@company.com",
    SigningReason = "Document Approval"
};
pdf.Sign(signature);
$vbLabelText   $csharpLabel

功能对比摘要

特征 PdfPig IronPDF
文本提取
元数据访问
图像提取
PDF 创建 有限的
HTML 至 PDF
URL 至 PDF
合并 PDF
拆分 PDF
水印
表格填写
密码保护
数字签名
词位数据

迁移清单

迁移前

  • 清点代码库中所有PdfPig的使用情况
  • 确定是否需要词级位置数据(考虑混合方法)
  • 注意所有页面索引引用(需要将基于 1 的索引转换为基于 0 的索引)
  • 规划IronPDF许可证密钥存储(建议使用环境变量)
  • 先使用IronPDF试用许可证进行测试

软件包变更

  • 移除PdfPig NuGet 包: dotnet remove package PdfPig 安装IronPdf NuGet 包: dotnet add package IronPdf

代码更改

  • 更新命名空间导入( using UglyToad.PdfPig;using IronPdf;
  • PdfDocument.Open()替换为PdfDocument.FromFile()
  • document.Information替换为pdf.MetaData
  • document.NumberOfPages替换为pdf.PageCount
  • 将页面索引从基于 1 的索引转换为基于 0 的索引
  • 删除using语句(可选,IronPDF 不需要它们)
  • 在应用程序启动时添加IronPDF许可证密钥

后迁移

  • 测试文本提取输出符合预期
  • 测试所有 PDF 生成场景
  • 根据需要添加新功能(合并、水印、安全)
  • 如果部署到 Linux,请安装 Linux 依赖项

Curtis Chau
技术作家

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

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