MIGRATION GUIDES How to Migrate from Apache PDFBox to IronPDF Curtis Chau 發表日期:2026年1月11日 下載 IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 Apache PDFBox 是一個備受推崇的開源 Java 程式庫,用於處理 PDF 檔案。 然而,對於 .NET 開發人員來說,可用的選項是非官方的社群驅動移植版本,這些版本帶來了重大挑戰——Java 風格的 API、不完整的功能覆蓋範圍以及有限的 .NET 社群支援。 本指南提供了從 Apache PDFBox .NET 移植版到 IronPDF 的詳細遷移路徑,IronPDF 是一個專為 .NET 生態系統建置的原生 .NET PDF 程式庫。 為什麼要考慮從 Apache PDFBox .NET 連接埠遷移? 雖然 Apache PDFBox 在 Java 生態系統中表現出色,但其非官方的 .NET 移植版本卻為 .NET 開發團隊帶來了一些挑戰。 非官方港口狀態 Apache PDFBox 主要是一個 Java 函式庫。 所有 .NET 版本都是社群驅動的移植版本,缺乏 Apache 專案的官方支援。 這些移植版本通常落後於 Java 版本,可能會錯過關鍵功能、錯誤修復或安全性更新。 對於開發需要長期運行到 2025 年和 2026 年的應用程式的團隊來說,這種不確定性會帶來技術風險。 Java優先API設計 移植的 API 保留了 Java 約定,這在 .NET 程式碼中顯得很陌生。 開發人員會遇到camelCase法而不是PascalCase法,Java File物件而不是標準的 .NET 字串,以及明確close()呼叫而不是IDisposable模式。 這種認知負擔會影響開發速度和程式碼可維護性。 不具備HTML渲染功能 Apache PDFBox 是為 PDF 操作而設計的,而不是為 HTML 到 PDF 的轉換而設計的。 建立 PDF 需要手動建立頁面並進行精確的座標定位——這是一個繁瑣且容易出錯的過程,無法滿足現代文件生成的需求。 .NET 社群支援有限 圍繞 Apache PDFBox 連接埠的 .NET 生態系統還很稀少。 與擁有活躍的 .NET 社群的程式庫相比,尋找 .NET 特定問題的協助、範例或最佳實務非常困難。 潛在的 JVM 依賴項 某些 Apache PDFBox 移植版本可能需要 Java 執行時間元件,這會增加以 .NET 為中心的架構中的部署和環境管理的複雜性。 Apache PDFBox 與 IronPDF:主要區別 了解這些庫之間的根本區別有助於制定有效的遷移策略。 方面 Apache PDFBox .NET 端口 IronPDF 原生設計 以 Java 為中心的非官方 .NET 移植版 原生 .NET,專業支持 API 風格 Java 命名約定( camelCase , close() ) 慣用的 C# 命名法( PascalCase , using ) HTML渲染 不支援(手動建立頁面) 完全基於 Chrium 核心的 HTML/CSS/JS PDF 建立 手動座標定位 基於 CSS 的佈局 社群 以 Java 為中心的、資源稀少的 .NET 資源 活躍的.NET社區,下載量超過1000萬次 支援 僅限社區成員 可提供專業支持 資源清理 顯式呼叫close() using語句的 IDisposable 遷移前準備 先決條件 請確保您的環境符合以下要求: .NET Framework 4.6.2+ 或 .NET Core 3.1 / .NET 5-9 Visual Studio 2019+ 或 JetBrains Rider NuGet 套件管理器訪問 IronPDF 許可證金鑰(可在ironpdf.com提供免費試用) 審核 Apache PDFBox 使用情況 在解決方案目錄中執行以下命令,以識別所有 Apache PDFBox 引用: grep -r "apache.pdfbox\|PdfBox\|PDDocument\|PDFTextStripper" --include="*.cs" . grep -r "PdfBox\|Apache.PdfBox" --include="*.csproj" . grep -r "apache.pdfbox\|PdfBox\|PDDocument\|PDFTextStripper" --include="*.cs" . grep -r "PdfBox\|Apache.PdfBox" --include="*.csproj" . SHELL 需要預見的重大變化 類別 Apache PDFBox .NET 端口 IronPDF 移民行動 物件模型 PDDocument , PDPage PdfDocument , ChromePdfRenderer 不同的類別層次結構 PDF 建立 手動頁面/內容流 HTML渲染 重寫創建邏輯 方法風格 camelCase() (Java 風格) PascalCase() (.NET 風格) 更新方法名稱 資源清理 document.close() using語句 變更處置模式 文件訪問 Java File對象 標準 .NET 字串/流 使用 .NET 類型 文字擷取 PDFTextStripper類 pdf.ExtractAllText() 更簡單的 API 逐步遷移過程 步驟 1:更新 NuGet 套件 移除 Apache PDFBox .NET 移植包,並安裝 IronPDF: # Remove PDFBox .NET port packages dotnet remove package PdfBox dotnet remove package PDFBoxNet dotnet remove package Apache.PdfBox # Install IronPDF dotnet add package IronPdf # Remove PDFBox .NET port packages dotnet remove package PdfBox dotnet remove package PDFBoxNet dotnet remove package Apache.PdfBox # Install IronPDF dotnet add package IronPdf SHELL 步驟 2:設定許可證密鑰 在應用程式啟動時新增 IronPDF 許可證密鑰: // Add at application startup, before any IronPDF operations IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"; // Add at application startup, before any IronPDF operations IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"; $vbLabelText $csharpLabel 步驟 3:更新命名空間引用 對整個解決方案執行全域查找和取代: 尋找 替換為 using org.apache.pdfbox.pdmodel; using IronPdf; using org.apache.pdfbox.text; using IronPdf; using org.apache.pdfbox.multipdf; using IronPdf; using PdfBoxDotNet.Pdmodel; using IronPdf; using Apache.Pdfbox.PdModel; using IronPdf; 完整的 API 遷移參考 文檔操作 Apache PDFBox 方法 IronPDF 方法 筆記 PDDocument.load(path) PdfDocument.FromFile(path) 載入PDF PDDocument.load(stream) PdfDocument.FromStream(stream) 從串流中載入 new PDDocument() new ChromePdfRenderer() 用於創建PDF document.save(path) pdf.SaveAs(path) 儲存PDF document.close() using語句或Dispose() 清理 document.getNumberOfPages() pdf.PageCount 頁面計數屬性 document.getPage(index) pdf.Pages[index] 訪問頁面 document.removePage(index) pdf.RemovePages(index) 移除頁面 文字擷取 Apache PDFBox 方法 IronPDF 方法 筆記 new PDFTextStripper() 不需要 無需脫衣舞表演道具 stripper.getText(document) pdf.ExtractAllText() 完整文檔擷取 stripper.setStartPage(n) pdf.Pages[n].Text 按頁擷取 stripper.setSortByPosition(true) 自動的 內建文字排序 合併和拆分操作 Apache PDFBox 方法 IronPDF 方法 筆記 new PDFMergerUtility() 不需要 靜態合併方法 merger.addSource(file) 使用FromFile()加載 首先載入文檔 merger.mergeDocuments() PdfDocument.Merge(pdfs) 靜態合併 new Splitter() 不需要 直接頁面操作 splitter.split(document) pdf.CopyPages(indices) 複製特定頁面 安全與加密 Apache PDFBox 方法 IronPDF 方法 筆記 StandardProtectionPolicy pdf.SecuritySettings 安全性設定 policy.setUserPassword() pdf.SecuritySettings.UserPassword 使用者密碼 policy.setOwnerPassword() pdf.SecuritySettings.OwnerPassword 所有者密碼 policy.setPermissions() pdf.SecuritySettings.AllowUserXxx 權限 程式碼遷移範例 文字擷取 Apache PDFBox 最常見的操作反映了 IronPDF 提供的 API 簡化功能。 Apache PDFBox .NET 移植實作: // Apache PDFBox .NET ports are experimental and incomplete using PdfBoxDotNet.Pdmodel; using PdfBoxDotNet.Text; using System; using System.IO; class Program { static void Main() { // Note: PDFBox-dotnet has limited functionality using (var document = PDDocument.Load("document.pdf")) { var stripper = new PDFTextStripper(); string text = stripper.GetText(document); Console.WriteLine(text); } } } // Apache PDFBox .NET ports are experimental and incomplete using PdfBoxDotNet.Pdmodel; using PdfBoxDotNet.Text; using System; using System.IO; class Program { static void Main() { // Note: PDFBox-dotnet has limited functionality using (var document = PDDocument.Load("document.pdf")) { var stripper = new PDFTextStripper(); string text = stripper.GetText(document); Console.WriteLine(text); } } } $vbLabelText $csharpLabel IronPDF實現: // NuGet: Install-Package IronPdf using IronPdf; using System; class Program { static void Main() { var pdf = PdfDocument.FromFile("document.pdf"); string text = pdf.ExtractAllText(); Console.WriteLine(text); // Or extract text from specific pages string pageText = pdf.ExtractTextFromPage(0); Console.WriteLine(pageText); } } // NuGet: Install-Package IronPdf using IronPdf; using System; class Program { static void Main() { var pdf = PdfDocument.FromFile("document.pdf"); string text = pdf.ExtractAllText(); Console.WriteLine(text); // Or extract text from specific pages string pageText = pdf.ExtractTextFromPage(0); Console.WriteLine(pageText); } } $vbLabelText $csharpLabel IronPDF 完全取消了PDFTextStripper類,以單一方法呼叫取代了多步驟提取。 HTML 轉 PDF Apache PDFBox 本身不支援 HTML 到 PDF 的轉換-這代表著一個根本性的功能缺陷。 Apache PDFBox .NET 連接埠(不支援): // Apache PDFBox does not have official .NET port // Community ports like PDFBox-dotnet are incomplete // and do not support HTML to PDF conversion natively. // You would need to use additional libraries like iText or combine with HTML renderers separately. using PdfBoxDotNet.Pdmodel; using System.IO; // Note: This is NOT supported in PDFBox // PDFBox is primarily for PDF manipulation, not HTML rendering // You would need external HTML渲染 engine // Apache PDFBox does not have official .NET port // Community ports like PDFBox-dotnet are incomplete // and do not support HTML to PDF conversion natively. // You would need to use additional libraries like iText or combine with HTML renderers separately. using PdfBoxDotNet.Pdmodel; using System.IO; // Note: This is NOT supported in PDFBox // PDFBox is primarily for PDF manipulation, not HTML rendering // You would need external HTML渲染 engine $vbLabelText $csharpLabel IronPDF實現: // NuGet: Install-Package IronPdf using IronPdf; using System; class Program { static void Main() { var renderer = new ChromePdfRenderer(); var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1><p>This is HTML to PDF</p>"); 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(); var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1><p>This is HTML to PDF</p>"); pdf.SaveAs("output.pdf"); Console.WriteLine("PDF created successfully"); } } $vbLabelText $csharpLabel IronPDF 基於 Chromium 的渲染引擎提供完整的 HTML、CSS 和 JavaScript 支援。 對於進階場景,請參閱HTML 轉 PDF 文件。 合併多個PDF文件 Apache PDFBox .NET 移植實作: // Apache PDFBox .NET port attempt (incomplete support) using PdfBoxDotNet.Pdmodel; using PdfBoxDotNet.Multipdf; using System; using System.IO; class Program { static void Main() { // PDFBox-dotnet ports have incomplete API coverage var merger = new PDFMergerUtility(); merger.AddSource("document1.pdf"); merger.AddSource("document2.pdf"); merger.SetDestinationFileName("merged.pdf"); merger.MergeDocuments(); Console.WriteLine("PDFs merged"); } } // Apache PDFBox .NET port attempt (incomplete support) using PdfBoxDotNet.Pdmodel; using PdfBoxDotNet.Multipdf; using System; using System.IO; class Program { static void Main() { // PDFBox-dotnet ports have incomplete API coverage var merger = new PDFMergerUtility(); merger.AddSource("document1.pdf"); merger.AddSource("document2.pdf"); merger.SetDestinationFileName("merged.pdf"); merger.MergeDocuments(); Console.WriteLine("PDFs merged"); } } $vbLabelText $csharpLabel IronPDF實現: // NuGet: Install-Package IronPdf using IronPdf; using System; using System.Collections.Generic; class Program { static void Main() { var pdf1 = PdfDocument.FromFile("document1.pdf"); var pdf2 = PdfDocument.FromFile("document2.pdf"); var pdf3 = PdfDocument.FromFile("document3.pdf"); var merged = PdfDocument.Merge(pdf1, pdf2, pdf3); merged.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() { var pdf1 = PdfDocument.FromFile("document1.pdf"); var pdf2 = PdfDocument.FromFile("document2.pdf"); var pdf3 = PdfDocument.FromFile("document3.pdf"); var merged = PdfDocument.Merge(pdf1, pdf2, pdf3); merged.SaveAs("merged.pdf"); Console.WriteLine("PDFs merged successfully"); } } $vbLabelText $csharpLabel IronPDF 的靜態Merge方法直接接受多個文檔,而無需使用實用程式類別模式。 從零開始建立 PDF 最顯著的差異體現在建立 PDF 檔案時。 Apache PDFBox 需要手動進行座標定位。 Apache PDFBox .NET 移植實作: using org.apache.pdfbox.pdmodel; using org.apache.pdfbox.pdmodel.font; using org.apache.pdfbox.pdmodel.edit; public void CreatePdf(string outputPath) { PDDocument document = new PDDocument(); try { PDPage page = new PDPage(); document.addPage(page); PDPageContentStream contentStream = new PDPageContentStream(document, page); PDFont font = PDType1Font.HELVETICA_BOLD; contentStream.beginText(); contentStream.setFont(font, 24); contentStream.moveTextPositionByAmount(72, 700); contentStream.drawString("Hello World"); contentStream.endText(); contentStream.beginText(); contentStream.setFont(PDType1Font.HELVETICA, 12); contentStream.moveTextPositionByAmount(72, 650); contentStream.drawString("This is a paragraph of text."); contentStream.endText(); contentStream.close(); document.save(outputPath); } finally { document.close(); } } using org.apache.pdfbox.pdmodel; using org.apache.pdfbox.pdmodel.font; using org.apache.pdfbox.pdmodel.edit; public void CreatePdf(string outputPath) { PDDocument document = new PDDocument(); try { PDPage page = new PDPage(); document.addPage(page); PDPageContentStream contentStream = new PDPageContentStream(document, page); PDFont font = PDType1Font.HELVETICA_BOLD; contentStream.beginText(); contentStream.setFont(font, 24); contentStream.moveTextPositionByAmount(72, 700); contentStream.drawString("Hello World"); contentStream.endText(); contentStream.beginText(); contentStream.setFont(PDType1Font.HELVETICA, 12); contentStream.moveTextPositionByAmount(72, 650); contentStream.drawString("This is a paragraph of text."); contentStream.endText(); contentStream.close(); document.save(outputPath); } finally { document.close(); } } $vbLabelText $csharpLabel IronPDF實現: using IronPdf; public void CreatePdf(string outputPath) { var renderer = new ChromePdfRenderer(); string html = @" <html> <head> <style> body { font-family: Helvetica, Arial, sans-serif; margin: 1in; } h1 { font-size: 24pt; font-weight: bold; } p { font-size: 12pt; } </style> </head> <body> <h1>Hello World</h1> <p>This is a paragraph of text.</p> </body> </html>"; using var pdf = renderer.RenderHtmlAsPdf(html); pdf.SaveAs(outputPath); } using IronPdf; public void CreatePdf(string outputPath) { var renderer = new ChromePdfRenderer(); string html = @" <html> <head> <style> body { font-family: Helvetica, Arial, sans-serif; margin: 1in; } h1 { font-size: 24pt; font-weight: bold; } p { font-size: 12pt; } </style> </head> <body> <h1>Hello World</h1> <p>This is a paragraph of text.</p> </body> </html>"; using var pdf = renderer.RenderHtmlAsPdf(html); pdf.SaveAs(outputPath); } $vbLabelText $csharpLabel 基於 HTML/CSS 的建立方式省去了座標計算、字型管理和內容流操作。 新增密碼保護 Apache PDFBox .NET 移植實作: using org.apache.pdfbox.pdmodel; using org.apache.pdfbox.pdmodel.encryption; public void ProtectPdf(string inputPath, string outputPath, string password) { PDDocument document = PDDocument.load(new File(inputPath)); try { AccessPermission ap = new AccessPermission(); ap.setCanPrint(true); ap.setCanExtractContent(false); StandardProtectionPolicy spp = new StandardProtectionPolicy(password, password, ap); spp.setEncryptionKeyLength(128); document.protect(spp); document.save(outputPath); } finally { document.close(); } } using org.apache.pdfbox.pdmodel; using org.apache.pdfbox.pdmodel.encryption; public void ProtectPdf(string inputPath, string outputPath, string password) { PDDocument document = PDDocument.load(new File(inputPath)); try { AccessPermission ap = new AccessPermission(); ap.setCanPrint(true); ap.setCanExtractContent(false); StandardProtectionPolicy spp = new StandardProtectionPolicy(password, password, ap); spp.setEncryptionKeyLength(128); document.protect(spp); document.save(outputPath); } finally { document.close(); } } $vbLabelText $csharpLabel IronPDF實現: using IronPdf; public void ProtectPdf(string inputPath, string outputPath, string password) { using var pdf = PdfDocument.FromFile(inputPath); pdf.SecuritySettings.UserPassword = password; pdf.SecuritySettings.OwnerPassword = password; pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.FullPrintRights; pdf.SecuritySettings.AllowUserCopyPasteContent = false; pdf.SaveAs(outputPath); } using IronPdf; public void ProtectPdf(string inputPath, string outputPath, string password) { using var pdf = PdfDocument.FromFile(inputPath); pdf.SecuritySettings.UserPassword = password; pdf.SecuritySettings.OwnerPassword = password; pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.FullPrintRights; pdf.SecuritySettings.AllowUserCopyPasteContent = false; pdf.SaveAs(outputPath); } $vbLabelText $csharpLabel IronPDF 使用強型別屬性,而不是單獨的權限和策略物件。 添加浮水印 Apache PDFBox .NET 移植實作: using org.apache.pdfbox.pdmodel; using org.apache.pdfbox.pdmodel.edit; using org.apache.pdfbox.pdmodel.font; public void AddWatermark(string inputPath, string outputPath, string watermarkText) { PDDocument document = PDDocument.load(new File(inputPath)); try { PDFont font = PDType1Font.HELVETICA_BOLD; for (int i = 0; i < document.getNumberOfPages(); i++) { PDPage page = document.getPage(i); PDPageContentStream cs = new PDPageContentStream( document, page, PDPageContentStream.AppendMode.APPEND, true, true); cs.beginText(); cs.setFont(font, 72); cs.setNonStrokingColor(200, 200, 200); cs.setTextMatrix(Matrix.getRotateInstance(Math.toRadians(45), 200, 400)); cs.showText(watermarkText); cs.endText(); cs.close(); } document.save(outputPath); } finally { document.close(); } } using org.apache.pdfbox.pdmodel; using org.apache.pdfbox.pdmodel.edit; using org.apache.pdfbox.pdmodel.font; public void AddWatermark(string inputPath, string outputPath, string watermarkText) { PDDocument document = PDDocument.load(new File(inputPath)); try { PDFont font = PDType1Font.HELVETICA_BOLD; for (int i = 0; i < document.getNumberOfPages(); i++) { PDPage page = document.getPage(i); PDPageContentStream cs = new PDPageContentStream( document, page, PDPageContentStream.AppendMode.APPEND, true, true); cs.beginText(); cs.setFont(font, 72); cs.setNonStrokingColor(200, 200, 200); cs.setTextMatrix(Matrix.getRotateInstance(Math.toRadians(45), 200, 400)); cs.showText(watermarkText); cs.endText(); cs.close(); } document.save(outputPath); } finally { document.close(); } } $vbLabelText $csharpLabel IronPDF實現: using IronPdf; public void AddWatermark(string inputPath, string outputPath, string watermarkText) { using var pdf = PdfDocument.FromFile(inputPath); pdf.ApplyWatermark( $"<h1 style='color:lightgray;font-size:72px;'>{watermarkText}</h1>", rotation: 45, opacity: 50); pdf.SaveAs(outputPath); } using IronPdf; public void AddWatermark(string inputPath, string outputPath, string watermarkText) { using var pdf = PdfDocument.FromFile(inputPath); pdf.ApplyWatermark( $"<h1 style='color:lightgray;font-size:72px;'>{watermarkText}</h1>", rotation: 45, opacity: 50); pdf.SaveAs(outputPath); } $vbLabelText $csharpLabel IronPDF基於 HTML 的浮水印功能消除了頁面迭代和矩陣計算。 URL 轉 PDF Apache PDFBox 不支援 URL 轉 PDF 轉換。 IronPDF 提供原生支援: using IronPdf; public void ConvertUrlToPdf(string url, string outputPath) { var renderer = new ChromePdfRenderer(); using var pdf = renderer.RenderUrlAsPdf(url); pdf.SaveAs(outputPath); } using IronPdf; public void ConvertUrlToPdf(string url, string outputPath) { var renderer = new ChromePdfRenderer(); using var pdf = renderer.RenderUrlAsPdf(url); pdf.SaveAs(outputPath); } $vbLabelText $csharpLabel 有關完整的 URL 轉換選項,請參閱URL 轉 PDF 文件。 頁首和頁尾 Apache PDFBox 需要手動在每一頁上定位,沒有內建的頁首/頁尾支援。 IronPDF 提供聲明式配置: using IronPdf; public void CreatePdfWithHeaderFooter(string html, string outputPath) { var renderer = new ChromePdfRenderer(); renderer.RenderingOptions.TextHeader = new TextHeaderFooter { CenterText = "Document Title", FontSize = 12 }; renderer.RenderingOptions.TextFooter = new TextHeaderFooter { CenterText = "Page {page} of {total-pages}", FontSize = 10 }; using var pdf = renderer.RenderHtmlAsPdf(html); pdf.SaveAs(outputPath); } using IronPdf; public void CreatePdfWithHeaderFooter(string html, string outputPath) { var renderer = new ChromePdfRenderer(); renderer.RenderingOptions.TextHeader = new TextHeaderFooter { CenterText = "Document Title", FontSize = 12 }; renderer.RenderingOptions.TextFooter = new TextHeaderFooter { CenterText = "Page {page} of {total-pages}", FontSize = 10 }; using var pdf = renderer.RenderHtmlAsPdf(html); pdf.SaveAs(outputPath); } $vbLabelText $csharpLabel 有關進階佈局,請參閱頁首和頁尾文件。 ASP.NET Core 集成 IronPDF 可與現代 .NET Web 應用程式自然整合: [HttpPost] public IActionResult GeneratePdf([FromBody] ReportRequest request) { var renderer = new ChromePdfRenderer(); using var pdf = renderer.RenderHtmlAsPdf(request.Html); return File(pdf.BinaryData, "application/pdf", "report.pdf"); } [HttpPost] public IActionResult GeneratePdf([FromBody] ReportRequest request) { var renderer = new ChromePdfRenderer(); using var pdf = renderer.RenderHtmlAsPdf(request.Html); return File(pdf.BinaryData, "application/pdf", "report.pdf"); } $vbLabelText $csharpLabel 非同步支援 Apache PDFBox 連接埠不支援非同步操作。 IronPDF 提供完整的非同步/等待功能: using IronPdf; public async Task<byte[]> GeneratePdfAsync(string html) { var renderer = new ChromePdfRenderer(); using var pdf = await renderer.RenderHtmlAsPdfAsync(html); return pdf.BinaryData; } using IronPdf; public async Task<byte[]> GeneratePdfAsync(string html) { var renderer = new ChromePdfRenderer(); using var pdf = await renderer.RenderHtmlAsPdfAsync(html); return pdf.BinaryData; } $vbLabelText $csharpLabel 依賴注入配置 public interface IPdfService { Task<byte[]> GeneratePdfAsync(string html); string ExtractText(string pdfPath); } public class IronPdfService : IPdfService { private readonly ChromePdfRenderer _renderer; public IronPdfService() { _renderer = new ChromePdfRenderer(); _renderer.RenderingOptions.PaperSize = PdfPaperSize.A4; } public async Task<byte[]> GeneratePdfAsync(string html) { using var pdf = await _renderer.RenderHtmlAsPdfAsync(html); return pdf.BinaryData; } public string ExtractText(string pdfPath) { using var pdf = PdfDocument.FromFile(pdfPath); return pdf.ExtractAllText(); } } public interface IPdfService { Task<byte[]> GeneratePdfAsync(string html); string ExtractText(string pdfPath); } public class IronPdfService : IPdfService { private readonly ChromePdfRenderer _renderer; public IronPdfService() { _renderer = new ChromePdfRenderer(); _renderer.RenderingOptions.PaperSize = PdfPaperSize.A4; } public async Task<byte[]> GeneratePdfAsync(string html) { using var pdf = await _renderer.RenderHtmlAsPdfAsync(html); return pdf.BinaryData; } public string ExtractText(string pdfPath) { using var pdf = PdfDocument.FromFile(pdfPath); return pdf.ExtractAllText(); } } $vbLabelText $csharpLabel 效能最佳化 記憶體使用比較 設想 Apache PDFBox .NET 端口 IronPDF 筆記 文字擷取 約 80 MB 約 50 MB IronPDF 更有效率 PDF 建立 約100 MB 約 60 MB HTML渲染優化 批量(100 個 PDF 文件) 高(手動清理) 約100 MB 使用using語句 優化技巧 使用using語句: // 自動的 cleanup with IDisposable pattern using var pdf = PdfDocument.FromFile(path); // 自動的 cleanup with IDisposable pattern using var pdf = PdfDocument.FromFile(path); $vbLabelText $csharpLabel 批次操作中重複使用渲染器: var renderer = new ChromePdfRenderer(); foreach (var html in htmlList) { using var pdf = renderer.RenderHtmlAsPdf(html); pdf.SaveAs($"output_{i}.pdf"); } var renderer = new ChromePdfRenderer(); foreach (var html in htmlList) { using var pdf = renderer.RenderHtmlAsPdf(html); pdf.SaveAs($"output_{i}.pdf"); } $vbLabelText $csharpLabel 在 Web 應用程式中使用非同步: using var pdf = await renderer.RenderHtmlAsPdfAsync(html); using var pdf = await renderer.RenderHtmlAsPdfAsync(html); $vbLabelText $csharpLabel 解決常見遷移問題 問題:找不到 Java 風格的方法名稱 將camelCase Java 方法替換為PascalCase .NET 等效方法: // PDFBox: stripper.getText(document) // IronPDF: pdf.ExtractAllText() // PDFBox: document.getNumberOfPages() // IronPDF: pdf.PageCount // PDFBox: stripper.getText(document) // IronPDF: pdf.ExtractAllText() // PDFBox: document.getNumberOfPages() // IronPDF: pdf.PageCount $vbLabelText $csharpLabel 問題:缺少close()方法 IronPDF 使用IDisposable模式: // PDFBox document.close(); // IronPDF using var pdf = PdfDocument.FromFile(path); // 自動的 disposal at end of scope // PDFBox document.close(); // IronPDF using var pdf = PdfDocument.FromFile(path); // 自動的 disposal at end of scope $vbLabelText $csharpLabel 問題:沒有等效的PDFTextStripper 文本提取簡化為單一方法: // IronPDF: Just call ExtractAllText() string text = pdf.ExtractAllText(); // Per-page extraction: string pageText = pdf.Pages[0].Text; // IronPDF: Just call ExtractAllText() string text = pdf.ExtractAllText(); // Per-page extraction: string pageText = pdf.Pages[0].Text; $vbLabelText $csharpLabel 問題:未找到PDFMergerUtility 使用靜態的Merge方法: // IronPDF uses static Merge var merged = PdfDocument.Merge(pdf1, pdf2, pdf3); // IronPDF uses static Merge var merged = PdfDocument.Merge(pdf1, pdf2, pdf3); $vbLabelText $csharpLabel 遷移後檢查清單 程式碼遷移完成後,請驗證以下內容: 運行所有現有的單元測試和整合測試 將 PDF 輸出結果與先前的版本進行直觀比較 測試文字擷取準確率 驗證許可證是否正常運作( IronPdf.License.IsLicensed ) 與先前實現的性能基準測試 更新 CI/CD 管線依賴項 為您的開發團隊記錄新的模式 讓您的 PDF 基礎架構面向未來 隨著 .NET 10 即將到來,C# 14 也引入了新的語言特性,選擇原生 .NET PDF 程式庫可確保與不斷發展的執行時間功能相容。 IronPDF 致力於支援最新的 .NET 版本,這意味著隨著專案擴展到 2025 年和 2026 年,您的遷移投資將獲得回報。 其他資源 IronPDF 文件 HTML 轉 PDF 教學課程 API 參考 NuGet 套件 -授權選項 從 Apache PDFBox .NET 連接埠遷移到 IronPDF 會將您的 PDF 程式碼庫從 Java 風格的模式轉換為慣用的 C# 模式。 從手動座標定位到 HTML/CSS 渲染的轉變,結合原生非同步支援和現代 .NET 集成,可提供更簡潔、更易於維護的程式碼,並有專業的支援為您的生產應用程式保駕護航。 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 Api2pdf to IronPDF in C#How to Migrate from Adobe PDF Libra...
發表日期 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. 閱讀更多