產品比較 SpirePDF C# HTML到PDF教程和庫比較 Jacob Mellor 更新日期:8月 6, 2025 Download IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 將 HTML 轉換為 PDF 是許多 .NET 應用程序的關鍵需求,從生成發票和報告到創建文檔和電子書。 當您在 IronPDF 和 Spire.PDF 之間選擇 HTML 到 PDF 的轉換需求時,了解它們的優勢、局限性和真實世界性能變得至關重要。 這個全面的比較檢查了這兩個庫的功能,幫助您為您的 PDF 生成需求做出明智的決定。 IronPDF 和 Spire.PDF 在 HTML 到 PDF 轉換方面的主要區別是什麼? IronPDF 是一個多功能的 .NET PDF 庫,旨在處理多種與 PDF 相關的任務,包括轉換 PDF 文件、PDF 編輯、PDF 創建、閱讀 PDF 文件等。 它以易於集成到現有 .NET 應用程序、高性能和支持現代 Web 標準(如 HTML5、CSS3 和 JavaScript)而聞名。 IronPDF 的基於 Chrome 的渲染引擎確保從 HTML 內容生成像素完美的 PDF。 Spire.PDF for .NET 是另一個強大的 .NET PDF 庫,提供了一整套 PDF 創建和操作功能。 它支持各種 PDF 功能,例如文本和圖像提取、PDF 表單填充和數字簽名。 雖然功能齊全,但其 HTML 到 PDF 轉換功能具有開發人員應考慮的特定限制。 class="product__comprehensive-comparison-table"> class="table-container"> class="table-number">產品比較概述 class="table-title">IronPDF 和 Spire.PDF 功能比較在 .NET PDF 處理中的作用 類別 功能/方面 IronPDF Spire.PDF 主要優勢 核心架構 設計理念 簡單為先、直觀的 API 多類方法,手動設置 IronPDF:更快的開發 API 複雜性 簡單的方法,如 RenderHtmlAsPdf() 需要多線程方式 IronPDF:代碼少70% 學習曲線 通常1-2天 通常1-2周 IronPDF:更快的採用 平台支持 跨平台 本地支持,無需額外包 有限的 Linux/macOS 支持 IronPDF:真正的跨平台 .NET 版本 .NET 8、7、6、5、Core 3.1+、Framework 4.6.2+ .NET Core 2.0+、Framework 2.0+ 兩者:廣泛的框架支持 操作系統 Windows、Linux、macOS、Docker 本地 主要面向 Windows IronPDF:更廣泛的操作系統支持 HTML 到 PDF 渲染引擎 完整的 Chrome V8 引擎 定製的 HTML 解析器 IronPDF:98%+ 瀏覽器保真度 CSS3/HTML5 支持 完全支持 有限(僅基本 CSS) IronPDF:現代網絡標準 JavaScript 執行 完整的 JavaScript 支持 計劃中但未實現 IronPDF:動態內容就緒 網頁字體 Google 字體、系統字體 字體支持有限 IronPDF:字體靈活性 渲染速度 通常為 0.8–1.2 秒 簡單 HTML 為 0.5–0.8 秒 Spire:適合基礎 HTML 更快 安全與加密 加密級別 AES-256,自定義處理器 AES-256 標準 兩者:行業標準 API 簡化 單一 SecuritySettings 類 多個安全方法 IronPDF:統一的方法 內容操作 修訂方法 真正的內容刪除,一行 API 手動矩形疊加 IronPDF:合規準備 水印 基於 HTML/CSS,完整的樣式 帶旋轉的文本為基礎 IronPDF:豐富的水印 文件轉換 DOCX 到 PDF 內建 DocxToPdfRenderer 需要 Spire.Doc 庫 IronPDF:無額外成本 性能指標 內存使用 150–200MB (Chrome 引擎) 80–120MB Spire:更低的內存佔用 線程支持 本地 async/await 優化 需要 STA 線程 IronPDF:現代異步模式 開發者體驗 代碼示例 100+ 可運行示例 基本示例 IronPDF:豐富的資源 錯誤信息 描述性,可操作 技術信息 IronPDF:更好的調試 許可及定價 入門級 Lite:$799 (1 開發者, 1 項目) 開發者:$1,199/年 (1 開發者) IronPDF:永久許可 團隊許可 Plus:$1,199 (3 開發者, 3 項目) 站點:,599 (10 開發者) IronPDF:更好的團隊價值 套件選項 Iron Suite:$1,498 (9 個產品) 無套件選項 IronPDF:卓越的價值 支持 支持包含 是,24/5 工程支持 僅論壇(額外付費支持) IronPDF:支持包 支持渠道 電子郵件、聊天、電話、屏幕共享 論壇、電子郵件(付費等級) IronPDF:更多渠道 最佳用途 使用案例 現代網絡應用、複雜的 HTML/CSS 簡單文檔、基本版式 上下文依賴 class="table-note"> class="note">注意: Spire.PDF 提供基本的 PDF 功能,內存佔用較小,而 IronPDF 在 HTML 渲染和支持現代 Web 標準方面表現出色。請根據複雜的 HTML 渲染或資源使用最小化的優先級選擇。 欲了解有關 IronPDF 的更詳細信息,請訪問 IronPDF.com。 這些庫之間的跨平台兼容性如何比較? IronPDF IronPDF 以其廣泛的跨平台兼容性而脫穎而出。 它支持 .NET 框架中的多種環境,確保在不同平台上的無縫操作。 以下是 IronPDF 平台兼容性的摘要: .NET 版本: 完全用和支持 C#、VB.NET 和 F# 寫成 .NET Core(8、7、6、5 和 3.1+) .NET Standard(2.0+) .NET Framework(4.6.2+) 應用程式環境: IronPDF 適用於諸如 Windows、Linux、Mac、Docker、Azure 和 AWS 等不同的應用程式環境。 IDE: 支援於 Microsoft Visual Studio 和 JetBrains Rider & ReSharper 等 IDE 操作系統和處理器: 支援多個不同的操作系統和處理器,包括 Windows、Mac、Linux、x64、x86、ARM 如需了解 IronPDF 的兼容性的更多信息,請訪問 IronPDF 功能頁面。 Spire.PDF Spire.PDF 在 .NET 環境中具有全面支持,但雖然它可以在 Windows 操作系統下工作,但在 Linux 和 macOS 上缺乏 IronPDF 所具有的本地支持。 在 macOS 或 Linux 上部署時,Spire.PDF 每個平台都需要不同的插件,這可能會使部署場景複雜化。 .NET 支持: .NET Framework 2.0+ .NET Core 2.0+ 以 C# 編寫,支持 VB.NET 應用環境: Spire.PDF 在各種應用環境中工作,如 Windows 和 ASP.NET 應用。 它還可以支持 Windows Forms。 哪個庫提供更好的 HTML 到 PDF 轉換功能? IronPDF 功能 PDF 轉換:IronPDF 可以 將 HTML 轉換為 PDF。 通過全面支持現代 Web 標準,您可以確保 IronPDF 能夠一致地從您的 HTML 內容返回像素完美的 PDF。 IronPDF 還可以將 PDF 文件從其他格式轉換,例如 DOCX、圖像、RTF 等。 PDF 生成: 使用 IronPDF,您可以從 URL、ASPX 文件或 HTML 字符串生成 PDF。 安全功能:用 IronPDF,您可以始終確保任何敏感的 PDF 文件都安全,這要歸功於其 安全功能。 使用 IronPDF 對您的 PDF 文件進行加密、設置密碼和對您的 PDF 文件設置權限。 PDF 編輯功能: 使用 IronPDF,您可以輕鬆處理現有的 PDF 文檔,編輯它們,以及閱讀 PDF 文件。 IronPDF offers editing features such as adding headers and footers, stamping text and images onto the PDF pages, adding custom watermarks to the PDF, working with PDF forms, and splitting or merging PDF files. 集成: 無縫集成在 ASP.NET 和 MVC 應用。 PDF 版本支持: 可支持 PDF 版本 1.2-1.7 如需了解 IronPDF 的完整功能列表,請訪問 IronPDF 功能。 Spire.PDF 功能 PDF 創建: 從頭開始或從現有文件創建 PDF。 文本和圖像提取: 使用 Spire.PDF 提取 PDF 頁面和內容。 PDF 表單處理: 填寫和管理 PDF 表單。 PDF 轉換: 將 PDF 轉換為其他格式,如 HTML、RTF 和圖像。 頁面操作: 插入、刪除和重新定義 PDF 中的頁面順序。 有限的 HTML 支持: HTML 到 PDF 引擎在現代 CSS 和 JavaScript 支持方面有限。 默認的基於 Qt 的插件不支持 CSS 頁面斷開屬性或外部 CSS 文件識別,而 JavaScript 執行是一個尚未完全實現的計劃功能。 有可選的 ChromeHtmlConverter,可用於更好的渲染。 Bootstrap 和現代 CSS 框架渲染 現代網絡開發大量依賴於如 Bootstrap 這樣的 CSS 框架,以進行快速的 UI 開發和響應式設計。 PDF 生成庫必須能準確地轉換這些框架,以維持專業文檔質量和設計一致性。 IronPDF:完整的 Bootstrap 和 Tailwind 支持 IronPDF 的 Chromium 引擎全面支持所有現代 CSS 框架: Bootstrap 5:使用所有相應部件的完整 Flexbox 和 CSS Grid 支持 Bootstrap 4:完整的卡片佈局、導航系統和實用類 Tailwind CSS:所有實用優先類和響應修飾符 Foundation:完整的網格和組件系統 現代 CSS3:Flexbox、Grid、定義屬性、動畫和轉換 Proven with real examples: Bootstrap homepage and Bootstrap templates convert with pixel-perfect accuracy. 代碼示例:用 Bootstrap 團隊成員卡片 using IronPdf; var renderer = new ChromePdfRenderer(); string bootstrapTeam = @" <!DOCTYPE html> <html> <head> <link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css' rel='stylesheet'> </head> <body> <div class='container my-5'> <h1 class='text-center mb-5'>Our Team</h1> <div class='row row-cols-1 row-cols-md-3 g-4'> <div class='col'> <div class='card h-100 shadow-sm text-center'> <div class='card-body d-flex flex-column'> <img src='https://via.placeholder.com/150' class='rounded-circle mx-auto mb-3' alt='Team member' style='width: 150px; height: 150px; object-fit: cover;'> <h5 class='card-title'>Jennifer Martinez</h5> <p class='text-muted mb-3'>Chief Technology Officer</p> <p class='card-text flex-grow-1'>Leading our technical vision and innovation strategy with over 15 years of experience in software architecture.</p> <div class='d-flex justify-content-center gap-2 mt-auto'> <a href='#' class='btn btn-sm btn-outline-primary'>LinkedIn</a> <a href='#' class='btn btn-sm btn-outline-secondary'>Email</a> </div> </div> </div> </div> <div class='col'> <div class='card h-100 shadow-sm text-center'> <div class='card-body d-flex flex-column'> <img src='https://via.placeholder.com/150' class='rounded-circle mx-auto mb-3' alt='Team member' style='width: 150px; height: 150px; object-fit: cover;'> <h5 class='card-title'>David Chen</h5> <p class='text-muted mb-3'>Head of Engineering</p> <p class='card-text flex-grow-1'>Managing our engineering teams and ensuring product quality through rigorous testing and code review processes.</p> <div class='d-flex justify-content-center gap-2 mt-auto'> <a href='#' class='btn btn-sm btn-outline-primary'>LinkedIn</a> <a href='#' class='btn btn-sm btn-outline-secondary'>Email</a> </div> </div> </div> </div> <div class='col'> <div class='card h-100 shadow-sm text-center'> <div class='card-body d-flex flex-column'> <img src='https://via.placeholder.com/150' class='rounded-circle mx-auto mb-3' alt='Team member' style='width: 150px; height: 150px; object-fit: cover;'> <h5 class='card-title'>Sarah Johnson</h5> <p class='text-muted mb-3'>Product Manager</p> <p class='card-text flex-grow-1'>Driving product strategy and customer satisfaction through data-driven decisions and user research.</p> <div class='d-flex justify-content-center gap-2 mt-auto'> <a href='#' class='btn btn-sm btn-outline-primary'>LinkedIn</a> <a href='#' class='btn btn-sm btn-outline-secondary'>Email</a> </div> </div> </div> </div> </div> <div class='text-center mt-5'> <h3>Join Our Team</h3> <p class='text-muted mb-3'>We're always looking for talented individuals to join our growing team.</p> <a href='#' class='btn btn-primary btn-lg'>View Open Positions</a> </div> </div> </body> </html>"; var pdf = renderer.RenderHtmlAsPdf(bootstrapTeam); pdf.SaveAs("team-members.pdf"); using IronPdf; var renderer = new ChromePdfRenderer(); string bootstrapTeam = @" <!DOCTYPE html> <html> <head> <link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css' rel='stylesheet'> </head> <body> <div class='container my-5'> <h1 class='text-center mb-5'>Our Team</h1> <div class='row row-cols-1 row-cols-md-3 g-4'> <div class='col'> <div class='card h-100 shadow-sm text-center'> <div class='card-body d-flex flex-column'> <img src='https://via.placeholder.com/150' class='rounded-circle mx-auto mb-3' alt='Team member' style='width: 150px; height: 150px; object-fit: cover;'> <h5 class='card-title'>Jennifer Martinez</h5> <p class='text-muted mb-3'>Chief Technology Officer</p> <p class='card-text flex-grow-1'>Leading our technical vision and innovation strategy with over 15 years of experience in software architecture.</p> <div class='d-flex justify-content-center gap-2 mt-auto'> <a href='#' class='btn btn-sm btn-outline-primary'>LinkedIn</a> <a href='#' class='btn btn-sm btn-outline-secondary'>Email</a> </div> </div> </div> </div> <div class='col'> <div class='card h-100 shadow-sm text-center'> <div class='card-body d-flex flex-column'> <img src='https://via.placeholder.com/150' class='rounded-circle mx-auto mb-3' alt='Team member' style='width: 150px; height: 150px; object-fit: cover;'> <h5 class='card-title'>David Chen</h5> <p class='text-muted mb-3'>Head of Engineering</p> <p class='card-text flex-grow-1'>Managing our engineering teams and ensuring product quality through rigorous testing and code review processes.</p> <div class='d-flex justify-content-center gap-2 mt-auto'> <a href='#' class='btn btn-sm btn-outline-primary'>LinkedIn</a> <a href='#' class='btn btn-sm btn-outline-secondary'>Email</a> </div> </div> </div> </div> <div class='col'> <div class='card h-100 shadow-sm text-center'> <div class='card-body d-flex flex-column'> <img src='https://via.placeholder.com/150' class='rounded-circle mx-auto mb-3' alt='Team member' style='width: 150px; height: 150px; object-fit: cover;'> <h5 class='card-title'>Sarah Johnson</h5> <p class='text-muted mb-3'>Product Manager</p> <p class='card-text flex-grow-1'>Driving product strategy and customer satisfaction through data-driven decisions and user research.</p> <div class='d-flex justify-content-center gap-2 mt-auto'> <a href='#' class='btn btn-sm btn-outline-primary'>LinkedIn</a> <a href='#' class='btn btn-sm btn-outline-secondary'>Email</a> </div> </div> </div> </div> </div> <div class='text-center mt-5'> <h3>Join Our Team</h3> <p class='text-muted mb-3'>We're always looking for talented individuals to join our growing team.</p> <a href='#' class='btn btn-primary btn-lg'>View Open Positions</a> </div> </div> </body> </html>"; var pdf = renderer.RenderHtmlAsPdf(bootstrapTeam); pdf.SaveAs("team-members.pdf"); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 輸出:專業的團隊頁面,有 Bootstrap 的卡片網格、Flexbox 對齊方式、響應式圖像和按鈕組件,所有這些都準確呈現在 PDF 中。 Spire.PDF:有限的 HTML 和現代框架支持 Spire.PDF 的 HTML 到 PDF 引擎對現代 CSS 框架有顯著的限制,正如其自己的文檔中所指出的: 有限的 CSS 支持:僅基本的 CSS,沒有全面的 CSS3 功能 沒有 Flexbox:Bootstrap 4/5 大量依賴於 Flexbox,但不支持 沒有 CSS 格柵:使用 CSS 格柵的現代佈局系統將無法正確渲染 JavaScript 限制:與現代瀏覽器相比,Minimal JavaScript 執行量 Bootstrap 3 最佳:只能信賴舊的基於表格的 Bootstrap 版本 複雜的佈局失敗:現代響應設計需要廣泛的變通方法 開發人員報告Spire.PDF HTML 轉換的問題: Bootstrap 導航欄渲染存在佈局問題 使用 Flexbox 的卡片組件未能正確對齊 PDF 輸出中忽略了響應的網格斷點 現代 CSS 功能需要手動 CSS 重寫或后備方案 開發影響:使用 Bootstrap 4+ 或現代 CSS 框架的應用在 Spire.PDF 中面臨顯著挑戰。 開發團隊必須: 維護專為 PDF 生成的獨立簡化版 CSS(雙重維護) 降級到 Bootstrap 3 或完全放棄框架(限制現代功能) 3.廣泛的測試和手動修復每個組件(耗時且脆弱) 對於項目需要現代 CSS 框架支持,Spire.PDF 的 HTML 渲染限制使其不適合,除非有大量的其他開發工作以及持續的維護負擔。 有關詳細的 Bootstrap 兼容性信息,請參見 Bootstrap & Flexbox CSS 指南。 常見 PDF 任務的代碼示例如何比較? 為了展示 IronPDF 和 Spire.PDF 在 .NET 中的功能,我們將比較它們在若干關鍵 PDF 功能實現中的代碼示例,給您一個這些特性如何幫助您的 PDF 項目的想法。 轉換 HTML 到 PDF IronPDF 示例: using IronPdf; // Disable local disk access or cross-origin requests Installation.EnableWebSecurity = true; // Instantiate Renderer var renderer = new ChromePdfRenderer(); // Create a PDF from an HTML string using C# var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>"); pdf.SaveAs("output.pdf"); // Advanced Example with HTML Assets // Load external HTML assets: images, CSS, and JavaScript. // An optional BasePath 'C:\site\assets\' is set as the file location to load assets from var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\"); myAdvancedPdf.SaveAs("html-with-assets.pdf"); using IronPdf; // Disable local disk access or cross-origin requests Installation.EnableWebSecurity = true; // Instantiate Renderer var renderer = new ChromePdfRenderer(); // Create a PDF from an HTML string using C# var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>"); pdf.SaveAs("output.pdf"); // Advanced Example with HTML Assets // Load external HTML assets: images, CSS, and JavaScript. // An optional BasePath 'C:\site\assets\' is set as the file location to load assets from var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\"); myAdvancedPdf.SaveAs("html-with-assets.pdf"); Imports IronPdf ' Disable local disk access or cross-origin requests Installation.EnableWebSecurity = True ' Instantiate Renderer Dim renderer = New ChromePdfRenderer() ' Create a PDF from an HTML string using C# Dim pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>") pdf.SaveAs("output.pdf") ' Advanced Example with HTML Assets ' Load external HTML assets: images, CSS, and JavaScript. ' An optional BasePath 'C:\site\assets\' is set as the file location to load assets from Dim myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", "C:\site\assets\") myAdvancedPdf.SaveAs("html-with-assets.pdf") $vbLabelText $csharpLabel IronPDF 中的 ChromePdfRenderer 類利用完整的 Chrome 瀏覽器引擎來渲染 HTML 內容。 這意味著 CSS3 動畫、JavaScript 執行和現代 Web 字體將以與 Chrome 中一致的顯示。 RenderHtmlAsPdf() 方法接受 HTML 字符串和可選的基路徑來加載外部資源,使其轉換簡單和複雜的 HTML 文檔變得簡單。 渲染器支持 高級渲染選項,包括自定義紙張尺寸、邊距和 JavaScript 延遲設置。 Spire.PDF 示例: using Spire.Pdf; using Spire.Pdf.HtmlConverter; using System.IO; using System.Threading; using System.Drawing; namespace ConvertHtmlStringToPdfWithoutPlugin { class Program { static void Main(string[] args) { // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Create a PdfPageSettings object PdfPageSettings setting = new PdfPageSettings(); // Set page size and margins through the object setting.Size = new SizeF(1000, 1000); setting.Margins = new Spire.Pdf.Graphics.PdfMargins(20); // Create a PdfHtmlLayoutFormat object PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat(); // Set IsWaiting property to true htmlLayoutFormat.IsWaiting = true; // Read HTML string from a .html file string htmlString = File.ReadAllText(@"C:\Users\Administrator\Desktop\Document\Html\Sample.html"); // Load HTML from HTML string using LoadFromHTML method Thread thread = new Thread(() => { doc.LoadFromHTML(htmlString, true, setting, htmlLayoutFormat); }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); // Save to a PDF file doc.SaveToFile("HtmlStringToPdf.pdf"); } } } using Spire.Pdf; using Spire.Pdf.HtmlConverter; using System.IO; using System.Threading; using System.Drawing; namespace ConvertHtmlStringToPdfWithoutPlugin { class Program { static void Main(string[] args) { // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Create a PdfPageSettings object PdfPageSettings setting = new PdfPageSettings(); // Set page size and margins through the object setting.Size = new SizeF(1000, 1000); setting.Margins = new Spire.Pdf.Graphics.PdfMargins(20); // Create a PdfHtmlLayoutFormat object PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat(); // Set IsWaiting property to true htmlLayoutFormat.IsWaiting = true; // Read HTML string from a .html file string htmlString = File.ReadAllText(@"C:\Users\Administrator\Desktop\Document\Html\Sample.html"); // Load HTML from HTML string using LoadFromHTML method Thread thread = new Thread(() => { doc.LoadFromHTML(htmlString, true, setting, htmlLayoutFormat); }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); // Save to a PDF file doc.SaveToFile("HtmlStringToPdf.pdf"); } } } Imports Spire.Pdf Imports Spire.Pdf.HtmlConverter Imports System.IO Imports System.Threading Imports System.Drawing Namespace ConvertHtmlStringToPdfWithoutPlugin Friend Class Program Shared Sub Main(ByVal args() As String) ' Create a PdfDocument object Dim doc As New PdfDocument() ' Create a PdfPageSettings object Dim setting As New PdfPageSettings() ' Set page size and margins through the object setting.Size = New SizeF(1000, 1000) setting.Margins = New Spire.Pdf.Graphics.PdfMargins(20) ' Create a PdfHtmlLayoutFormat object Dim htmlLayoutFormat As New PdfHtmlLayoutFormat() ' Set IsWaiting property to true htmlLayoutFormat.IsWaiting = True ' Read HTML string from a .html file Dim htmlString As String = File.ReadAllText("C:\Users\Administrator\Desktop\Document\Html\Sample.html") ' Load HTML from HTML string using LoadFromHTML method Dim thread As New Thread(Sub() doc.LoadFromHTML(htmlString, True, setting, htmlLayoutFormat) End Sub) thread.SetApartmentState(ApartmentState.STA) thread.Start() thread.Join() ' Save to a PDF file doc.SaveToFile("HtmlStringToPdf.pdf") End Sub End Class End Namespace $vbLabelText $csharpLabel Spire.PDF 的 HTML 到 PDF 轉換需要創建多個物件,包括 PdfDocument、PdfPageSettings 和 PdfHtmlLayoutFormat。 轉換必須在單線程應用程序 (STA) 線程中執行,增加了實現的複雜性。 用戶報告說“Spire 生成了一個僅僅是一個圖像的 PDF 文件。, 一些 CSS 甚至是不正確的,例如忽略粗體字體”,這表明在 CSS 渲染保真度上的限制。 IronPDF 使用 Chrome 的渲染引擎進行高保真的 HTML 到 PDF 轉換,確保 web 內容的精確表示,這要歸功於它對現代 Web 標準的支持。 Spire.PDF 也提供強大的 HTML 到 PDF 轉換,但可能無法匹配 IronPDF 的渲染準確度,且需要更長時間的更手動的方法。 如何加密 PDF 文檔? IronPDF 示例: using IronPdf; using System; // Open an Encrypted File, alternatively create a new PDF from HTML var pdf = PdfDocument.FromFile("encrypted.pdf", "password"); // Edit file metadata pdf.MetaData.Author = "Satoshi Nakamoto"; pdf.MetaData.Keywords = "SEO, Friendly"; pdf.MetaData.ModifiedDate = DateTime.Now; // Edit file security settings pdf.SecuritySettings.RemovePasswordsAndEncryption(); pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key"); pdf.SecuritySettings.AllowUserAnnotations = false; pdf.SecuritySettings.AllowUserCopyPasteContent = false; pdf.SecuritySettings.AllowUserFormData = false; pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.FullPrintRights; // Change or set the document encryption password pdf.Password = "my-password"; pdf.SaveAs("secured.pdf"); using IronPdf; using System; // Open an Encrypted File, alternatively create a new PDF from HTML var pdf = PdfDocument.FromFile("encrypted.pdf", "password"); // Edit file metadata pdf.MetaData.Author = "Satoshi Nakamoto"; pdf.MetaData.Keywords = "SEO, Friendly"; pdf.MetaData.ModifiedDate = DateTime.Now; // Edit file security settings pdf.SecuritySettings.RemovePasswordsAndEncryption(); pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key"); pdf.SecuritySettings.AllowUserAnnotations = false; pdf.SecuritySettings.AllowUserCopyPasteContent = false; pdf.SecuritySettings.AllowUserFormData = false; pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.FullPrintRights; // Change or set the document encryption password pdf.Password = "my-password"; pdf.SaveAs("secured.pdf"); Imports IronPdf Imports System ' Open an Encrypted File, alternatively create a new PDF from HTML Private pdf = PdfDocument.FromFile("encrypted.pdf", "password") ' Edit file metadata pdf.MetaData.Author = "Satoshi Nakamoto" pdf.MetaData.Keywords = "SEO, Friendly" pdf.MetaData.ModifiedDate = DateTime.Now ' Edit file security settings pdf.SecuritySettings.RemovePasswordsAndEncryption() pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key") pdf.SecuritySettings.AllowUserAnnotations = False pdf.SecuritySettings.AllowUserCopyPasteContent = False pdf.SecuritySettings.AllowUserFormData = False pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.FullPrintRights ' Change or set the document encryption password pdf.Password = "my-password" pdf.SaveAs("secured.pdf") $vbLabelText $csharpLabel IronPDF 的 SecuritySettings 類提供一個全面的 API 來管理 PDF 安全。 可以用布爾屬性控制每個權限,設定不同級別的列印權限,並獨立於其他安全設置來管理密碼。 API 的設計遵循 .NET 慣例,使用強類型的枚舉,如 PdfPrintSecurity。 更多有關 PDF 權限和密碼 的信息請參考文檔。 Spire.PDF 示例: using Spire.Pdf; using Spire.Pdf.Security; // Create a PdfDocument object PdfDocument pdf = new PdfDocument(); // Load a sample PDF file pdf.LoadFromFile(@"E:\Files\sample.pdf"); // Encrypt the PDF file with a password pdf.Security.Encrypt("open", "permission", PdfPermissionsFlags.Print | PdfPermissionsFlags.CopyContent, PdfEncryptionKeySize.Key128Bit); // Save the result file pdf.SaveToFile("Encrypt.pdf", FileFormat.PDF); using Spire.Pdf; using Spire.Pdf.Security; // Create a PdfDocument object PdfDocument pdf = new PdfDocument(); // Load a sample PDF file pdf.LoadFromFile(@"E:\Files\sample.pdf"); // Encrypt the PDF file with a password pdf.Security.Encrypt("open", "permission", PdfPermissionsFlags.Print | PdfPermissionsFlags.CopyContent, PdfEncryptionKeySize.Key128Bit); // Save the result file pdf.SaveToFile("Encrypt.pdf", FileFormat.PDF); Imports Spire.Pdf Imports Spire.Pdf.Security ' Create a PdfDocument object Private pdf As New PdfDocument() ' Load a sample PDF file pdf.LoadFromFile("E:\Files\sample.pdf") ' Encrypt the PDF file with a password pdf.Security.Encrypt("open", "permission", PdfPermissionsFlags.Print Or PdfPermissionsFlags.CopyContent, PdfEncryptionKeySize.Key128Bit) ' Save the result file pdf.SaveToFile("Encrypt.pdf", FileFormat.PDF) $vbLabelText $csharpLabel Spire.PDF 的加密方法使用 Security.Encrypt() 方法和位旗標來設置權限。 雖然較為簡潔,但這種方法要求對位操作的理解,且相比於 IronPDF 的單獨權限屬性提供的控制更少。 這兩個庫都提供了易於使用的方法來加密 PDF 和設置加密 PDF 文件的可讀性。 IronPDF 提供直接的方法,同時依然給您充分控製 PDF 的安全設置。 Spire.PDF 的過程稍微短一些,但仍然包括更多設置權限的選項。 可供選擇的修訂 PDF 內容的方法有哪些? IronPDF 示例: using IronPdf; // Load the PDF document PdfDocument pdf = PdfDocument.FromFile("novel.pdf"); // Redact 'are' phrase from all pages pdf.RedactTextOnAllPages("are"); // Save the redacted document pdf.SaveAs("redacted.pdf"); using IronPdf; // Load the PDF document PdfDocument pdf = PdfDocument.FromFile("novel.pdf"); // Redact 'are' phrase from all pages pdf.RedactTextOnAllPages("are"); // Save the redacted document pdf.SaveAs("redacted.pdf"); Imports IronPdf ' Load the PDF document Private pdf As PdfDocument = PdfDocument.FromFile("novel.pdf") ' Redact 'are' phrase from all pages pdf.RedactTextOnAllPages("are") ' Save the redacted document pdf.SaveAs("redacted.pdf") $vbLabelText $csharpLabel IronPDF 提供專門的 修訂 API,進行真正的內容刪除,而不僅僅是視覺遮罩。 RedactTextOnAllPages() 方法搜索並永久刪除指定文本,確保符合數據保護法規。 額外的方法允許特定頁面或區域的修訂。 Spire.PDF 通過矩形疊加進行修訂的示例: 雖然 Spire.PDF 沒有專門的修訂工具,內容仍可以通過手動操作的方式來進行修訂,如下面所示: using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; // Specify the input PDF file path string inputPdfFilePath = "path/to/your/input.pdf"; // Specify the output redacted PDF file path string outputPdfFilePath = "path/to/your/redacted_output.pdf"; // Create a new PdfDocument object PdfDocument pdfDocument = new PdfDocument(); // Load the existing PDF document pdfDocument.LoadFromFile(inputPdfFilePath); // Redact content on each page foreach (PdfPageBase page in pdfDocument.Pages) { // Define the area to redact (e.g., coordinates and size of the rectangle) RectangleF redactArea = new RectangleF(100, 100, 200, 50); // Example coordinates and size // Apply redaction page.Canvas.DrawRectangle(new PdfSolidBrush(Color.Black), redactArea); } // Save the redacted PDF document pdfDocument.SaveToFile(outputPdfFilePath); using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; // Specify the input PDF file path string inputPdfFilePath = "path/to/your/input.pdf"; // Specify the output redacted PDF file path string outputPdfFilePath = "path/to/your/redacted_output.pdf"; // Create a new PdfDocument object PdfDocument pdfDocument = new PdfDocument(); // Load the existing PDF document pdfDocument.LoadFromFile(inputPdfFilePath); // Redact content on each page foreach (PdfPageBase page in pdfDocument.Pages) { // Define the area to redact (e.g., coordinates and size of the rectangle) RectangleF redactArea = new RectangleF(100, 100, 200, 50); // Example coordinates and size // Apply redaction page.Canvas.DrawRectangle(new PdfSolidBrush(Color.Black), redactArea); } // Save the redacted PDF document pdfDocument.SaveToFile(outputPdfFilePath); Imports Spire.Pdf Imports Spire.Pdf.Graphics Imports System.Drawing ' Specify the input PDF file path Private inputPdfFilePath As String = "path/to/your/input.pdf" ' Specify the output redacted PDF file path Private outputPdfFilePath As String = "path/to/your/redacted_output.pdf" ' Create a new PdfDocument object Private pdfDocument As New PdfDocument() ' Load the existing PDF document pdfDocument.LoadFromFile(inputPdfFilePath) ' Redact content on each page For Each page As PdfPageBase In pdfDocument.Pages ' Define the area to redact (e.g., coordinates and size of the rectangle) Dim redactArea As New RectangleF(100, 100, 200, 50) ' Example coordinates and size ' Apply redaction page.Canvas.DrawRectangle(New PdfSolidBrush(Color.Black), redactArea) Next page ' Save the redacted PDF document pdfDocument.SaveToFile(outputPdfFilePath) $vbLabelText $csharpLabel 這種手動方式需要計算每個修訂區域的精確坐標,只提供視覺遮罩而非真正的內容刪除。 底層文本仍可通過 PDF 編輯工具提取。 IronPDF 以其強大而簡單的修訂工具簡化了修訂工作。 由於缺乏內建修訂工具,Spire.PDF 需要更多的手動處理,因而使修訂變得可能效率較低。 如何簽署 PDF 文檔? IronPDF 示例: using IronPdf; using IronPdf.Signing; using System.Security.Cryptography.X509Certificates; // Instantiate the renderer ChromePdfRenderer renderer = new ChromePdfRenderer(); PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>foo</h1>"); // Create X509Certificate2 object with X509KeyStorageFlags set to Exportable X509Certificate2 cert = new X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable); // Create PdfSignature with certificate var sig = new PdfSignature(cert); // Sign PDF document pdf.Sign(sig); pdf.SaveAs("signed.pdf"); using IronPdf; using IronPdf.Signing; using System.Security.Cryptography.X509Certificates; // Instantiate the renderer ChromePdfRenderer renderer = new ChromePdfRenderer(); PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>foo</h1>"); // Create X509Certificate2 object with X509KeyStorageFlags set to Exportable X509Certificate2 cert = new X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable); // Create PdfSignature with certificate var sig = new PdfSignature(cert); // Sign PDF document pdf.Sign(sig); pdf.SaveAs("signed.pdf"); Imports IronPdf Imports IronPdf.Signing Imports System.Security.Cryptography.X509Certificates ' Instantiate the renderer Private renderer As New ChromePdfRenderer() Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>foo</h1>") ' Create X509Certificate2 object with X509KeyStorageFlags set to Exportable Private cert As New X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable) ' Create PdfSignature with certificate Private sig = New PdfSignature(cert) ' Sign PDF document pdf.Sign(sig) pdf.SaveAs("signed.pdf") $vbLabelText $csharpLabel IronPDF 的 數字簽名實現遵循 .NET 的證書處理模式。 PdfSignature 類接受標準的 X509Certificate2 對象並內部處理 PDF 簽名的複雜性。 還可以添加視覺化的簽名外觀並配置簽名字段。 Spire.PDF 示例: using Spire.Pdf; using Spire.Pdf.Security; using System.Drawing; // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a sample PDF file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf"); // Load the certificate PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue"); // Create a PdfSignature object and specify its position and size PdfSignature signature = new PdfSignature(doc, doc.Pages[doc.Pages.Count - 1], cert, "MySignature"); RectangleF rectangleF = new RectangleF(doc.Pages[0].ActualSize.Width - 260 - 54 , 200, 260, 110); signature.Bounds = rectangleF; signature.Certificated = true; // Set the graphics mode to ImageAndSignDetail signature.GraphicsMode = GraphicMode.SignImageAndSignDetail; // Set various details about the signature signature.NameLabel = "Signer:"; signature.Name = "Gary"; signature.ContactInfoLabel = "Phone:"; signature.ContactInfo = "0123456"; signature.DateLabel = "Date:"; signature.Date = DateTime.Now; signature.LocationInfoLabel = "Location:"; signature.LocationInfo = "USA"; signature.ReasonLabel = "Reason:"; signature.Reason = "I am the author"; signature.DistinguishedNameLabel = "DN:"; signature.DistinguishedName = signature.Certificate.IssuerName.Name; // Set the signature image source signature.SignImageSource = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\handwrittingSignature.png"); // Set the signature font signature.SignDetailsFont = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular)); // Set the document permission to forbid changes but allow form fill signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill; // Save to file doc.SaveToFile("VisiableSignature.pdf"); using Spire.Pdf; using Spire.Pdf.Security; using System.Drawing; // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a sample PDF file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf"); // Load the certificate PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue"); // Create a PdfSignature object and specify its position and size PdfSignature signature = new PdfSignature(doc, doc.Pages[doc.Pages.Count - 1], cert, "MySignature"); RectangleF rectangleF = new RectangleF(doc.Pages[0].ActualSize.Width - 260 - 54 , 200, 260, 110); signature.Bounds = rectangleF; signature.Certificated = true; // Set the graphics mode to ImageAndSignDetail signature.GraphicsMode = GraphicMode.SignImageAndSignDetail; // Set various details about the signature signature.NameLabel = "Signer:"; signature.Name = "Gary"; signature.ContactInfoLabel = "Phone:"; signature.ContactInfo = "0123456"; signature.DateLabel = "Date:"; signature.Date = DateTime.Now; signature.LocationInfoLabel = "Location:"; signature.LocationInfo = "USA"; signature.ReasonLabel = "Reason:"; signature.Reason = "I am the author"; signature.DistinguishedNameLabel = "DN:"; signature.DistinguishedName = signature.Certificate.IssuerName.Name; // Set the signature image source signature.SignImageSource = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\handwrittingSignature.png"); // Set the signature font signature.SignDetailsFont = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular)); // Set the document permission to forbid changes but allow form fill signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill; // Save to file doc.SaveToFile("VisiableSignature.pdf"); Imports Spire.Pdf Imports Spire.Pdf.Security Imports System.Drawing ' Create a PdfDocument object Private doc As New PdfDocument() ' Load a sample PDF file doc.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf") ' Load the certificate Dim cert As New PdfCertificate("C:\Users\Administrator\Desktop\MyCertificate.pfx", "e-iceblue") ' Create a PdfSignature object and specify its position and size Dim signature As New PdfSignature(doc, doc.Pages(doc.Pages.Count - 1), cert, "MySignature") Dim rectangleF As New RectangleF(doc.Pages(0).ActualSize.Width - 260 - 54, 200, 260, 110) signature.Bounds = rectangleF signature.Certificated = True ' Set the graphics mode to ImageAndSignDetail signature.GraphicsMode = GraphicMode.SignImageAndSignDetail ' Set various details about the signature signature.NameLabel = "Signer:" signature.Name = "Gary" signature.ContactInfoLabel = "Phone:" signature.ContactInfo = "0123456" signature.DateLabel = "Date:" signature.Date = DateTime.Now signature.LocationInfoLabel = "Location:" signature.LocationInfo = "USA" signature.ReasonLabel = "Reason:" signature.Reason = "I am the author" signature.DistinguishedNameLabel = "DN:" signature.DistinguishedName = signature.Certificate.IssuerName.Name ' Set the signature image source signature.SignImageSource = PdfImage.FromFile("C:\Users\Administrator\Desktop\handwrittingSignature.png") ' Set the signature font signature.SignDetailsFont = New PdfTrueTypeFont(New Font("Arial Unicode MS", 12F, FontStyle.Regular)) ' Set the document permission to forbid changes but allow form fill signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges Or PdfCertificationFlags.AllowFormFill ' Save to file doc.SaveToFile("VisiableSignature.pdf") $vbLabelText $csharpLabel Spire.PDF 提供了對簽名外觀的廣泛控制,但需要更多的代碼。 必須手動設置每個標簽,精確定位簽名,並分別配置可視元件。 數字簽署 PDF 可以在各種設置下有幫助,從驗證 PDF 文件到單純地對新項目簽署。 IronPDF 提供了簡單的方法來添加數字簽名,允許簽署過程的輕松自動化。 Spire.PDF 也支持 PDF 的數字簽名; 然而,過程會更手動,需要更多的努力來實施。 有哪些方法可以為 PDF 添加水印? IronPDF 示例: using IronPdf; // Stamps a Watermark onto a new or existing PDF var renderer = new ChromePdfRenderer(); var pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf"); pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE</h2>", 30, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center); pdf.SaveAs(@"C:\Path\To\Watermarked.pdf"); using IronPdf; // Stamps a Watermark onto a new or existing PDF var renderer = new ChromePdfRenderer(); var pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf"); pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE</h2>", 30, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center); pdf.SaveAs(@"C:\Path\To\Watermarked.pdf"); Imports IronPdf ' Stamps a Watermark onto a new or existing PDF Private renderer = New ChromePdfRenderer() Private pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf") pdf.ApplyWatermark("<h2 style='color:red'>SAMPLE</h2>", 30, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center) pdf.SaveAs("C:\Path\To\Watermarked.pdf") $vbLabelText $csharpLabel IronPDF 的水印功能接受 HTML 內容,允許使用 CSS 進行樣式設置。 ApplyWatermark() 方法提供旋轉角度和對齊的參數,便於精確定位水印。 了解有關 自定義水印 和高級樣式選項的更多信息。 Spire.PDF 示例: using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; // Create a PdfDocument object PdfDocument pdf = new PdfDocument(); // Load a sample PDF document pdf.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf"); // Create a PdfTrueTypeFont object PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 50f), true); // Set the watermark text string text = "CONFIDENTIAL"; // Measure the text size SizeF textSize = font.MeasureString(text); // Calculate the values of two offset variables, // which will be used to calculate the translation amount of the coordinate system float offset1 = (float)(textSize.Width * System.Math.Sqrt(2) / 4); float offset2 = (float)(textSize.Height * System.Math.Sqrt(2) / 4); // Traverse all the pages in the document foreach (PdfPageBase page in pdf.Pages) { // Set the page transparency page.Canvas.SetTransparency(0.8f); // Translate the coordinate system by specified coordinates page.Canvas.TranslateTransform(page.Canvas.Size.Width / 2 - offset1 - offset2, page.Canvas.Size.Height / 2 + offset1 - offset2); // Rotate the coordinate system 45 degrees counterclockwise page.Canvas.RotateTransform(-45); // Draw watermark text on the page page.Canvas.DrawString(text, font, PdfBrushes.DarkGray, 0, 0); } // Save the changes to another file pdf.SaveToFile("TextWatermark.pdf"); using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; // Create a PdfDocument object PdfDocument pdf = new PdfDocument(); // Load a sample PDF document pdf.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf"); // Create a PdfTrueTypeFont object PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 50f), true); // Set the watermark text string text = "CONFIDENTIAL"; // Measure the text size SizeF textSize = font.MeasureString(text); // Calculate the values of two offset variables, // which will be used to calculate the translation amount of the coordinate system float offset1 = (float)(textSize.Width * System.Math.Sqrt(2) / 4); float offset2 = (float)(textSize.Height * System.Math.Sqrt(2) / 4); // Traverse all the pages in the document foreach (PdfPageBase page in pdf.Pages) { // Set the page transparency page.Canvas.SetTransparency(0.8f); // Translate the coordinate system by specified coordinates page.Canvas.TranslateTransform(page.Canvas.Size.Width / 2 - offset1 - offset2, page.Canvas.Size.Height / 2 + offset1 - offset2); // Rotate the coordinate system 45 degrees counterclockwise page.Canvas.RotateTransform(-45); // Draw watermark text on the page page.Canvas.DrawString(text, font, PdfBrushes.DarkGray, 0, 0); } // Save the changes to another file pdf.SaveToFile("TextWatermark.pdf"); Imports System Imports Spire.Pdf Imports Spire.Pdf.Graphics Imports System.Drawing ' Create a PdfDocument object Private pdf As New PdfDocument() ' Load a sample PDF document pdf.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf") ' Create a PdfTrueTypeFont object Dim font As New PdfTrueTypeFont(New Font("Arial", 50F), True) ' Set the watermark text Dim text As String = "CONFIDENTIAL" ' Measure the text size Dim textSize As SizeF = font.MeasureString(text) ' Calculate the values of two offset variables, ' which will be used to calculate the translation amount of the coordinate system Dim offset1 As Single = CSng(textSize.Width * Math.Sqrt(2) / 4) Dim offset2 As Single = CSng(textSize.Height * Math.Sqrt(2) / 4) ' Traverse all the pages in the document For Each page As PdfPageBase In pdf.Pages ' Set the page transparency page.Canvas.SetTransparency(0.8F) ' Translate the coordinate system by specified coordinates page.Canvas.TranslateTransform(page.Canvas.Size.Width \ 2 - offset1 - offset2, page.Canvas.Size.Height \ 2 + offset1 - offset2) ' Rotate the coordinate system 45 degrees counterclockwise page.Canvas.RotateTransform(-45) ' Draw watermark text on the page page.Canvas.DrawString(text, font, PdfBrushes.DarkGray, 0, 0) Next page ' Save the changes to another file pdf.SaveToFile("TextWatermark.pdf") $vbLabelText $csharpLabel Spire.PDF 的水印需要手動計算的定位和旋轉,使用坐標系統變換。 盡管這提供了全面的控制,但需要懂得圖形編程的概念。 IronPDF 提供了一種簡單的方法來添加文本水印,並給予完全的控制權來調整水印的位置和設計。 如果您有 HTML 和 CSS 的經驗,IronPDF 的使用更簡單。 Spire.PDF 的方法更長,涉及更多手動過程,但仍提供了一個強大的水印工具,可以完全控制設計和位置。 如何在 PDF 上加蓋圖像和文字? IronPDF 示例: using IronPdf; using IronPdf.Editing; // Instantiate Renderer ChromePdfRenderer renderer = new ChromePdfRenderer(); PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>"); // Create text stamper TextStamper textStamper = new TextStamper() { Text = "Text Stamper!", FontFamily = "Bungee Spice", UseGoogleFont = true, FontSize = 30, IsBold = true, IsItalic = true, VerticalAlignment = VerticalAlignment.Top, }; // Stamp the text stamper pdf.ApplyStamp(textStamper); pdf.SaveAs("stampText.pdf"); // Create image stamper ImageStamper imageStamper = new ImageStamper(new Uri("https://ironpdf.com/img/svgs/iron-pdf-logo.svg")) { VerticalAlignment = VerticalAlignment.Top, }; // Stamp the image stamper pdf.ApplyStamp(imageStamper, 0); pdf.SaveAs("stampImage.pdf"); using IronPdf; using IronPdf.Editing; // Instantiate Renderer ChromePdfRenderer renderer = new ChromePdfRenderer(); PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>"); // Create text stamper TextStamper textStamper = new TextStamper() { Text = "Text Stamper!", FontFamily = "Bungee Spice", UseGoogleFont = true, FontSize = 30, IsBold = true, IsItalic = true, VerticalAlignment = VerticalAlignment.Top, }; // Stamp the text stamper pdf.ApplyStamp(textStamper); pdf.SaveAs("stampText.pdf"); // Create image stamper ImageStamper imageStamper = new ImageStamper(new Uri("https://ironpdf.com/img/svgs/iron-pdf-logo.svg")) { VerticalAlignment = VerticalAlignment.Top, }; // Stamp the image stamper pdf.ApplyStamp(imageStamper, 0); pdf.SaveAs("stampImage.pdf"); Imports IronPdf Imports IronPdf.Editing ' Instantiate Renderer Private renderer As New ChromePdfRenderer() Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>") ' Create text stamper Private textStamper As New TextStamper() With { .Text = "Text Stamper!", .FontFamily = "Bungee Spice", .UseGoogleFont = True, .FontSize = 30, .IsBold = True, .IsItalic = True, .VerticalAlignment = VerticalAlignment.Top } ' Stamp the text stamper pdf.ApplyStamp(textStamper) pdf.SaveAs("stampText.pdf") ' Create image stamper Dim imageStamper As New ImageStamper(New Uri("https://ironpdf.com/img/svgs/iron-pdf-logo.svg")) With {.VerticalAlignment = VerticalAlignment.Top} ' Stamp the image stamper pdf.ApplyStamp(imageStamper, 0) pdf.SaveAs("stampImage.pdf") $vbLabelText $csharpLabel IronPDF 的加蓋 API 使用專門的類型來不同的加蓋類型。TextStamper 通過 UseGoogleFont 屬性直接支持 Google 字體,而 ImageStamper 可以從 URL 或本地文件加載圖像。 兩個加蓋工具共享通用對齊屬性以保持一致的定位。 探索更多 加蓋選項,包括 HTML 加蓋 和 條形碼集成。 Spire.PDF(文字加蓋示例): using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; // Assumes a PdfDocument is already loaded as `doc` object PdfPageBase page = doc.Pages[0]; PdfGraphicsState state = page.Canvas.Save(); // Define font and brushes for different effects PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 18f); PdfSolidBrush brush1 = new PdfSolidBrush(Color.DeepSkyBlue); PdfSolidBrush brush2 = new PdfSolidBrush(Color.CadetBlue); // Apply transformations and draw strings with shadow and effect page.Canvas.TranslateTransform(20, 200); page.Canvas.ScaleTransform(1f, 0.6f); page.Canvas.SkewTransform(-10, 0); page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush1, 0, 0); page.Canvas.SkewTransform(10, 0); page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush2, 0, 0); page.Canvas.ScaleTransform(1f, -1f); page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush2, 0, -2 * 18); // Restore graphics to previous state page.Canvas.Restore(state); using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; // Assumes a PdfDocument is already loaded as `doc` object PdfPageBase page = doc.Pages[0]; PdfGraphicsState state = page.Canvas.Save(); // Define font and brushes for different effects PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 18f); PdfSolidBrush brush1 = new PdfSolidBrush(Color.DeepSkyBlue); PdfSolidBrush brush2 = new PdfSolidBrush(Color.CadetBlue); // Apply transformations and draw strings with shadow and effect page.Canvas.TranslateTransform(20, 200); page.Canvas.ScaleTransform(1f, 0.6f); page.Canvas.SkewTransform(-10, 0); page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush1, 0, 0); page.Canvas.SkewTransform(10, 0); page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush2, 0, 0); page.Canvas.ScaleTransform(1f, -1f); page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush2, 0, -2 * 18); // Restore graphics to previous state page.Canvas.Restore(state); Imports Spire.Pdf Imports Spire.Pdf.Graphics Imports System.Drawing ' Assumes a PdfDocument is already loaded as `doc` object Private page As PdfPageBase = doc.Pages(0) Private state As PdfGraphicsState = page.Canvas.Save() ' Define font and brushes for different effects Private font As New PdfFont(PdfFontFamily.Helvetica, 18F) Private brush1 As New PdfSolidBrush(Color.DeepSkyBlue) Private brush2 As New PdfSolidBrush(Color.CadetBlue) ' Apply transformations and draw strings with shadow and effect page.Canvas.TranslateTransform(20, 200) page.Canvas.ScaleTransform(1F, 0.6F) page.Canvas.SkewTransform(-10, 0) page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush1, 0, 0) page.Canvas.SkewTransform(10, 0) page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush2, 0, 0) page.Canvas.ScaleTransform(1F, -1F) page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush2, 0, -2 * 18) ' Restore graphics to previous state page.Canvas.Restore(state) $vbLabelText $csharpLabel Spire.PDF 需要直接修改畫布來手動轉換每個效果。 創建複雜的加蓋需要理解圖形狀態管理和坐標轉換。 IronPDF 的文本和圖像加蓋工具是一個強大且易於使用的加蓋工具,在應用加蓋的內容時採用了類似於 HTML 和 CSS 的方法。 雖然 Spire.PDF 也提供了基本的文本加蓋工具,但應用加蓋文本到您的 PDF 文件需要更多的努力。 哪個庫能更好地處理 DOCX 到 PDF 的轉換? IronPDF 示例: using IronPdf; // Instantiate Renderer DocxToPdfRenderer renderer = new DocxToPdfRenderer(); // Render from DOCX file PdfDocument pdf = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx"); // Save the PDF pdf.SaveAs("pdfFromDocx.pdf"); using IronPdf; // Instantiate Renderer DocxToPdfRenderer renderer = new DocxToPdfRenderer(); // Render from DOCX file PdfDocument pdf = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx"); // Save the PDF pdf.SaveAs("pdfFromDocx.pdf"); Imports IronPdf ' Instantiate Renderer Private renderer As New DocxToPdfRenderer() ' Render from DOCX file Private pdf As PdfDocument = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx") ' Save the PDF pdf.SaveAs("pdfFromDocx.pdf") $vbLabelText $csharpLabel IronPDF 包括一個專門的 DocxToPdfRenderer 類,處理 DOCX 的本地轉換。 API 與 IronPDF 中的其他渲染類型保持一致,基本的轉換只需要兩行代碼。 通過渲染器的屬性可以獲得頁面設置和格式化的高級選項。 了解更多有關 DOCX 到 PDF 轉換 的特性 使用 Spire.Doc 將 Spire.PDF 用於 DOCX 到 PDF 轉換: Spire.PDF 本身不能將 DOCX 轉換為 PDF; 然而,可以使用 Spire.Doc 庫來完成這個轉換,然後可以使用 Spire.PDF 處理結果 PDF。 using Spire.Doc; // Create a Document object Document doc = new Document(); // Load a Word document doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.docx"); // Save the document to PDF doc.SaveToFile("ToPDF.pdf", FileFormat.PDF); // Dispose of resources doc.Dispose(); using Spire.Doc; // Create a Document object Document doc = new Document(); // Load a Word document doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.docx"); // Save the document to PDF doc.SaveToFile("ToPDF.pdf", FileFormat.PDF); // Dispose of resources doc.Dispose(); Imports Spire.Doc ' Create a Document object Private doc As New Document() ' Load a Word document doc.LoadFromFile("C:\Users\Administrator\Desktop\Sample.docx") ' Save the document to PDF doc.SaveToFile("ToPDF.pdf", FileFormat.PDF) ' Dispose of resources doc.Dispose() $vbLabelText $csharpLabel 這種方式需要購買和管理一個額外的庫(Spire.Doc),增加了成本和複雜性。 IronPDF 提供直接 DOCX 到 PDF 轉換的內建支持,這意味著您可以節約時間而不需要額外的庫來完成此任務。 而 Spire.PDF 本身不能直接處理 DOCX 到 PDF 的轉換,需用 Spire.Doc 庫來將 DOCX 文件轉換為 PDF。 性能指標揭示了什麼? 通過測試和用戶反饋,以下是庫在真實世界使用中的對比: 渲染性能 對於基本佈局,Spire.PDF 對靜態表格或基本樣式文本等簡單內容進行了優化,使其在這些用例中快速高效。 然而,當處理包含現代 CSS3 功能、JavaScript 或響應式佈局的複雜 HTML 時,IronPDF 的基於 Chrome 的引擎提供了更精確的結果,代價是稍高的渲染時間。 內存使用 由於其簡單的渲染引擎,與 IronPDF(150-200MB)相比,Spire.PDF 的內存使用量通常較少(80-120MB)。 然而,這是以渲染保真度和特性支持為代價的。 對於內存至關重要且 HTML 複雜性最低的應用而言,Spire.PDF 可能更具優勢。 線程和異步支持 IronPDF 提供本地 async/await 支持,使用類似於 RenderHtmlAsPdfAsync() 的方法,使其適合現代 .NET 應用。 Spire.PDF 需要手動線程管理和 STA 線程,為多線程應用程序增加了複雜性。 IronPDF定價和許可 定價和許可模式如何比較? IronPDF 提供各種級別和附加特徵的許可購買選項。 如果您還沒有準備好購買許可證,IronPDF 提供了一個30 天的免費試用版。 定價(截至 2025 年): 永久使用許可證: 提供一系列永久使用許可證,具體取決於您的團隊規模、專案需求和地點數量。 每種授權類型都包含郵件支持。 開發者還可以購買 Iron Suite,此套件讓您以兩個的價格得到 Iron Software 的所有產品。 Plus 許可證: 支持三位開發者、三個地點和三個專案,這一步是繼 Lite 許可證之後的升級,費用是 $1,199。 加強授權提供即時聊天支持和電話支持,除此之外還擁有基本的郵件支持。 Professional 許可證:此許可證適合較大的團隊,支持十位開發者、十個地點和十個專案,費用為 $2,399。 除前一級別的聯絡支持頻道,同時提供屏幕共享支持。 免版稅再分發: IronPDF的許可還提供額外的免版稅再分發保障$2,399 不中斷的產品支持:IronPDF 提供持續的產品更新、安全功能升級以及其工程團隊的支持,價格為 999 美元/年,或一次性購買 1,999 美元的 5 年保障。 Lite 許可: 此許可價值 $799 並支持一個開發者、一個位置和一個項目。 IronPDF 提供靈活的許可選項,擁有永久許可和包含全面支持 Spire.PDF 的定價和許可 Spire.PDF 提供一系列根據您的需求的許可。 - 免費版本: Spire.PDF 提供免費版本,限於 10 個 PDF 頁面; 超過這個限制,您需要購買付費許可。 開發者訂閱: 根據您需要的支持類型,價格為每年 999 美元或 1,199 美元,此選項針對需要建立與 PDF 間相關功能但不需要 OEM 分發權限的開發者。 開發者 OEM 訂閱: 價格約為每年 2,549–4,248 美元,取決於您需要多少支持,包括免費升級、高級支持和將應用分發給終端用戶的能力。 小企業網站: 根據您想要的支持,價格為 4,599 美元或 7,598 美元,這個許可級別支持最多 10 個開發者在多達 10 個地點,提供 1 年的更新和免費的在線技術資源。 OEM 網站訂閱: 價格為 13,088–16,687 美元每年,取決於您所需的支持類型,針對需要在多個地點部署應用的大型企業。 IronPDF IronPDF 在提供廣泛的文檔和支持方面表現出色: 哪個庫提供更好的文檔和支持? 24/5 支持: 活躍的工程師支援可用。 視頻教程: 在 YouTube 上提供逐步視頻指南。 社區論壇: 活躍的社群提供額外支援。 定期更新: 每月的產品更新,以確保最新功能和安全補丁。 全面的文檔:涵蓋所有功能的廣泛且用戶友好的 文檔。 PDF API 參考: 提供 API 參考,以便讓您充分發揮我們工具提供的功能。 欲了解更多信息,請查看 IronPDF 的廣泛文檔,並訪問 Iron Software YouTube 頻道。 論壇: Spire.PDF 有一個論壇,使用者可以在其中互動,分享使用技巧、用途和遇到的問題。 程式碼範例: Spire.PDF 在其網站上提供程式碼範例,顯示如何在各種使用情境中使用該產品。 電子報: Spire.PDF 提供開發者電子報,用於公告更新和其他重要資訊。 支援: 支援主要通過論壇進行,根據許可版本,額外收費提供付費的電子郵件/電話支援。 一般開發者的經驗和反饋如何? 根據社群反饋和實際使用情況: IronPDF 反饋 開發者一致稱讚 IronPDF 能夠提供“更全面和準確的原始 HTML 頁面表示”。 該庫能夠處理複雜的 CSS3 布局、JavaScript 密集型頁面和現代 Web 框架,使其成為需要高保真 PDF 生成的應用程式的首選。 常見的正面點包括: 直觀的 API 設計可以減少開發時間 優秀的渲染品質,匹配瀏覽器輸出 全面的文件說明,包含實用範例 反應迅速的技術支援 Spire.PDF 反饋 社群反饋顯示“Spire 生成的 PDF 檔案只是一個圖像。 有些CSS甚至不正確,比如忽略了粗體字體。” 此外,JavaScript 支援仍計畫中,“因為太困難,短時間內無法完成”,根據官方支援回應。 常見的問題包括: 有限的 CSS3 和現代 Web 標準支援 缺乏 JavaScript 執行能力 HTML 轉換需要複雜的執行緒要求 某些功能需要額外的庫要求 何時應選擇每個庫? 選擇IronPDF當: 您的 HTML 包含現代 CSS3、JavaScript 或響應式設計 您需要與瀏覽器輸出匹配的像素級渲染 需要跨平臺部署(Linux、macOS、Docker) 您希望許可中包含全面的支援 您需要內建功能,例如 DOCX 轉換,而不需額外的庫 您的團隊重視快速開發和編碼最小化 選擇 Spire.PDF 的時機: 您的 PDF 僅包含基本的 HTML 和簡單的樣式 記憶體使用是一個關鍵限制 您主要在 Windows 環境中工作 您的 HTML 不需要 JavaScript 執行 您已在堆疊中使用其他 Spire 產品 結論 總之,IronPDF 和 Spire.PDF for .NET 都是適合處理 .NET 應用中的 PDF 文件的工具。 IronPDF 在易用性、跨平臺相容性和支援現代 Web 標準方面表現突出,是您提升 PDF 文件處理水平的首選。 Spire.PDF 提供了一套全面的功能,但對於複雜任務可能需要更多努力,並且缺乏現代 Web 應用所需的渲染精度。 對於需要準確 HTML 到 PDF 轉換或可靠跨平臺支援的開發者,IronPDF 提供一個更完整的解決方案。 其基於 Chrome 的渲染引擎、直觀的 API 和全面的支援,特別適合企業應用和複雜文件生成場景。 您可以嘗試30 天免費試用以檢查其可用功能。 請注意Spire.PDF 是其各自所有者的註冊商標。 此網站沒有與 Spire.PDF 有關聯、認可或贊助。 所有產品名稱、徽標和品牌均為其各自所有者的財產。 比較僅供信息參考,並反映撰寫時公開可用的信息。 常見問題解答 怎樣在 C# 中將 HTML 轉換為 PDF? 您可以使用 IronPDF 的 RenderHtmlAsPdf 方法將 HTML 字符串轉換為 PDF。您還可以使用 RenderHtmlFileAsPdf 將 HTML 文件轉換為 PDF。 IronPDF 在 HTML 到 PDF 轉換中是否支持 JavaScript 執行? 是的,IronPDF 支持完整的 JavaScript 執行,使其非常適合將動態內容或依賴 JavaScript 的佈局轉換為 PDF。 IronPDF 支持哪個平台進行 PDF 轉換? IronPDF 提供多個平台的本地支持,包括 Windows、Linux、macOS、Docker,以及 Azure 和 AWS 等雲端服務。 IronPDF 的記憶體使用情況如何與 Spire.PDF 比較? IronPDF 使用的記憶體略多,通常為 150-200MB,相比之下,Spire.PDF 為 80-120MB,但它提供更好的渲染質量並支持現代的 async/await 模式。 IronPDF 能直接將 DOCX 文件轉換為 PDF 嗎? 是的,IronPDF 包含通過其 DocxToPdfRenderer 類進行 DOCX 到 PDF 轉換的內建功能,無需其他庫。 IronPDF 的許可選項有哪些? IronPDF 的永久許可證起價為 $749 適用於單一開發者,包括所有功能和支持。它還提供包含 9 個產品的 Iron Suite 套件,售價 $1,498。 IronPDF 提供哪些支援? IronPDF 通過電子郵件、聊天、電話和螢幕共享提供 24/5 工程支持,確保完整的協助。 我如何保障 IronPDF 的 PDF 安全和加密? IronPDF 通過其 SecuritySettings 類支持 AES-256 加密和密碼保護,允許對 PDF 權限進行細緻控制。 IronPDF 適合渲染具有現代 CSS 的複雜網頁嗎? 是的,IronPDF 使用全 Chrome 渲染引擎,支持 CSS3、flexbox、網格佈局、web 字體和 JavaScript,適合複雜網頁。 IronPDF 免費版本的限制是什麼? IronPDF 提供功能齊全的 30 天免費試用版本,沒有頁數限制,提供充足的時間測試所有功能在購買前。 IronPDF 如何確保高質量的 HTML 渲染? IronPDF 提供與 Chrome 瀏覽器輸出匹配的像素精確渲染,確保高保真和準確呈現原始 HTML 內容。 Jacob Mellor 立即與工程團隊聊天 首席技术官 Jacob Mellor 是 Iron Software 的首席技術官,作為 C# PDF 技術的先鋒工程師。作為 Iron Software 核心代碼的原作者,他自開始以來塑造了公司產品架構,與 CEO Cameron Rimington 一起將其轉變為一家擁有超過 50 名員工的公司,為 NASA、特斯拉 和 全世界政府機構服務。Jacob 持有曼徹斯特大學土木工程一級榮譽学士工程學位(BEng) (1998-2001)。他於 1999 年在倫敦開設了他的第一家軟件公司,並於 2005 年製作了他的首個 .NET 組件,專注於解決 Microsoft 生態系統內的複雜問題。他的旗艦產品 IronPDF & IronSuite .NET 庫在全球 NuGet 被安裝超過 3000 萬次,其基礎代碼繼續為世界各地的開發工具提供動力。擁有 25 年的商業經驗和 41 年的編碼專業知識,Jacob 仍專注於推動企業級 C#、Java 及 Python PDF 技術的創新,同時指導新一代技術領袖。 相關文章 發表日期 11月 13, 2025 C# HTML 與 PDF 開源版本比較 IronPDF 將開源 HTML 轉 PDF 庫與 IronPDF for C# 進行比較。探索哪種解決方案能為您的 .NET 專案提供最佳的 PDF 生成功能。 閱讀更多 發表日期 10月 27, 2025 哪個 ASP.NET Core PDF 庫具有最佳價值? 發現適用於 ASP.NET Core 應用程式的最佳 PDF 庫。比較 IronPDF 的 Chrome 引擎與 Aspose 和 Syncfusion 的替代方案。 閱讀更多 發表日期 10月 27, 2025 如何使用 Aspose C# 和 IronPDF 創建 PDF 通過這份針對開發人員設計的分步指南,學習如何使用 Aspose C# 與 IronPDF 創建 PDF。 閱讀更多 ActivePDF vs IronPDF:HTML到PDF教程和全面對比PDFSharp HTML轉PDF範例及教程比較
發表日期 11月 13, 2025 C# HTML 與 PDF 開源版本比較 IronPDF 將開源 HTML 轉 PDF 庫與 IronPDF for C# 進行比較。探索哪種解決方案能為您的 .NET 專案提供最佳的 PDF 生成功能。 閱讀更多
發表日期 10月 27, 2025 哪個 ASP.NET Core PDF 庫具有最佳價值? 發現適用於 ASP.NET Core 應用程式的最佳 PDF 庫。比較 IronPDF 的 Chrome 引擎與 Aspose 和 Syncfusion 的替代方案。 閱讀更多
發表日期 10月 27, 2025 如何使用 Aspose C# 和 IronPDF 創建 PDF 通過這份針對開發人員設計的分步指南,學習如何使用 Aspose C# 與 IronPDF 創建 PDF。 閱讀更多