MIGRATION GUIDES How to Migrate from PdfPig to IronPDF in C# Curtis Chau 發表日期:2026年2月1日 下載 IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 從 PdfPig 遷移到 IronPDF 可以將您的 PDF 功能從唯讀庫擴展到全面的 PDF 解決方案,該解決方案可以處理建立、操作、文字擷取和安全功能。 本指南提供了一個完整的、逐步的遷移路徑,在保留您現有提取工作流程的同時,添加了 PdfPig 無法提供的 PDF 生成、HTML 轉換、文件操作和安全功能。 為什麼要從 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 的讀取功能。 開始之前 先決條件 .NET 環境: .NET Framework 4.6.2+ 或 .NET Core 3.1+ / .NET 5/6/7/8/9+ NuGet 存取權限:能夠安裝 NuGet 套件 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 使用情況 # Find PdfPig usage grep -r "UglyToad\.PdfPig\|PdfDocument\.Open\|GetPages\(\)" --include="*.cs" . # Find page index references (may need 1→0 conversion) grep -r "GetPage(\|NumberOfPages" --include="*.cs" . # Find PdfPig usage 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(path) 從檔案載入 PdfDocument.Open(bytes) PdfDocument.FromBinaryData(bytes) 從位元組加載 PdfDocument.Open(stream) PdfDocument.FromStream(stream) 從串流中載入 using (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(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()); } } } $vbLabelText $csharpLabel (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): // PdfPig does not support HTML 轉 PDF conversion // PdfPig is a PDF reading/parsing library, not a PDF generation library // You would need to use a different library for HTML 轉 PDF conversion // PdfPig does not support HTML 轉 PDF conversion // PdfPig is a PDF reading/parsing library, not a PDF generation library // You would need to use a different library for HTML 轉 PDF conversion $vbLabelText $csharpLabel (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 (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: 基於 0 的 indexing 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: 基於 0 的 indexing 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 ✗ ✓ PDF檔案的URL ✗ ✓ 合併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 bots,將他對技術的熱愛與創意結合。 相關文章 發表日期 2026年2月1日 How to Migrate from ZetPDF to IronPDF in C# Master the migration from ZetPDF to IronPDF with this complete C# guide. Switch from a coordinate-based library to a modern HTML-to-PDF solution. Includes code examples for HTML conversion, merging PDFs, and removing PDFSharp dependencies. 閱讀更多 發表日期 2026年2月1日 How to Migrate from Scryber.Core to IronPDF in C# Master the migration from Scryber.Core to IronPDF with this complete C# guide. Switch from custom XML/HTML parsing to a modern Chromium renderer. Includes code examples for HTML conversion, URL rendering, and replacing proprietary bindings. 閱讀更多 發表日期 2026年2月1日 How to Migrate from XFINIUM.PDF to IronPDF in C# Master the migration from XFINIUM.PDF to IronPDF with this complete C# guide. Switch from manual coordinate-based positioning to declarative HTML/CSS rendering. Includes code examples for replacing graphics primitives and automatic layout. 閱讀更多 How to Migrate from PDFPrinting.NET to IronPDF in C#How to Migrate from pdforge to Iron...
發表日期 2026年2月1日 How to Migrate from ZetPDF to IronPDF in C# Master the migration from ZetPDF to IronPDF with this complete C# guide. Switch from a coordinate-based library to a modern HTML-to-PDF solution. Includes code examples for HTML conversion, merging PDFs, and removing PDFSharp dependencies. 閱讀更多
發表日期 2026年2月1日 How to Migrate from Scryber.Core to IronPDF in C# Master the migration from Scryber.Core to IronPDF with this complete C# guide. Switch from custom XML/HTML parsing to a modern Chromium renderer. Includes code examples for HTML conversion, URL rendering, and replacing proprietary bindings. 閱讀更多
發表日期 2026年2月1日 How to Migrate from XFINIUM.PDF to IronPDF in C# Master the migration from XFINIUM.PDF to IronPDF with this complete C# guide. Switch from manual coordinate-based positioning to declarative HTML/CSS rendering. Includes code examples for replacing graphics primitives and automatic layout. 閱讀更多