MIGRATION GUIDES How to Migrate from MigraDoc 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 上分享 複製連結 電子郵件文章 從 MigraDoc 遷移到 IronPDF 會將您的 .NET PDF 工作流程從需要手動逐個元素建立的冗長程式化文件模型轉變為利用現有 Web 開發技能的現代 HTML/CSS 方法。 本指南提供了一個全面的、循序漸進的遷移路徑,消除了專業 .NET 開發人員學習 MigraDoc 專有文件物件模型的陡峭學習曲線。 為什麼要從 MigraDoc 遷移到 IronPDF MigraDoc 的挑戰 MigraDoc雖然功能強大,可用於程式化產生PDF,但它存在一些根本性的局限性,會影響現代開發工作流程: 1.不支援 HTML: MigraDoc 不直接支援 HTML。 您必須使用Document 、 Section 、 Paragraph和Table物件手動逐個元素建立文件-您無法利用現有的 HTML/CSS 設計。 2.專有文件模型: MigraDoc 需要學習獨特的文檔模型,其中包含AddSection() 、 AddParagraph() 、 AddTable() 、 AddRow()和AddCell()等概念。 這種陡峭的學習曲線對於有網頁開發背景的開發人員來說尤其具有挑戰性。 3.樣式選項有限:雖然 MigraDoc 提供了強大的文件結構管理功能,但與現代 Web 工具相比,其樣式功能較為有限。 與完整的 CSS3 相比, Format.Font.Size 、 Format.Font.Bold和Format.Alignment等屬性的功能有限。 4.冗長的程式碼:即使是創建簡單的佈局也需要幾十行程式碼。 一個有表頭的基本表格可能需要 15-20 行 MigraDoc 程式碼。 5.不支援 JavaScript: MigraDoc 無法渲染動態內容或執行 JavaScript,限制了現代圖表和互動元素的選擇。 6.圖表功能較為基礎:與 Chart.js 或 D3 等現代 JavaScript 圖表函式庫相比,MigraDoc 的圖表功能較為有限。 MigraDoc 與 IronPDF 對比 特徵 MigraDoc IronPDF 內容定義 程序化(文檔/章節/段落) HTML/CSS 學習曲線 Steep(專有 DOM) 簡單(網路技能) 造型 有限財產 完整的 CSS3 JavaScript 沒有任何 完整的 Chromium 執行 表格 手動定義列/行 HTML<table>使用 CSS 圖表 MigraDoc 基本圖表 任何 JavaScript 圖表庫 圖片 手動調整尺寸/定位 標準 HTML<img> 響應式佈局 不支援 Flexbox,網格 執照 開源軟體(MIT) 商業的 對於計劃在 2025 年和 2026 年採用 .NET 10 和 C# 14 的團隊來說,IronPDF 提供了一個面向未來的基礎,使開發人員能夠使用熟悉的 HTML/CSS 技能,而不是學習專有的文件模型。 遷移複雜度評估 各功能預計工作量 特徵 遷移複雜性 筆記 簡單文字 非常低 段落 → HTML 元素 表格 低的 表格/行/單元格 → HTML<table> 頁首/頁尾 低的 節.標題 → 渲染選項 款式 中等的 格式屬性 → CSS 類 圖片 低的 新增圖片 → HTML<img> 圖表 中等的 需要 JavaScript 函式庫 範式轉移 此次 MigraDoc 遷移的根本轉變是從程式化文件建置轉向HTML 優先渲染: MigraDoc:文件 → 新增節(AddSection()) → 新增段落(AddParagraph()) → PdfDocumentRenderer → 儲存(Save()) IronPDF:ChromePdfRenderer → RenderHtmlAsPdf(html) → SaveAs() 這種範式轉變顯著降低了程式碼的複雜性,同時透過 CSS 提供了無限的樣式功能。 開始之前 先決條件 .NET 環境: .NET Framework 4.6.2+ 或 .NET Core 3.1+ / .NET 5/6/7/8/9+ NuGet 存取權限:能夠安裝 NuGet 套件 IronPDF 許可證:請從ironpdf.com取得您的許可證密鑰。 NuGet 套件變更 # Remove MigraDoc packages dotnet remove package PdfSharp-MigraDoc dotnet remove package PdfSharp-MigraDoc-GDI dotnet remove package PDFsharp.MigraDoc.Standard # Install IronPDF dotnet add package IronPdf # Remove MigraDoc packages dotnet remove package PdfSharp-MigraDoc dotnet remove package PdfSharp-MigraDoc-GDI dotnet remove package PDFsharp.MigraDoc.Standard # 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 識別 MigraDoc 使用情況 # Find all MigraDoc references grep -r "using MigraDoc\|PdfDocumentRenderer\|AddSection\|AddParagraph" --include="*.cs" . grep -r "AddTable\|AddRow\|AddColumn\|AddCell\|AddImage" --include="*.cs" . # Find all MigraDoc references grep -r "using MigraDoc\|PdfDocumentRenderer\|AddSection\|AddParagraph" --include="*.cs" . grep -r "AddTable\|AddRow\|AddColumn\|AddCell\|AddImage" --include="*.cs" . SHELL 完整 API 參考 類別映射 MigraDoc 類 IronPDF當量 筆記 Document ChromePdfRenderer 使用渲染器,而不是文檔 Section HTML或者 結構容器 Paragraph HTML<p> ,<h1> , ETC。 文字元素 FormattedText HTML<span> ,<strong> , ETC。 行內格式 Table HTML<table> 使用 CSS 樣式 Row HTML<tr> 表格行 Column HTML<col>或 CSS 列樣式 Cell HTML<td> ,<th> 表格單元格 PdfDocumentRenderer ChromePdfRenderer 主渲染器 方法映射 MigraDoc 方法 IronPDF當量 筆記 document.AddSection() HTML結構 使用<div>或者<section> section.AddParagraph(text) <p>text</p> HTML段落 section.AddTable() <table> HTML表格 table.AddColumn(width) CSS width屬性 時尚風格<th>或者<td> table.AddRow() <tr> HTML 行 row.Cells[n].AddParagraph() <td>content</td> HTML單元格 renderer.RenderDocument() RenderHtmlAsPdf(html) 渲染為 PDF pdfDocument.Save(path) pdf.SaveAs(path) 儲存檔案 佔位符映射(頁首/頁尾) MigraDoc 方法 IronPDF佔位符 筆記 AddPageField() {page} 目前頁碼 AddNumPagesField() {total-pages} 總頁數 AddDateField() {date} 目前日期 程式碼遷移範例 範例 1:HTML 轉 PDF 的基本轉換(根本差異) 之前(MigraDoc): // NuGet: Install-Package PdfSharp-MigraDoc-GDI using MigraDoc.DocumentObjectModel; using MigraDoc.Rendering; using System.Diagnostics; class Program { static void Main() { // MigraDoc doesn't support HTML directly // Must manually create document structure Document document = new Document(); Section section = document.AddSection(); Paragraph paragraph = section.AddParagraph(); paragraph.AddFormattedText("Hello World", TextFormat.Bold); paragraph.Format.Font.Size = 16; PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(); pdfRenderer.Document = document; pdfRenderer.RenderDocument(); pdfRenderer.PdfDocument.Save("output.pdf"); } } // NuGet: Install-Package PdfSharp-MigraDoc-GDI using MigraDoc.DocumentObjectModel; using MigraDoc.Rendering; using System.Diagnostics; class Program { static void Main() { // MigraDoc doesn't support HTML directly // Must manually create document structure Document document = new Document(); Section section = document.AddSection(); Paragraph paragraph = section.AddParagraph(); paragraph.AddFormattedText("Hello World", TextFormat.Bold); paragraph.Format.Font.Size = 16; PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(); pdfRenderer.Document = document; pdfRenderer.RenderDocument(); pdfRenderer.PdfDocument.Save("output.pdf"); } } $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>"); 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>"); pdf.SaveAs("output.pdf"); } } $vbLabelText $csharpLabel 這個例子說明了 MigraDoc 和 IronPDF 之間的根本差異。 MigraDoc 需要建立一個Document ,增加一個Section ,增加一個Paragraph ,使用AddFormattedText()和TextFormat.Bold ,設定Format.Font.Size ,建立一個PdfDocumentRenderer ,分配文檔,呼叫RenderDocument() ,最後儲存。 那可是十多行程式碼,涉及多個物件。 IronPDF 以 3 行程式碼即可實現相同的結果:建立渲染器、渲染 HTML 並儲存。 HTML<h1>標籤自然而然地提供了粗體、大標題樣式。 有關其他渲染選項,請參閱HTML 轉 PDF 文件。 範例 2:建立表格 之前(MigraDoc): // NuGet: Install-Package PdfSharp-MigraDoc-GDI using MigraDoc.DocumentObjectModel; using MigraDoc.DocumentObjectModel.Tables; using MigraDoc.Rendering; class Program { static void Main() { Document document = new Document(); Section section = document.AddSection(); Table table = section.AddTable(); table.Borders.Width = 0.75; Column column1 = table.AddColumn("3cm"); Column column2 = table.AddColumn("3cm"); Row row1 = table.AddRow(); row1.Cells[0].AddParagraph("Name"); row1.Cells[1].AddParagraph("Age"); Row row2 = table.AddRow(); row2.Cells[0].AddParagraph("John"); row2.Cells[1].AddParagraph("30"); PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(); pdfRenderer.Document = document; pdfRenderer.RenderDocument(); pdfRenderer.PdfDocument.Save("table.pdf"); } } // NuGet: Install-Package PdfSharp-MigraDoc-GDI using MigraDoc.DocumentObjectModel; using MigraDoc.DocumentObjectModel.Tables; using MigraDoc.Rendering; class Program { static void Main() { Document document = new Document(); Section section = document.AddSection(); Table table = section.AddTable(); table.Borders.Width = 0.75; Column column1 = table.AddColumn("3cm"); Column column2 = table.AddColumn("3cm"); Row row1 = table.AddRow(); row1.Cells[0].AddParagraph("Name"); row1.Cells[1].AddParagraph("Age"); Row row2 = table.AddRow(); row2.Cells[0].AddParagraph("John"); row2.Cells[1].AddParagraph("30"); PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(); pdfRenderer.Document = document; pdfRenderer.RenderDocument(); pdfRenderer.PdfDocument.Save("table.pdf"); } } $vbLabelText $csharpLabel (IronPDF 之後): // NuGet: Install-Package IronPdf using IronPdf; class Program { static void Main() { var renderer = new ChromePdfRenderer(); string htmlTable = @" <table border='1'> <tr><th>Name</th><th>Age</th></tr> <tr><td>John</td><td>30</td></tr> </table>"; var pdf = renderer.RenderHtmlAsPdf(htmlTable); pdf.SaveAs("table.pdf"); } } // NuGet: Install-Package IronPdf using IronPdf; class Program { static void Main() { var renderer = new ChromePdfRenderer(); string htmlTable = @" <table border='1'> <tr><th>Name</th><th>Age</th></tr> <tr><td>John</td><td>30</td></tr> </table>"; var pdf = renderer.RenderHtmlAsPdf(htmlTable); pdf.SaveAs("table.pdf"); } } $vbLabelText $csharpLabel 建立 MigraDoc 表格需要了解Table 、 Column 、 Row和Cell格的層次結構。 必須使用AddColumn()明確新增列,使用AddRow()建立行,使用Cells[n]透過索引存取單元格,並使用AddParagraph()新增內容。 邊框是透過table.Borders.Width設定的。 IronPDF 使用任何 Web 開發人員都熟悉的標準 HTML 表格語法。 border='1'屬性用來設定邊框,<th>元素創建標題單元格,並且<td>元素創建資料單元格。 可以添加 CSS 來實現高級樣式,例如斑馬紋、懸停效果或響應式佈局。 了解更多關於在PDF中建立表格的資訊。 範例 3:帶頁碼的頁首和頁尾 之前(MigraDoc): // NuGet: Install-Package PdfSharp-MigraDoc-GDI using MigraDoc.DocumentObjectModel; using MigraDoc.Rendering; class Program { static void Main() { Document document = new Document(); Section section = document.AddSection(); // Add header Paragraph headerPara = section.Headers.Primary.AddParagraph(); headerPara.AddText("Document Header"); headerPara.Format.Font.Size = 12; headerPara.Format.Alignment = ParagraphAlignment.Center; // Add footer Paragraph footerPara = section.Footers.Primary.AddParagraph(); footerPara.AddText("Page "); footerPara.AddPageField(); footerPara.Format.Alignment = ParagraphAlignment.Center; // Add content section.AddParagraph("Main content of the document"); PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(); pdfRenderer.Document = document; pdfRenderer.RenderDocument(); pdfRenderer.PdfDocument.Save("header-footer.pdf"); } } // NuGet: Install-Package PdfSharp-MigraDoc-GDI using MigraDoc.DocumentObjectModel; using MigraDoc.Rendering; class Program { static void Main() { Document document = new Document(); Section section = document.AddSection(); // Add header Paragraph headerPara = section.Headers.Primary.AddParagraph(); headerPara.AddText("Document Header"); headerPara.Format.Font.Size = 12; headerPara.Format.Alignment = ParagraphAlignment.Center; // Add footer Paragraph footerPara = section.Footers.Primary.AddParagraph(); footerPara.AddText("Page "); footerPara.AddPageField(); footerPara.Format.Alignment = ParagraphAlignment.Center; // Add content section.AddParagraph("Main content of the document"); PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(); pdfRenderer.Document = document; pdfRenderer.RenderDocument(); pdfRenderer.PdfDocument.Save("header-footer.pdf"); } } $vbLabelText $csharpLabel (IronPDF 之後): // NuGet: Install-Package IronPdf using IronPdf; class Program { static void Main() { var renderer = new ChromePdfRenderer(); var pdf = renderer.RenderHtmlAsPdf("<h1>Main content of the document</h1>"); pdf.AddTextHeader("Document Header"); pdf.AddTextFooter("Page {page}"); pdf.SaveAs("header-footer.pdf"); } } // NuGet: Install-Package IronPdf using IronPdf; class Program { static void Main() { var renderer = new ChromePdfRenderer(); var pdf = renderer.RenderHtmlAsPdf("<h1>Main content of the document</h1>"); pdf.AddTextHeader("Document Header"); pdf.AddTextFooter("Page {page}"); pdf.SaveAs("header-footer.pdf"); } } $vbLabelText $csharpLabel MigraDoc 的頁首和頁尾需要訪問section.Headers.Primary和section.Footers.Primary ,在其中創建段落,使用AddText()添加文本,並使用AddPageField()等特殊方法來添加動態內容。 對齊方式需要設定Format.Alignment 。 IronPDF 在PdfDocument物件上提供了簡單的AddTextHeader()和AddTextFooter()方法。 {page}佔位符會自動插入目前頁碼。 對於更複雜的頁眉,您可以使用具有完整 HTML/CSS 支援的HtmlHeaderFooter 。 有關進階選項,請參閱頁首和頁尾文件。 關鍵遷移說明 頁碼佔位符語法 頁首和頁尾最重要的變化是佔位語法: // MigraDoc field methods: footerPara.AddPageField(); // Current page footerPara.AddNumPagesField(); // Total pages // IronPDF placeholders: "Page {page} of {total-pages}" // MigraDoc field methods: footerPara.AddPageField(); // Current page footerPara.AddNumPagesField(); // Total pages // IronPDF placeholders: "Page {page} of {total-pages}" $vbLabelText $csharpLabel 將屬性格式化為 CSS MigraDoc 的Format屬性對應於 CSS: // MigraDoc: paragraph.Format.Font.Size = 16; paragraph.Format.Font.Bold = true; paragraph.Format.Alignment = ParagraphAlignment.Center; // IronPDF (CSS): <p style="font-size: 16pt; font-weight: bold; text-align: center;"> // MigraDoc: paragraph.Format.Font.Size = 16; paragraph.Format.Font.Bold = true; paragraph.Format.Alignment = ParagraphAlignment.Center; // IronPDF (CSS): <p style="font-size: 16pt; font-weight: bold; text-align: center;"> $vbLabelText $csharpLabel 單位換算 MigraDoc 使用各種單位; IronPDF 的頁邊距單位為毫米: 1公分 = 10毫米 1吋 = 25.4毫米 "72pt" = 25.4毫米 // MigraDoc: table.AddColumn("3cm"); // IronPDF (CSS): <th style="width: 3cm;"> // MigraDoc: table.AddColumn("3cm"); // IronPDF (CSS): <th style="width: 3cm;"> $vbLabelText $csharpLabel 渲染模式變化 整個渲染模式都改變了: // MigraDoc pattern (DELETE): PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(); pdfRenderer.Document = document; pdfRenderer.RenderDocument(); pdfRenderer.PdfDocument.Save("output.pdf"); // IronPDF pattern: var pdf = renderer.RenderHtmlAsPdf(html); pdf.SaveAs("output.pdf"); // MigraDoc pattern (DELETE): PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(); pdfRenderer.Document = document; pdfRenderer.RenderDocument(); pdfRenderer.PdfDocument.Save("output.pdf"); // IronPDF pattern: var pdf = renderer.RenderHtmlAsPdf(html); pdf.SaveAs("output.pdf"); $vbLabelText $csharpLabel 故障排除 問題 1:找不到文件/章節 問題: IronPDF 中不存在Document和Section類別。 解決方案:替換為 HTML 結構: // MigraDoc Document document = new Document(); Section section = document.AddSection(); // IronPDF string html = "<html><body>...</body></html>"; var pdf = renderer.RenderHtmlAsPdf(html); // MigraDoc Document document = new Document(); Section section = document.AddSection(); // IronPDF string html = "<html><body>...</body></html>"; var pdf = renderer.RenderHtmlAsPdf(html); $vbLabelText $csharpLabel 問題 2:找不到 AddParagraph 函數 問題: AddParagraph()方法不存在。 解決方案:使用 HTML 元素: // MigraDoc section.AddParagraph("Hello World"); // IronPDF "<p>Hello World</p>" // MigraDoc section.AddParagraph("Hello World"); // IronPDF "<p>Hello World</p>" $vbLabelText $csharpLabel 問題 3:未找到 PdfDocumentRenderer 問題: PdfDocumentRenderer類別不存在。 解決方案:使用ChromePdfRenderer : // MigraDoc PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(); // IronPDF var renderer = new ChromePdfRenderer(); // MigraDoc PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(); // IronPDF var renderer = new ChromePdfRenderer(); $vbLabelText $csharpLabel 問題 4:AddPageField 功能無法正常運作 問題: AddPageField()方法不存在。 解決方法:使用 IronPDF 佔位符語法: // MigraDoc footerPara.AddPageField(); // IronPDF pdf.AddTextFooter("Page {page}"); // MigraDoc footerPara.AddPageField(); // IronPDF pdf.AddTextFooter("Page {page}"); $vbLabelText $csharpLabel 遷移清單 遷移前 using語句辨識所有 MigraDoc 文件表格結構(列、行、樣式) 注意頁首/頁尾內容和頁面欄位的使用 列出使用document.Styles定義的自訂樣式 取得 IronPDF 許可證密鑰 軟體包變更 移除PdfSharp-MigraDoc軟體包 刪除PdfSharp-MigraDoc-GDI軟體包 安裝IronPdf NuGet 套件: dotnet add package IronPdf 更新命名空間匯入 程式碼更改 在啟動時新增許可證金鑰配置 將Document / Section替換為 HTML 結構 將AddParagraph()轉換為 HTML<p>元素 將Table / Row / Cell轉換為 HTML<table>結構 將AddPageField()替換為{page}佔位符 將AddNumPagesField()替換為{total-pages}佔位符 將Format屬性轉換為 CSS 樣式 將PdfDocumentRenderer替換為ChromePdfRenderer 測試 對比新舊PDF檔案的視覺效果 驗證分頁符號是否正常運作 檢查頁首/頁尾渲染和頁碼 檢查表格格式和邊框 使用複雜的多頁文件進行測試 移民後 刪除與 MigraDoc 相關的文檔 更新團隊培訓資料 記錄新的 HTML 範本位置 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 MuPDF to IronPDF in C#How to Migrate from Kaizen.io to Ir...
發表日期 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. 閱讀更多