產品比較 SpirePDF C# HTML到PDF教程和庫比較 Jacob Mellor 更新:8月 6, 2025 下載 IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 將 HTML 轉換為 PDF 是許多 .NET 應用程式的重要需求,從產生發票和報告到建立文件和電子書。 當您在 IronPDF 和 Spire.PDF 之間選擇,以滿足 HTML 轉換為 PDF 的需求時,瞭解它們的優點、限制和實際效能變得非常重要。 這份全面的比較研究了這兩個函式庫的功能,可協助您針對 PDF 產生的需求做出明智的決定。 IronPDF 和 Spire.PDF 在 HTML 至 PDF 轉換方面的主要差異為何? IronPDF for .NET 是一個多功能的 .NET PDF 函式庫,可處理各種 PDF 相關任務,包括轉換 PDF 檔案、編輯 PDF、建立 PDF、閱讀 PDF 檔案等。 它以易於整合至現有的 .NET 應用程式、高效能,以及支援 HTML5、CSS3 和 JavaScript 等現代網路標準而聞名。 IronPDF 基於 Chrome 的渲染引擎可確保從 HTML 內容生成像素完美的 PDF。 Spire.PDF for .NET 是另一個功能強大的 .NET PDF 函式庫,提供 PDF 建立與操作的全面功能。 它支援各種 PDF 功能,例如文字和影像擷取、PDF 表單填寫和數位簽章。 雖然功能全面,但其 HTML 至 PDF 的轉換功能有特定的限制,開發人員應加以考慮。 產品比較概述 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.2s 典型 0.簡單 HTML 需時 5-0.8 秒 Spire:基本 HTML 更快速 Security & Encryption 加密等級 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:更好的除錯 Licensing & Pricing 入門級 Lite: $799 (1 dev, 1 project) Developer: $1,199/year (1 dev) IronPdf:永久授權 Team License Plus: $1,199 (3 devs, 3 projects) 網站:$4,599 (10 位開發人員) IronPdf:更好的團隊價值 套件選項 Iron Suite: $1,498 (9 products) 無套件選項 IronPdf:卓越的價值 Support 支援包括 是,24/5 工程支援 僅限於論壇(付費支援另計) IronPdf:包含支援 支援管道 電子郵件、聊天、電話、螢幕分享 論壇、電子郵件(付費層級) IronPdf:更多頻道 Best For 用例 現代網路應用程式、複雜的 HTML/CSS 簡單的文件、基本的版面設計 依據上下文 註 Spire.PDF 以較低的記憶體使用率提供基本的 PDF 功能,而 IronPDF 則在 HTML 渲染和現代網路標準支援方面表現優異。請根據複雜的 HTML 呈現或最少的資源使用量來選擇。 如需 IronPDF 的詳細資訊,請造訪 IronPDF.com。 不同程式庫的跨平台相容性如何比較? IronPDF。 IronPDF 以其廣泛的跨平台相容性脫穎而出。 它支援 .NET Framework 內的多種環境,確保在不同平台上的無縫操作。 以下是 IronPDF 的平台兼容性摘要: .NET版本: 完全以 C#、VB.NET 和 F# 撰寫,並支援這些語言。 .NET Core (8, 7, 6, 5, and 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 作業系統中運作,卻缺乏 IronPDF 所擁有的對 Linux 和 macOS 的原生支援。 在 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。 IronPDF 完全支援現代網路標準,因此您可以放心,IronPDF 將持續從您的 HTML 內容傳回像素完美的 PDF。 IronPDF 也可以轉換其他格式的 PDF 檔案,例如 DOCX、影像、RTF 等。 PDF 生成:使用 IronPdf,您可以从 URL、ASPX 文件或 HTML 字符串生成 PDF。 安全功能:有了 IronPDF,您總是可以放心,任何敏感的 PDF 檔案都是安全的,這要歸功於它的安全功能。 使用 IronPDF 加密您的 PDF 檔案、設定密碼,以及為 PDF 檔案設定權限。 PDF編輯功能:使用 IronPDF,您可以處理現有的 PDF 文件、編輯它們,並輕鬆讀取 PDF 檔案。 IronPdf 提供的編輯功能包括加入頁首和頁尾、在 PDF 頁面上印上文字和圖片、在 PDF 上加入自訂水印、處理 PDF 表單,以及分割或合併 PDF 檔案。 整合:與 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 to PDF 引擎對現代 CSS 和 JavaScript 的支援有限。 預設的基於 Qt 的外掛程式不支援 CSS 分頁屬性或外部 CSS 檔案辨識,而 JavaScript 執行則是尚未完全實作的規劃功能。 可選用 ChromeHtmlConverter 以獲得更好的渲染效果。 Bootstrap 和現代 CSS 框架渲染 現代網路開發非常依賴 CSS 架構(如 Bootstrap)來進行快速 UI 開發和反應式設計。 PDF 生成函式庫必須準確轉換這些框架,以維持專業的文件品質和設計一致性。 IronPdf:完整的 Bootstrap 與 Tailwind 支援。 IronPDF 的 Chromium 引擎提供對所有現代 CSS 框架的完整支援: Bootstrap 5:支援所有回應式元件的完整 flexbox 與 CSS Grid Bootstrap 4:完整的卡片佈局、導航系統和實用類型 Tailwind CSS:所有實用為先的類別和反應式修改器 基礎:完整的網格和元件系統 現代 CSS3:Flexbox、Grid、自訂屬性、動畫和變形 以實例證明:Bootstrap首頁和Bootstrap範本的轉換精確到像素。 程式碼範例:使用 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-to-PDF 引擎對於現代 CSS 架構有很大的限制,這在他們自己的說明文件中已經指出: 有限的 CSS 支援:只有基本的 CSS,沒有全面的 CSS3 功能 No Flexbox:Bootstrap 4/5 在很大程度上依賴於不支援的 flexbox。 No CSS Grid:使用 CSS Grid 的現代佈局系統將無法正確呈現 JavaScript 的限制:與現代瀏覽器相比,JavaScript 的執行能力極低 Bootstrap 3 最大值:只有較舊的以表格為基礎的 Bootstrap 版本才能穩定運作 複雜的佈局失敗:現代的回應式設計需要大量的變通 開發人員報告的 Spire.PDF HTML 轉換問題: Bootstrap 導覽列呈現布局問題 使用 flexbox 的卡片元件無法正確對齊 在 PDF 輸出中會忽略回應式網格的分割點 現代 CSS 功能需要手動重寫 CSS 或回退 開發影響:使用 Bootstrap 4+ 或現代 CSS 框架的應用程式在使用 Spire.PDF 時會面臨重大挑戰。 開發團隊必須 1.維護專門用於 PDF 生成的獨立簡化 CSS(雙重維護) 2.降級至 Bootstrap 3 或完全放棄框架(限制現代功能) 3.廣泛測試並手動修正每個元件 (耗時且易碎) 對於需要現代 CSS 架構支援的專案而言,Spire.PDF 的 HTML 渲染限制使其不適合在沒有重大額外開發工作和持續維護負擔的情況下使用。 如需詳細的 Bootstrap 相容性資訊,請參閱 Bootstrap & Flexbox CSS 指南。 如何比較常見 PDF 任務的程式碼範例? 為了說明 IronPDF 和 Spire.PDF for .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 執行和現代網路字型都會完全呈現在 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. 轉換必須在 Single Thread Apartment (STA) 線程中執行,增加了實作的複雜性。 使用者反映"Spire 產生的 PDF 檔案只是一張影像。 有些 css 甚至不正確,例如忽略粗體字型",顯示出 CSS 呈現保真度的限制。 IronPDF 使用 Chrome 的渲染引擎進行高保真 HTML 至 PDF 的轉換,由於其支援現代網路標準,因此可確保正確呈現網頁內容。 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 類提供了管理 PDF 安全性的全面 API。 您可以使用布林屬性控制個別權限、設定不同等級的列印權限,以及將密碼與其他安全性設定分開管理。 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 提供專用的 redaction 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 for .NET 的數位簽章實作遵循 .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 的數位簽章; 然而,此過程會較為手動,需要花更多心力來執行。 What Methods Are Available for 添加水印 PDFs? 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 提供了一種簡單的方法來新增文字水印,同時讓您完全控制水印的位置和設計。 如果您對這些語言有經驗,IronPdf 對 HTML 和 CSS 的使用會讓翻譯過程變得更容易。 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 蓋章器和 BarCode 整合。 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 將 DOCX 轉換為 PDF:Spire.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 已針對靜態表格或基本樣式的文字等簡單內容進行最佳化,因此在這些使用個案中,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提供不同等級和附加功能的購買授權。 開發人員也可以購買 Iron Suite,以兩件產品的價格即可使用 Iron Software 的所有產品。 如果您尚未準備好購買授權,IronPDF 提供 免費試用,為期 30 天。 定價(截至 2025 年): 永久授權:根據您的團隊規模、專案需求和地點數量,提供一系列永久授權。 每種授權類型都附有電子郵件支援。 Lite License: 此授權費用為 $799 並支援一位開發人員、一個地點和一個專案。 Plus License: 支援三位開發人員、三個地點和三個專案,這是 Lite License 的進階版本,費用為 $1,199。 Plus License 除了提供基本的電子郵件支援外,還提供聊天支援和電話支援。 Professional License:此 License 適合較大規模的團隊,可支援十位開發人員、十個地點和十個專案,費用為 $2,399 。 它提供與前幾層相同的聯絡支援管道,但也提供螢幕分享支援。 免版稅再發行: IronPDF 的授權也提供免版稅再發行的保障,只需額外支付 $2,399 的費用。 不間斷的產品支援: IronPdf 提供持續的產品更新、安全功能升級,以及工程團隊的支援,費用為 999 美元/年或一次性購買 5 年保障的 1,999 美元。 Iron Suite:只需 $1,498 即可使用所有 Iron Software 產品,包括 IronPDF、IronOCR、IronWord、IronXL、IronBarcode、IronQR、IronZIP、IronPrint 和 IronWebScraper。 。 IronPDF 提供靈活的授權選項,包含永久授權和全面支援 Spire.PDF 定價與授權 Spire.PDF 可根據您的需求提供一系列授權。 免費版本: Spire.PDF 提供限於 10 頁 PDF 的免費版本; 除此之外,您需要購買付費授權。 Developer Subscription:此選項的價格為每年 999 美元或 1199 美元,視您需要的支援類型而定,適用於需要建立 PDF 相關功能但不需要 OEM 發行權的開發人員。 開發人員 OEM 訂閱:每年費用約為 $2,549 - $4,248,視您需要的支援金額而定,包括免費升級、優質支援,以及將應用程式分發給終端使用者的功能。 Site 小型企業:此授權層級的價格為 $4,599 或 $7,598,視您需要的支援而定,最多支援 10 個地點的 10 位開發人員、1 年的更新,以及免費線上技術資源。 Site OEM 訂閱:每年費用為 13,088 - 16,687 美元,視您所需的支援類型而定,主要針對需要將應用程式部署到多個地點的大型企業。 哪個函式庫能提供更好的文件和支援? IronPDF。 IronPdf 擅長於提供廣泛的文件和支援: 全面的文件:廣泛且友善的文件,涵蓋所有功能。 24/5支援:提供積極的工程師支援。 視訊教學:YouTube 上提供了分步視訊指南。 社群論壇:參與社群以獲得更多支援。 定期更新:每月進行產品更新,以確保提供最新的功能和安全修補程式。 PDF API 參考:提供 API 參考,讓您可以充分利用我們的工具所提供的功能。 如需瞭解更多資訊,請查看 IronPdf 的廣泛說明文件,並請造訪 Iron Software YouTube 頻道。 Spire.PDF for .NET 文件和支援。 論壇: Spire.PDF 設有一個論壇,使用者可以在此互動,分享使用 Spire.PDF 的技巧、用途和遇到的問題。 程式碼範例: Spire.PDF 在其網站上提供程式碼範例,說明如何在各種使用情況下使用產品。 電子報: Spire.PDF 提供開發人員電子報,以提供更新公告和其他重要資訊。 支援:主要透過論壇提供支援,並依授權版本提供付費的電子郵件/電話支援。 有哪些常見的開發人員經驗與回饋? 以社群回饋和實際使用為基礎: IronPdf 反饋 開發人員一致稱讚 IronPDF"更全面、精確地呈現原始 HTML 頁面"。 該函式庫能夠處理複雜的 CSS3 佈局、JavaScript 繁重的頁面,以及現代的網頁框架,使其成為需要高保真 PDF 生成的應用程式的首選。 常見的正點包括 直覺的 API 設計,可縮短開發時間 與瀏覽器輸出相匹配的優異渲染品質 包含實例的全面文件 反應迅速的技術支援 Spire.PDF 反饋 社群回饋指出:"Spire 產生的 PDF 檔案只是一個影像。 有些 css 甚至不對,例如忽略粗體字型"。 此外,根據官方的支援回覆,JavaScript 支援仍屬於計劃中的功能,"將會非常困難,無法在短時間內完成"。 常見的問題包括 有限的 CSS3 與現代網頁標準支援 缺乏 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 在易用性、跨平台相容性和對現代網路標準的支援方面表現優異,因此如果您希望在處理 PDF 文件時更上一層樓,IronPDF 將是您的首選。 Spire.PDF 提供了一套全面的功能,但在執行複雜的任務時可能需要花費更多心力,而且缺乏現代網路應用程式所要求的呈現精準度。 對於使用當代網路技術、需要精確的 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 & Iron Suite .NET 庫在全球 NuGet 被安裝超過 3000 萬次,其基礎代碼繼續為世界各地的開發工具提供動力。擁有 25 年的商業經驗和 41 年的編碼專業知識,Jacob 仍專注於推動企業級 C#、Java 及 Python PDF 技術的創新,同時指導新一代技術領袖。 相關文章 發表日期 12月 18, 2025 哪個 ASP PDF 函式庫能為 .NET Core 開發帶來最大價值? 發現適用於 ASP.NET Core 應用程式的最佳 PDF 庫。比較 IronPDF 的 Chrome 引擎與 Aspose 和 Syncfusion 的替代方案。 閱讀更多 發表日期 12月 3, 2025 IronPDF vs iTextSharp HTML to PDF with Header and Footer in PDF Document 比較 iTextSharp 和 IronPDF 在 PDF 中加入頁首和頁尾的功能。程式碼範例、頁碼和 HTML 標頭實作。 閱讀更多 發表日期 12月 3, 2025 使用 IronPDF 解決 iTextSharp HTML to PDF 中的 「文件無頁 」錯誤 iTextSharp HTML to PDF 在解析失敗時不會發生頁面錯誤。瞭解 XMLWorker 為何會有同樣的問題,並發現 IronPDF 可靠的 HTML 轉換解決方案。 閱讀更多 ActivePDF vs IronPDF:HTML到PDF教程和全面對比PDFSharp HTML轉PDF範例及教程比較
發表日期 12月 18, 2025 哪個 ASP PDF 函式庫能為 .NET Core 開發帶來最大價值? 發現適用於 ASP.NET Core 應用程式的最佳 PDF 庫。比較 IronPDF 的 Chrome 引擎與 Aspose 和 Syncfusion 的替代方案。 閱讀更多
發表日期 12月 3, 2025 IronPDF vs iTextSharp HTML to PDF with Header and Footer in PDF Document 比較 iTextSharp 和 IronPDF 在 PDF 中加入頁首和頁尾的功能。程式碼範例、頁碼和 HTML 標頭實作。 閱讀更多
發表日期 12月 3, 2025 使用 IronPDF 解決 iTextSharp HTML to PDF 中的 「文件無頁 」錯誤 iTextSharp HTML to PDF 在解析失敗時不會發生頁面錯誤。瞭解 XMLWorker 為何會有同樣的問題,並發現 IronPDF 可靠的 HTML 轉換解決方案。 閱讀更多