跳過到頁腳內容
遷移指南

如何在 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取得您的許可證密鑰。

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";
' Add at application startup (Program.vb or Startup.vb)
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;
' Before: PdfPig
Imports UglyToad.PdfPig
Imports UglyToad.PdfPig.Content
Imports UglyToad.PdfPig.DocumentLayoutAnalysis.WordExtractor

' After: IronPDF
Imports IronPdf
Imports IronPdf.Rendering
$vbLabelText   $csharpLabel

文件載入映射

PdfPig IronPDF
PdfDocument.Open(path) PdfDocument.FromFile(path)
PdfDocument.Open(bytes) PdfDocument.FromBinaryData(bytes)
PdfDocument.Open(stream) PdfDocument.FromStream(stream)
using (var doc = ...) var pdf = ...

頁面存取與內容對應

PdfPig IronPDF
document.NumberOfPages pdf.PageCount
document.GetPages() pdf.Pages
document.GetPage(1) pdf.Pages[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(certificate) 數位簽名

程式碼遷移範例

範例 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());
        }
    }
}
Imports UglyToad.PdfPig
Imports System
Imports System.Text

Class Program
    Shared Sub Main()
        Using document = PdfDocument.Open("input.pdf")
            Dim text = New StringBuilder()
            For Each page In document.GetPages()
                text.AppendLine(page.Text)
            Next
            Console.WriteLine(text.ToString())
        End Using
    End Sub
End Class
$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);
    }
}
Imports IronPdf
Imports System

Class Program
    Shared Sub Main()
        Dim pdf = PdfDocument.FromFile("input.pdf")
        Dim text As String = pdf.ExtractAllText()
        Console.WriteLine(text)
    End Sub
End Class
$vbLabelText   $csharpLabel

PdfPig 和IronPDF都提供優異的文字擷取功能。 關鍵差異在於程式碼模式。PdfPig需要一個 using 語句,以及 PdfDocument.Open() 語句,需要手動遍歷頁 GetPages() 語句,也需要一個 StringBuilder 語句來從每個文字中收集文字 @ 18

IronPDF 將此簡化為一次調用:PdfDocument.FromFile() 載入文檔,ExtractAllText() 一次返回所有文字內容。 不需要 using 語句,不需要手動迭代,不需要 StringBuilder。 請參閱 文字萃取文件,以瞭解其他選項。

範例 2:HTML 到 PDF 的轉換

之前 (PdfPig):

PdfPig不支援HTML轉PDF。PdfPig是一個 PDF 閱讀/解析函式庫,而不是一個 PDF 產生函式庫。 您需要使用不同的庫來進行 HTML 到 PDF 的轉換。

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

Class Program
    Shared Sub Main()
        Dim renderer = New ChromePdfRenderer()
        Dim pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1><p>This is a PDF from HTML</p>")
        pdf.SaveAs("output.pdf")
    End Sub
End Class
$vbLabelText   $csharpLabel

這個例子突顯了最重要的能力差距。PdfPig 明確指出它"不支援 HTML 到 PDF 的轉換",而且"是一個 PDF 讀取/解析函式庫,而不是 PDF 產生函式庫"。如果你需要用PdfPig從HTML創建PDF,你將需要使用一個完全不同的庫。

IronPDF 透過 ChromePdfRenderer 提供原生 HTML 到 PDF 的轉換。 RenderHtmlAsPdf() 方法接受 HTML 字串,並使用 Chromium 引擎將其轉換為 PDF 文檔,以便準確地渲染 HTML、CSS 和 JavaScript。 產生的 PdfDocument 可以儲存為 SaveAs(),也可以在儲存前進行進一步處理。 請參閱 HTML to 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}");
        }
    }
}
Imports UglyToad.PdfPig
Imports System

Class Program
    Shared Sub Main()
        Using document = PdfDocument.Open("input.pdf")
            Dim 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}")
        End Using
    End Sub
End Class
$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}");
    }
}
Imports IronPdf
Imports System

Class Program
    Shared Sub Main()
        Dim pdf = PdfDocument.FromFile("input.pdf")
        Dim 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}")
    End Sub
End Class
$vbLabelText   $csharpLabel

這兩個函式庫都以幾乎相同的模式提供元資料存取。PdfPig透過 document.Information 存取元數據,透過 document.NumberOfPages 存取頁數。IronPDF使用 pdf.MetaData 作為元數據,使用 pdf.PageCount 作為頁數。

遷移很簡單:將 PdfDocument.Open() 替換為 PdfDocument.FromFile(),將 document.Information 替換為 pdf.MetaData,將 @@--CO@DE-18963--@--CODE-18962--@@,將 @@--CO@DE-18963--@COD-18962--v-14-63--@CO 刪除 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;
' PdfPig:1 基於indexing
Dim firstPage = document.GetPage(1)  ' First page

' IronPDF:基於 0indexing
Dim firstPage = pdf.Pages(0)  ' First page

' Migration helper
Dim pdfPigIndex As Integer = 1
Dim ironPdfIndex As Integer = 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
Imports PdfPig
Imports IronPDF

Using document = PdfDocument.Open("input.pdf")
    ' ...
End Using

Dim 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");
Imports PdfPig
Imports IronPDF

Using document = PdfDocument.Open("input.pdf")
End Using

Dim 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;
' PdfPig: document.Information
Dim info = document.Information

' IronPDF: pdf.MetaData
Dim 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}");
' 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");
Dim pdf1 = PdfDocument.FromFile("document1.pdf")
Dim pdf2 = PdfDocument.FromFile("document2.pdf")
Dim 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>");
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";
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);
Dim signature As New PdfSignature("certificate.pfx", "password") With {
    .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 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

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

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我