如何在 HTML 到 PDF 轉換中處理 C# 的頁面分隔
BitMiracle Docotic PDF 是一個備受推崇的.NET PDF 程式庫,以其100%的受控程式碼架構和廣泛的程式化PDF操作功能而聞名。 然而,其模組化附加結構——需要單獨的套件進行HTML到PDF的轉換、版面設計功能及其他功能——增加了專案管理和授權的複雜性。 這份全面的指南提供了從BitMiracle Docotic PDF遷移到IronPDF的逐步遷移路徑——一個統一的.NET PDF程式庫,內建Chromium為基礎的HTML渲染,並將所有功能包含在一個單一的NuGet套件中。
為什麼從BitMiracle Docotic PDF遷移到IronPDF?
雖然BitMiracle Docotic PDF提供強大的PDF操作功能,但有多種因素驅使開發團隊尋求具有更加精簡架構的替代方案。
套件架構比較
BitMiracle Docotic PDF採用模組化附加的方法,為了完整功能需要多個套件:
| 方面 | BitMiracle Docotic PDF | IronPDF |
|---|---|---|
| HTML到PDF | 需要單獨的附加功能(HtmlToPdf) | 內建核心功能 |
| 套件結構 | 核心+多個附加功能 | 單個NuGet套件 |
| 授權模型 | 按附加功能授權 | 所有功能均包含 |
| API複雜性 | 每個附加功能各自獨立的命名空間 | 統一的API |
| HTML引擎 | Chromium (通過附加功能) | Chromium (內建) |
| 社群規模 | 小 | 大規模,更多資源 |
| 文件 | 技術參考 | 廣泛的教學 |
功能對等
兩個程式庫都支援全面的PDF功能:
| 功能 | BitMiracle Docotic PDF | IronPDF |
|---|---|---|
| 從頭建立PDF | ✅ | ✅ |
| HTML轉PDF | ✅ (需要附加功能) | ✅ (內建) |
| URL到PDF | ✅ (需要附加功能) | ✅ (內建) |
| PDF操作 | ✅ | ✅ |
| 文字提取 | ✅ | ✅ |
| 合併/分割 | ✅ | ✅ |
| 數位簽名 | ✅ | ✅ |
| 加密 | ✅ | ✅ |
| 表單填寫 | ✅ | ✅ |
| PDF/A合規 | ✅ | ✅ |
方法上的主要差異
BitMiracle Docotic PDF使用基於畫布的繪圖與座標定位(canvas.DrawString(x, y, text)),而IronPDF利用HTML/CSS進行版面設計和定位。 這代表了一種範例轉移,簡化了對熟悉網頁技術的開發人員的內容建立。
遷移前準備
前提條件
確保您的環境符合以下要求:
- .NET Framework 4.6.2+ or .NET Core 3.1 / .NET 5-9
- Visual Studio 2019+或VS Code與C#擴展
- NuGet包管理器存取 -IronPDF許可證金鑰(免費試用可在 ironpdf.com 獲得)
審計BitMiracle Docotic PDF的使用情況
在您的解決方案目錄中運行這些命令以識別所有的 Docotic.Pdf 引用:
# Find all Docotic.Pdf usages in your codebase
grep -r "using BitMiracle.Docotic" --include="*.cs" .
grep -r "PdfDocument\|PdfPage\|PdfCanvas" --include="*.cs" .
# Find NuGet package references
grep -r "Docotic.Pdf" --include="*.csproj" .
# Find all Docotic.Pdf usages in your codebase
grep -r "using BitMiracle.Docotic" --include="*.cs" .
grep -r "PdfDocument\|PdfPage\|PdfCanvas" --include="*.cs" .
# Find NuGet package references
grep -r "Docotic.Pdf" --include="*.csproj" .
預期的中斷變更
| 更改 | BitMiracle Docotic PDF | IronPDF | 影響 |
|---|---|---|---|
| HTML 渲染 | 需要 HtmlToPdf 附加程式 | 內建 | 移除附加包 |
| 頁面索引 | 0基於 (Pages[0]) |
0基於 (Pages[0]) |
不需要變更 |
| 座標系統 | 底部左邊原點 | HTML/CSS流 | 使用 CSS 定位 |
| 畫布繪圖 | PdfCanvas.DrawText() |
HTML標記 | 預設轉變 |
| 文字提取 | page.GetText() |
pdf.ExtractAllText() |
方法名稱變更 |
| 文件載入 | new PdfDocument(path) |
PdfDocument.FromFile(path) |
構造函式 → 靜態方法 |
| 保存 | document.Save(path) |
pdf.SaveAs(path) |
方法名稱變更 |
| 處理 | IDisposable 模式 |
不需要 | 簡化的資源管理 |
逐步遷移過程
步驟1:更新NuGet包
移除 BitMiracle Docotic PDF 套件並安裝 IronPDF:
# Remove Docotic.Pdf packages
dotnet remove package BitMiracle.Docotic.Pdf
dotnet remove package BitMiracle.Docotic.Pdf.HtmlToPdf
dotnet remove package BitMiracle.Docotic.Pdf.Layout
# Install IronPDF
dotnet add package IronPdf
# Remove Docotic.Pdf packages
dotnet remove package BitMiracle.Docotic.Pdf
dotnet remove package BitMiracle.Docotic.Pdf.HtmlToPdf
dotnet remove package BitMiracle.Docotic.Pdf.Layout
# Install IronPDF
dotnet add package IronPdf
步驟 2:更新命名空間引用
用IronPDF替換 BitMiracle Docotic PDF 的命名空間:
// Remove these
using BitMiracle.Docotic.Pdf;
using BitMiracle.Docotic.Pdf.Layout;
// HtmlConverter from the BitMiracle.Docotic.Pdf.HtmlToPdf NuGet package
// also lives in the BitMiracle.Docotic.Pdf namespace (no extra using needed).
// Add this
using IronPdf;
// Remove these
using BitMiracle.Docotic.Pdf;
using BitMiracle.Docotic.Pdf.Layout;
// HtmlConverter from the BitMiracle.Docotic.Pdf.HtmlToPdf NuGet package
// also lives in the BitMiracle.Docotic.Pdf namespace (no extra using needed).
// Add this
using IronPdf;
Imports IronPdf
步驟3:配置授權
// Add at application startup (Program.cs or Global.asax)
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
// Add at application startup (Program.cs or Global.asax)
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
' Add at application startup (Program.vb or Global.asax)
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
完整 API 遷移參考
文件操作
| 任務 | BitMiracle Docotic PDF | IronPDF |
|---|---|---|
| 建立空文件 | new PdfDocument() |
new PdfDocument() |
| 從檔案載入 | new PdfDocument(path) |
PdfDocument.FromFile(path) |
| 從流載入 | PdfDocument.Load(stream) |
PdfDocument.FromStream(stream) |
| 從字節載入 | PdfDocument.Load(bytes) |
PdfDocument.FromBinaryData(bytes) |
| 保存到檔案 | document.Save(path) |
pdf.SaveAs(path) |
| 獲取頁數 | document.PageCount |
pdf.PageCount |
| 關閉/處理 | document.Dispose() |
不需要 |
HTML到PDF轉換
| 任務 | BitMiracle Docotic PDF (HtmlToPdf 附加程式) | IronPDF |
|---|---|---|
| HTML字串轉PDF | await converter.CreatePdfFromStringAsync(html) |
renderer.RenderHtmlAsPdf(html) |
| HTML文件轉PDF | await converter.CreatePdfAsync(filePath) |
renderer.RenderHtmlFileAsPdf(path) |
| URL到PDF | await converter.CreatePdfAsync(new Uri(url)) |
renderer.RenderUrlAsPdf(url) |
| 設定頁面大小 | options.Page.SetSize(PdfPaperSize.A4) |
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4 |
| 設定邊距 | options.Page.MarginTop = 20 (etc.) |
renderer.RenderingOptions.MarginTop = 20 |
合併和分割操作
| 任務 | BitMiracle Docotic PDF | IronPDF |
|---|---|---|
| 合併文件 | doc1.Append("file.pdf") (路徑/流/字節) |
PdfDocument.Merge(pdf1, pdf2) |
| 分割文件 | document.CopyPage(index) 到新的檔案 |
pdf.CopyPages(start, end) |
程式碼遷移範例
HTML到PDF轉換
最常見的操作展示了IronPDF提供的顯著簡化。
BitMiracle Docotic PDF實施:
// NuGet: Install-Package BitMiracle.Docotic.Pdf
// NuGet: Install-Package BitMiracle.Docotic.Pdf.HtmlToPdf (separate add-on required)
using BitMiracle.Docotic.Pdf;
using System;
using System.Threading.Tasks;
class Program
{
static async 任務 Main()
{
// HtmlConverter is async-only and downloads Chromium on first use
using var converter = await HtmlConverter.CreateAsync();
string html = "<html><body><h1>Hello World</h1><p>This isHTML轉PDFconversion.</p></body></html>";
using var pdf = await converter.CreatePdfFromStringAsync(html);
pdf.Save("output.pdf");
Console.WriteLine("PDF created successfully");
}
}
// NuGet: Install-Package BitMiracle.Docotic.Pdf
// NuGet: Install-Package BitMiracle.Docotic.Pdf.HtmlToPdf (separate add-on required)
using BitMiracle.Docotic.Pdf;
using System;
using System.Threading.Tasks;
class Program
{
static async 任務 Main()
{
// HtmlConverter is async-only and downloads Chromium on first use
using var converter = await HtmlConverter.CreateAsync();
string html = "<html><body><h1>Hello World</h1><p>This isHTML轉PDFconversion.</p></body></html>";
using var pdf = await converter.CreatePdfFromStringAsync(html);
pdf.Save("output.pdf");
Console.WriteLine("PDF created successfully");
}
}
Imports BitMiracle.Docotic.Pdf
Imports System
Imports System.Threading.Tasks
Module Program
Async Function Main() As Task
' HtmlConverter is async-only and downloads Chromium on first use
Using converter = Await HtmlConverter.CreateAsync()
Dim html As String = "<html><body><h1>Hello World</h1><p>This is HTML to PDF conversion.</p></body></html>"
Using pdf = Await converter.CreatePdfFromStringAsync(html)
pdf.Save("output.pdf")
End Using
Console.WriteLine("PDF created successfully")
End Using
End Function
End Module
IronPDF實現:
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string html = "<html><body><h1>Hello World</h1><p>This isHTML轉PDFconversion.</p></body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
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();
string html = "<html><body><h1>Hello World</h1><p>This isHTML轉PDFconversion.</p></body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF created successfully");
}
}
Imports IronPdf
Imports System
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim html As String = "<html><body><h1>Hello World</h1><p>This isHTML轉PDFconversion.</p></body></html>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("output.pdf")
Console.WriteLine("PDF created successfully")
End Sub
End Class
IronPDF消除了 using 語句需求,並提供了一個專門的 ChromePdfRenderer 類,清楚地表明其基於Chromium的渲染功能。 欲了解更多HTML轉換選項,請參閱 HTML到PDF文件。
合併多個 PDF
BitMiracle Docotic PDF實施:
// NuGet: Install-Package BitMiracle.Docotic.Pdf
using BitMiracle.Docotic.Pdf;
using System;
class Program
{
static void Main()
{
using (var pdf1 = new PdfDocument("document1.pdf"))
{
// PdfDocument.Append accepts a file path, Stream, or byte[] —
// not another PdfDocument instance.
pdf1.Append("document2.pdf");
pdf1.Save("merged.pdf");
}
Console.WriteLine("PDFs merged successfully");
}
}
// NuGet: Install-Package BitMiracle.Docotic.Pdf
using BitMiracle.Docotic.Pdf;
using System;
class Program
{
static void Main()
{
using (var pdf1 = new PdfDocument("document1.pdf"))
{
// PdfDocument.Append accepts a file path, Stream, or byte[] —
// not another PdfDocument instance.
pdf1.Append("document2.pdf");
pdf1.Save("merged.pdf");
}
Console.WriteLine("PDFs merged successfully");
}
}
Imports BitMiracle.Docotic.Pdf
Imports System
Class Program
Shared Sub Main()
Using pdf1 As New PdfDocument("document1.pdf")
' PdfDocument.Append accepts a file path, Stream, or byte[] —
' not another PdfDocument instance.
pdf1.Append("document2.pdf")
pdf1.Save("merged.pdf")
End Using
Console.WriteLine("PDFs merged successfully")
End Sub
End Class
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 merged = PdfDocument.Merge(new List<PdfDocument> { pdf1, pdf2 });
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 merged = PdfDocument.Merge(new List<PdfDocument> { pdf1, pdf2 });
merged.SaveAs("merged.pdf");
Console.WriteLine("PDFs merged successfully");
}
}
Imports IronPdf
Imports System
Imports System.Collections.Generic
Module Program
Sub Main()
Dim pdf1 As PdfDocument = PdfDocument.FromFile("document1.pdf")
Dim pdf2 As PdfDocument = PdfDocument.FromFile("document2.pdf")
Dim merged As PdfDocument = PdfDocument.Merge(New List(Of PdfDocument) From {pdf1, pdf2})
merged.SaveAs("merged.pdf")
Console.WriteLine("PDFs merged successfully")
End Sub
End Module
IronPDF的靜態 Merge 方法直接接受多個文件,提供比迭代 Append 模式更乾淨的API。 欲了解更多選項,請參閱 PDF合併文件。
文字提取
BitMiracle Docotic PDF實施:
// NuGet: Install-Package BitMiracle.Docotic.Pdf
using BitMiracle.Docotic.Pdf;
using System;
class Program
{
static void Main()
{
using (var pdf = new PdfDocument("document.pdf"))
{
string allText = "";
foreach (var page in pdf.Pages)
{
allText += page.GetText();
}
Console.WriteLine("Extracted text:");
Console.WriteLine(allText);
}
}
}
// NuGet: Install-Package BitMiracle.Docotic.Pdf
using BitMiracle.Docotic.Pdf;
using System;
class Program
{
static void Main()
{
using (var pdf = new PdfDocument("document.pdf"))
{
string allText = "";
foreach (var page in pdf.Pages)
{
allText += page.GetText();
}
Console.WriteLine("Extracted text:");
Console.WriteLine(allText);
}
}
}
Imports BitMiracle.Docotic.Pdf
Imports System
Class Program
Shared Sub Main()
Using pdf As New PdfDocument("document.pdf")
Dim allText As String = ""
For Each page In pdf.Pages
allText &= page.GetText()
Next
Console.WriteLine("Extracted text:")
Console.WriteLine(allText)
End Using
End Sub
End Class
IronPDF實現:
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var pdf = PdfDocument.FromFile("document.pdf");
string allText = pdf.ExtractAllText();
Console.WriteLine("Extracted text:");
Console.WriteLine(allText);
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
var pdf = PdfDocument.FromFile("document.pdf");
string allText = pdf.ExtractAllText();
Console.WriteLine("Extracted text:");
Console.WriteLine(allText);
}
}
Imports IronPdf
Imports System
Class Program
Shared Sub Main()
Dim pdf = PdfDocument.FromFile("document.pdf")
Dim allText As String = pdf.ExtractAllText()
Console.WriteLine("Extracted text:")
Console.WriteLine(allText)
End Sub
End Class
IronPDF將多行迴圈的文字提取減少為一次方法調用。 更多的提取選項,請參閱 文字提取文件。
密碼保護和加密
IronPDF實現:
using IronPdf;
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Confidential Document</h1>");
// Set security
pdf.SecuritySettings.UserPassword = "userPassword";
pdf.SecuritySettings.OwnerPassword = "ownerPassword";
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.FullPrintRights;
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SaveAs("protected.pdf");
using IronPdf;
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Confidential Document</h1>");
// Set security
pdf.SecuritySettings.UserPassword = "userPassword";
pdf.SecuritySettings.OwnerPassword = "ownerPassword";
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.FullPrintRights;
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SaveAs("protected.pdf");
Imports IronPdf
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Confidential Document</h1>")
' Set security
pdf.SecuritySettings.UserPassword = "userPassword"
pdf.SecuritySettings.OwnerPassword = "ownerPassword"
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.FullPrintRights
pdf.SecuritySettings.AllowUserCopyPasteContent = False
pdf.SaveAs("protected.pdf")
欲了解全面的安全選項,請參閱 加密文件。
頭部和尾部
IronPDF實現:
using IronPdf;
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
{
HtmlFragment = @"
<div style='text-align:center; font-size:12px;'>
Company Header - Confidential
</div>",
DrawDividerLine = true,
MaxHeight = 30
};
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter
{
HtmlFragment = @"
<div style='text-align:center; font-size:10px;'>
Page {page} of {total-pages}
</div>",
DrawDividerLine = true,
MaxHeight = 25
};
var pdf = renderer.RenderHtmlAsPdf("<h1>Document Content</h1>");
pdf.SaveAs("with_headers.pdf");
using IronPdf;
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
{
HtmlFragment = @"
<div style='text-align:center; font-size:12px;'>
Company Header - Confidential
</div>",
DrawDividerLine = true,
MaxHeight = 30
};
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter
{
HtmlFragment = @"
<div style='text-align:center; font-size:10px;'>
Page {page} of {total-pages}
</div>",
DrawDividerLine = true,
MaxHeight = 25
};
var pdf = renderer.RenderHtmlAsPdf("<h1>Document Content</h1>");
pdf.SaveAs("with_headers.pdf");
Imports IronPdf
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter With {
.HtmlFragment = "
<div style='text-align:center; font-size:12px;'>
Company Header - Confidential
</div>",
.DrawDividerLine = True,
.MaxHeight = 30
}
renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter With {
.HtmlFragment = "
<div style='text-align:center; font-size:10px;'>
Page {page} of {total-pages}
</div>",
.DrawDividerLine = True,
.MaxHeight = 25
}
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Document Content</h1>")
pdf.SaveAs("with_headers.pdf")
IronPDF支援像{total-pages}那樣的佔位符程式碼進行動態頁碼。 欲了解更多選項,請參見 頁眉和頁腳文件。
關鍵遷移注意事項
画布到HTML範例轉變
BitMiracle Docotic PDF的基於畫布的繪圖方法必須轉換為具有CSS定位的HTML:
BitMiracle Docotic PDF樣式:
var canvas = pdfPage.Canvas;
canvas.DrawString(50, 50, "Hello, World!");
var canvas = pdfPage.Canvas;
canvas.DrawString(50, 50, "Hello, World!");
Dim canvas = pdfPage.Canvas
canvas.DrawString(50, 50, "Hello, World!")
IronPDF模式:
var html = "<div style='position:absolute; left:50px; top:50px;'>Hello, World!</div>";
var pdf = renderer.RenderHtmlAsPdf(html);
var html = "<div style='position:absolute; left:50px; top:50px;'>Hello, World!</div>";
var pdf = renderer.RenderHtmlAsPdf(html);
Dim html As String = "<div style='position:absolute; left:50px; top:50px;'>Hello, World!</div>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
相同的頁面索引
兩個庫都使用0基準索引(Pages[0]是第一頁)——頁面存取程式碼不需要更改。
不需要處理
IronPDF不需要using語句用於記憶體管理,簡化了程式碼結構:
// BitMiracle Docotic PDF - disposal required
using (var pdf = new PdfDocument("input.pdf"))
{
// operations
}
//IronPDF- disposal optional
var pdf = PdfDocument.FromFile("input.pdf");
// operations - no using statement needed
// BitMiracle Docotic PDF - disposal required
using (var pdf = new PdfDocument("input.pdf"))
{
// operations
}
//IronPDF- disposal optional
var pdf = PdfDocument.FromFile("input.pdf");
// operations - no using statement needed
Imports BitMiracle.Docotic.Pdf
' BitMiracle Docotic PDF - disposal required
Using pdf As New PdfDocument("input.pdf")
' operations
End Using
' IronPDF- disposal optional
Dim pdf = PdfDocument.FromFile("input.pdf")
' operations - no using statement needed
異步支持
BitMiracle Docotic PDF的HtmlToPdf附加程式需要在所有地方的非同步模式。 IronPDF支援同步和非同步方法:
// Synchronous
var pdf = renderer.RenderHtmlAsPdf(html);
// Asynchronous
var pdf = await renderer.RenderHtmlAsPdfAsync(html);
// Synchronous
var pdf = renderer.RenderHtmlAsPdf(html);
// Asynchronous
var pdf = await renderer.RenderHtmlAsPdfAsync(html);
' Synchronous
Dim pdf = renderer.RenderHtmlAsPdf(html)
' Asynchronous
Dim pdf = Await renderer.RenderHtmlAsPdfAsync(html)
ASP.NET Core 整合
IronPDF模式:
[ApiController]
[Route("[controller]")]
public class PdfController : ControllerBase
{
[HttpGet("generate")]
public IActionResult GeneratePdf()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Report</h1>");
return File(pdf.BinaryData, "application/pdf", "report.pdf");
}
[HttpGet("generate-async")]
public async Task<IActionResult> GeneratePdfAsync()
{
var renderer = new ChromePdfRenderer();
var pdf = await renderer.RenderHtmlAsPdfAsync("<h1>Report</h1>");
return File(pdf.BinaryData, "application/pdf", "report.pdf");
}
}
[ApiController]
[Route("[controller]")]
public class PdfController : ControllerBase
{
[HttpGet("generate")]
public IActionResult GeneratePdf()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Report</h1>");
return File(pdf.BinaryData, "application/pdf", "report.pdf");
}
[HttpGet("generate-async")]
public async Task<IActionResult> GeneratePdfAsync()
{
var renderer = new ChromePdfRenderer();
var pdf = await renderer.RenderHtmlAsPdfAsync("<h1>Report</h1>");
return File(pdf.BinaryData, "application/pdf", "report.pdf");
}
}
Imports Microsoft.AspNetCore.Mvc
<ApiController>
<Route("[controller]")>
Public Class PdfController
Inherits ControllerBase
<HttpGet("generate")>
Public Function GeneratePdf() As IActionResult
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Report</h1>")
Return File(pdf.BinaryData, "application/pdf", "report.pdf")
End Function
<HttpGet("generate-async")>
Public Async Function GeneratePdfAsync() As Task(Of IActionResult)
Dim renderer = New ChromePdfRenderer()
Dim pdf = Await renderer.RenderHtmlAsPdfAsync("<h1>Report</h1>")
Return File(pdf.BinaryData, "application/pdf", "report.pdf")
End Function
End Class
後遷移檢查清單
完成程式碼遷移後,請驗證以下內容:
- 運行所有單元測試以驗證PDF生成是否正常工作
- 比較PDF輸出質量(IronPDF的Chromium引擎可能會稍有不同的渲染,通常更好)
- 驗證文字提取的準確性
- 測試表單填寫功能
- 驗證數位簽名(如果適用)
- 性能測試批量操作
- 在所有目標環境中測試
- 更新 CI/CD 管道
- 移除 Docotic.Pdf 授權檔案
其他資源
從BitMiracle Docotic PDF遷移到IronPDF消除了管理多個附加套件的複雜性,並提供了相同的Chromium基於HTML渲染功能。 從基於畫布的繪圖轉變到HTML/CSS定位利用了大多數.NET開發人員已經具備的網頁開發技能,結果是更易於維護的PDF生成程式碼。

