MIGRATION GUIDES How to Migrate from iText to IronPDF in C# Curtis Chau 發表日期:2026年1月25日 下載 IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 從 iText 遷移到 IronPDF 會將您的 .NET PDF 工作流程從需要手動建立Paragraph 、 Table和Cell物件的程式設計 API 轉變為具有完整 CSS3 和 JavaScript 支援的現代 HTML 優先方法。 本指南提供了一個全面的、逐步的遷移路徑,消除了 AGPL 許可問題,也無需專業的 .NET 開發人員單獨安裝 pdfHTML 插件。 為什麼要從 iText 遷移到 IronPDF AGPL許可陷阱 iText 為商業應用帶來了嚴重的法律和商業風險,而許多開發團隊發現這些風險時已經為時過晚: AGPL 病毒式授權:如果您在 Web 應用程式中使用 iText,AGPL 要求您開源您的整個應用程式—不僅僅是 PDF 程式碼,而是您的整個程式碼庫。 對於大多數商業軟體來說,這是行不通的。 2.沒有永久許可: iText 取消了永久許可,強制每年續訂訂閱,這增加了總擁有成本。 pdfHTML 附加元件費用: HTML 轉 PDF 功能需要 pdfHTML 附加元件,該元件需在基本授權之外另行購買。 4.複雜的許可審計:企業部署面臨許可的複雜性和審計風險,這可能會延誤專案並造成法律風險。 5.僅限程式 API: iText 需要手動使用Paragraph 、 Table 、 Cell物件建立底層 PDF——對於複雜的佈局來說,這既繁瑣又容易出錯。 6.現代 Web 渲染有限:即使使用 pdfHTML,複雜的 CSS 和 JavaScript 內容也需要付出大量額外努力。 iText 與 IronPDF 對比 特徵 iText 7 / iTextSharp IronPDF 執照 AGPL(病毒式傳播)或昂貴的訂閱 商業永續選擇權 HTML 轉 PDF 獨立的pdfHTML插件 內建 Chromium 渲染器 CSS 支援 基礎 CSS 完整的 CSS3、Flexbox 和 Grid 架構 JavaScript 沒有任何 全面執行 API範式 程序化(段落、表格、單元格) HTML優先,CSS優先 學習曲線 陡峭(PDF座標系) 對網路開發人員友好 開源風險 必須開源的網路應用程式 無病毒感染要求 定價模式 僅限訂閱用戶 永久或訂閱 對於計劃在 2025 年和 2026 年採用 .NET 10 和 C# 14 的團隊,IronPDF 憑藉其 HTML 優先的方法,利用團隊已有的 Web 開發技能,提供了一個面向未來的基礎。 遷移複雜度評估 各功能預計工作量 特徵 遷移複雜性 筆記 HTML 轉 PDF 非常低 直接替代法 合併PDF 低的 更簡單的 API 文字和圖片 低的 HTML取代了程式化 表格 中等的 HTML 表格取代了 iText 表格類 頁首/頁尾 中等的 事件處理程序 → HTML 模板 安全/加密 低的 基於屬性的 API 範式轉移 iText此次遷移的根本轉變是從程式化PDF建置轉向HTML優先渲染: iText:PdfWriter → PdfDocument → Document → Add(Paragraph) → Add(Table) IronPDF:ChromePdfRenderer → RenderHtmlAsPdf(htmlString) → SaveAs() 這個典範轉移令人耳目一新:你無需學習 iText 的物件模型,而是可以使用 Web 開發人員已經掌握的 HTML 和 CSS 技能。 開始之前 先決條件 .NET 環境: .NET Framework 4.6.2+ 或 .NET Core 3.1+ / .NET 5/6/7/8/9+ NuGet 存取權限:能夠安裝 NuGet 套件 IronPDF 許可證:請從ironpdf.com取得您的許可證密鑰。 NuGet 套件變更 # Remove iText packages dotnet remove package itext7 dotnet remove package itext7.pdfhtml dotnet remove package itextsharp # Install IronPDF dotnet add package IronPdf # Remove iText packages dotnet remove package itext7 dotnet remove package itext7.pdfhtml dotnet remove package itextsharp # 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 識別 iText 使用情況 # Find all iText references grep -r "using iText\|using iTextSharp" --include="*.cs" . grep -r "PdfWriter\|PdfDocument\|Document\|Paragraph\|Table\|Cell" --include="*.cs" . grep -r "HtmlConverter\|ConverterProperties" --include="*.cs" . # Find all iText references grep -r "using iText\|using iTextSharp" --include="*.cs" . grep -r "PdfWriter\|PdfDocument\|Document\|Paragraph\|Table\|Cell" --include="*.cs" . grep -r "HtmlConverter\|ConverterProperties" --include="*.cs" . SHELL 完整 API 參考 類別映射 iText 7 課 iTextSharp 類 IronPDF當量 PdfWriter PdfWriter ChromePdfRenderer PdfDocument Document PdfDocument Document Document ChromePdfRenderer.RenderHtmlAsPdf() Paragraph Paragraph HTML<p> ,<h1> , ETC。 Table PdfPTable HTML<table> Cell PdfPCell HTML<td> ,<th> Image Image HTML<img> PdfReader PdfReader PdfDocument.FromFile() PdfMerger 不適用 PdfDocument.Merge() 命名空間映射 iText 7 命名空間 IronPDF當量 iText.Kernel.Pdf IronPdf iText.Layout IronPdf iText.Layout.Element 使用 HTML 元素 iText.Html2Pdf IronPdf (內建) iText.IO.Image 使用 HTML<img> iText.Kernel.Utils IronPdf 程式碼遷移範例 範例 1:HTML 轉 PDF 之前(iText 7): // NuGet: Install-Package itext7 using iText.Html2pdf; using System.IO; class Program { static void Main() { string html = "<h1>Hello World</h1><p>This is a PDF from HTML.</p>"; string outputPath = "output.pdf"; using (FileStream fs = new FileStream(outputPath, FileMode.Create)) { HtmlConverter.ConvertToPdf(html, fs); } } } // NuGet: Install-Package itext7 using iText.Html2pdf; using System.IO; class Program { static void Main() { string html = "<h1>Hello World</h1><p>This is a PDF from HTML.</p>"; string outputPath = "output.pdf"; using (FileStream fs = new FileStream(outputPath, FileMode.Create)) { HtmlConverter.ConvertToPdf(html, fs); } } } $vbLabelText $csharpLabel (IronPDF 之後): // NuGet: Install-Package IronPdf using IronPdf; class Program { static void Main() { var renderer = new ChromePdfRenderer(); string html = "<h1>Hello World</h1><p>This is a PDF from HTML.</p>"; var pdf = renderer.RenderHtmlAsPdf(html); pdf.SaveAs("output.pdf"); } } // NuGet: Install-Package IronPdf using IronPdf; class Program { static void Main() { var renderer = new ChromePdfRenderer(); string html = "<h1>Hello World</h1><p>This is a PDF from HTML.</p>"; var pdf = renderer.RenderHtmlAsPdf(html); pdf.SaveAs("output.pdf"); } } $vbLabelText $csharpLabel iText 方法需要單獨的iText.Html2pdf套件(pdfHTML 插件,單獨出售),建立一個FileStream ,並將所有內容包裝在using語句中以便正確處置。 HtmlConverter.ConvertToPdf()方法直接寫入流。 IronPDF 的方法更簡潔:建立一個ChromePdfRenderer ,使用 HTML 字串呼叫RenderHtmlAsPdf() ,然後對產生的PdfDocument呼叫SaveAs() 。 沒有單獨的軟體包,沒有串流管理,而且 Chromium 渲染引擎提供了卓越的 CSS3 和 JavaScript 支援。 有關其他渲染選項,請參閱HTML 轉 PDF 文件。 範例 2:合併多個 PDF 文件 之前(iText 7): // NuGet: Install-Package itext7 using iText.Kernel.Pdf; using iText.Kernel.Utils; using System.IO; class Program { static void Main() { string outputPath = "merged.pdf"; string[] inputFiles = { "document1.pdf", "document2.pdf", "document3.pdf" }; using (PdfWriter writer = new PdfWriter(outputPath)) using (PdfDocument pdfDoc = new PdfDocument(writer)) { PdfMerger merger = new PdfMerger(pdfDoc); foreach (string file in inputFiles) { using (PdfDocument sourcePdf = new PdfDocument(new PdfReader(file))) { merger.Merge(sourcePdf, 1, sourcePdf.GetNumberOfPages()); } } } } } // NuGet: Install-Package itext7 using iText.Kernel.Pdf; using iText.Kernel.Utils; using System.IO; class Program { static void Main() { string outputPath = "merged.pdf"; string[] inputFiles = { "document1.pdf", "document2.pdf", "document3.pdf" }; using (PdfWriter writer = new PdfWriter(outputPath)) using (PdfDocument pdfDoc = new PdfDocument(writer)) { PdfMerger merger = new PdfMerger(pdfDoc); foreach (string file in inputFiles) { using (PdfDocument sourcePdf = new PdfDocument(new PdfReader(file))) { merger.Merge(sourcePdf, 1, sourcePdf.GetNumberOfPages()); } } } } } $vbLabelText $csharpLabel (IronPDF 之後): // NuGet: Install-Package IronPdf using IronPdf; using System.Collections.Generic; class Program { static void Main() { var pdfDocuments = new List<PdfDocument> { PdfDocument.FromFile("document1.pdf"), PdfDocument.FromFile("document2.pdf"), PdfDocument.FromFile("document3.pdf") }; var merged = PdfDocument.Merge(pdfDocuments); merged.SaveAs("merged.pdf"); } } // NuGet: Install-Package IronPdf using IronPdf; using System.Collections.Generic; class Program { static void Main() { var pdfDocuments = new List<PdfDocument> { PdfDocument.FromFile("document1.pdf"), PdfDocument.FromFile("document2.pdf"), PdfDocument.FromFile("document3.pdf") }; var merged = PdfDocument.Merge(pdfDocuments); merged.SaveAs("merged.pdf"); } } $vbLabelText $csharpLabel iText 合併作業需要大量的樣板程式碼:建立一個用於輸出的PdfWriter ,將其包裝在PdfDocument中,建立一個PdfMerger ,然後使用巢狀的 using 語句遍歷原始檔,每個PdfDocument和PdfReader都需要一個using語句。 您也必須使用merger.Merge(sourcePdf, 1, sourcePdf.GetNumberOfPages())來指定頁面範圍。 IronPDF 將此過程簡化為三個步驟:使用PdfDocument.FromFile()載入文檔,使用清單呼叫靜態的PdfDocument.Merge()方法,然後儲存。 整個合併操作變得易於閱讀和維護。 了解更多關於合併和拆分PDF的資訊。 範例 3:建立包含文字和圖像的 PDF 之前(iText 7): // NuGet: Install-Package itext7 using iText.Kernel.Pdf; using iText.Layout; using iText.Layout.Element; using iText.IO.Image; class Program { static void Main() { string outputPath = "document.pdf"; using (PdfWriter writer = new PdfWriter(outputPath)) using (PdfDocument pdf = new PdfDocument(writer)) using (Document document = new Document(pdf)) { document.Add(new Paragraph("Sample PDF Document")); document.Add(new Paragraph("This document contains text and an image.")); Image img = new Image(ImageDataFactory.Create("image.jpg")); img.SetWidth(200); document.Add(img); } } } // NuGet: Install-Package itext7 using iText.Kernel.Pdf; using iText.Layout; using iText.Layout.Element; using iText.IO.Image; class Program { static void Main() { string outputPath = "document.pdf"; using (PdfWriter writer = new PdfWriter(outputPath)) using (PdfDocument pdf = new PdfDocument(writer)) using (Document document = new Document(pdf)) { document.Add(new Paragraph("Sample PDF Document")); document.Add(new Paragraph("This document contains text and an image.")); Image img = new Image(ImageDataFactory.Create("image.jpg")); img.SetWidth(200); document.Add(img); } } } $vbLabelText $csharpLabel (IronPDF 之後): // NuGet: Install-Package IronPdf using IronPdf; class Program { static void Main() { var renderer = new ChromePdfRenderer(); string html = @" <h1>Sample PDF Document</h1> <p>This document contains text and an image.</p> <img src='image.jpg' width='200' />"; var pdf = renderer.RenderHtmlAsPdf(html); pdf.SaveAs("document.pdf"); } } // NuGet: Install-Package IronPdf using IronPdf; class Program { static void Main() { var renderer = new ChromePdfRenderer(); string html = @" <h1>Sample PDF Document</h1> <p>This document contains text and an image.</p> <img src='image.jpg' width='200' />"; var pdf = renderer.RenderHtmlAsPdf(html); pdf.SaveAs("document.pdf"); } } $vbLabelText $csharpLabel 這個例子最清楚地說明了典範轉移。 iText 需要: 三重嵌套using語句( PdfWriter 、 PdfDocument 、 Document ) 使用new Paragraph()為每個文字元素建立Paragraph對象 使用ImageDataFactory.Create()載入映像 分別建立Image物件並呼叫SetWidth() 對每個元素呼叫document.Add() IronPDF 使用標準 HTML:<h1>標題,<p>對於段落,以及<img>對於具有width屬性的圖像。 Web 開發人員可以立即利用他們現有的技能,設計師可以使用他們已經熟悉的 CSS 來設定文件樣式。 關鍵遷移說明 範式轉移:從程式化到 HTML 優先 此次 iText 遷移中最顯著的變化是概念上的。 iText 可以透過程式設計方式產生 PDF 檔案: // iText approach document.Add(new Paragraph("Title") .SetTextAlignment(TextAlignment.CENTER) .SetFontSize(24) .SetBold()); var table = new Table(UnitValue.CreatePercentArray(3)).UseAllAvailableWidth(); table.AddHeaderCell(new Cell().Add(new Paragraph("ID"))); table.AddHeaderCell(new Cell().Add(new Paragraph("Name"))); // ... many more lines // iText approach document.Add(new Paragraph("Title") .SetTextAlignment(TextAlignment.CENTER) .SetFontSize(24) .SetBold()); var table = new Table(UnitValue.CreatePercentArray(3)).UseAllAvailableWidth(); table.AddHeaderCell(new Cell().Add(new Paragraph("ID"))); table.AddHeaderCell(new Cell().Add(new Paragraph("Name"))); // ... many more lines $vbLabelText $csharpLabel IronPDF 用 HTML 和 CSS: // IronPDF approach string html = @" <style> h1 { text-align: center; font-size: 24px; font-weight: bold; } table { width: 100%; border-collapse: collapse; } th { background-color: #4CAF50; color: white; padding: 8px; } </style> <h1>Title</h1> <table> <tr><th>ID</th><th>Name</th></tr> </table>"; var pdf = renderer.RenderHtmlAsPdf(html); // IronPDF approach string html = @" <style> h1 { text-align: center; font-size: 24px; font-weight: bold; } table { width: 100%; border-collapse: collapse; } th { background-color: #4CAF50; color: white; padding: 8px; } </style> <h1>Title</h1> <table> <tr><th>ID</th><th>Name</th></tr> </table>"; var pdf = renderer.RenderHtmlAsPdf(html); $vbLabelText $csharpLabel AGPL授權已取消 iText 的 AGPL 授權要求您將整個 Web 應用程式開源,或購買昂貴的商業許可。 IronPDF 的商業許可允許在專有軟體中部署,而無需病毒式許可要求。 無需安裝pdfHTML插件 iText 需要單獨購買 pdfHTML 外掛程式才能將 HTML 轉換為 PDF。 IronPDF 的基礎軟體包中包含了完整的基於 Chromium 的 HTML 渲染功能。 方法替換模式 iText 圖案 IronPDF 替代方案 SetTextAlignment(TextAlignment.CENTER) CSS text-align: center SetFontSize(24) CSS font-size: 24px SetBold() CSS font-weight: bold new Table(3) HTML<table> AddHeaderCell(new Cell().Add(new Paragraph())) HTML<th> AddCell(new Cell().Add(new Paragraph())) HTML<td> 故障排除 問題 1:PdfWriter/文件模式 問題:程式碼使用了PdfWriter → PdfDocument → Document嵌套模式。 解決方案:替換為ChromePdfRenderer : // Delete this iText pattern: // using (var writer = new PdfWriter(outputPath)) // using (var pdfDoc = new PdfDocument(writer)) // using (var document = new Document(pdfDoc)) // Replace with: var renderer = new ChromePdfRenderer(); var pdf = renderer.RenderHtmlAsPdf(html); pdf.SaveAs(outputPath); // Delete this iText pattern: // using (var writer = new PdfWriter(outputPath)) // using (var pdfDoc = new PdfDocument(writer)) // using (var document = new Document(pdfDoc)) // Replace with: var renderer = new ChromePdfRenderer(); var pdf = renderer.RenderHtmlAsPdf(html); pdf.SaveAs(outputPath); $vbLabelText $csharpLabel 問題 2:未找到 HtmlConverter 問題:程式碼使用了iText.Html2pdf.HtmlConverter ,這需要 pdfHTML 外掛程式。 解決方案:使用 IronPDF 內建的 HTML 渲染功能: // iText (requires pdfHTML add-on) HtmlConverter.ConvertToPdf(html, fileStream); // IronPDF (built-in) var pdf = renderer.RenderHtmlAsPdf(html); pdf.SaveAs(outputPath); // iText (requires pdfHTML add-on) HtmlConverter.ConvertToPdf(html, fileStream); // IronPDF (built-in) var pdf = renderer.RenderHtmlAsPdf(html); pdf.SaveAs(outputPath); $vbLabelText $csharpLabel 問題 3:PdfMerger 的複雜性 問題: iText 的PdfMerger需要嵌套讀取器和頁面範圍指定。 解決方案:使用 IronPDF 的靜態合併方法: // iText merger pattern (delete this) // using (PdfDocument pdfDoc = new PdfDocument(writer)) // { // PdfMerger merger = new PdfMerger(pdfDoc); // foreach (string file in inputFiles) // { // using (PdfDocument sourcePdf = new PdfDocument(new PdfReader(file))) // { // merger.Merge(sourcePdf, 1, sourcePdf.GetNumberOfPages()); // } // } // } // IronPDF (simple) var merged = PdfDocument.Merge(pdfDocuments); merged.SaveAs("merged.pdf"); // iText merger pattern (delete this) // using (PdfDocument pdfDoc = new PdfDocument(writer)) // { // PdfMerger merger = new PdfMerger(pdfDoc); // foreach (string file in inputFiles) // { // using (PdfDocument sourcePdf = new PdfDocument(new PdfReader(file))) // { // merger.Merge(sourcePdf, 1, sourcePdf.GetNumberOfPages()); // } // } // } // IronPDF (simple) var merged = PdfDocument.Merge(pdfDocuments); merged.SaveAs("merged.pdf"); $vbLabelText $csharpLabel 遷移清單 遷移前 清點程式碼庫中所有 iText API 呼叫 識別程序化 PDF 建置模式(段落、表格、儲存格) 文件 HtmlConverter 用法(pdfHTML 外掛程式) 評估AGPL合規風險 取得 IronPDF 許可證密鑰 程式碼遷移 刪除 iText NuGet 套件: dotnet remove package itext7 安裝 IronPdf NuGet 套件: dotnet add package IronPdf 更新命名空間導入( using iText.* → using IronPdf ) 將PdfWriter / Document模式替換為ChromePdfRenderer 將Paragraph / Table / Cell轉換為 HTML 元素 將HtmlConverter.ConvertToPdf()替換為RenderHtmlAsPdf() 將合併作業更新為PdfDocument.Merge() 新增啟動時許可證金鑰初始化功能 測試 測試所有 PDF 生成路徑 驗證視覺輸出是否符合預期 使用複雜的 HTML/CSS 內容進行測試 基準性能 移民後 刪除 iText 許可證文件和引用 更新文檔 取消 iText 訂閱(如適用) 歸檔舊版 iText 程式碼 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 jsreport to IronPDF in C#How to Migrate from HiQPdf to IronP...
發表日期 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. 閱讀更多