产品比较 SpirePDF C# HTML到PDF教程及库对比 Jacob Mellor 已更新:八月 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 to PDF 渲染引擎 完整的Chrome V8引擎 自定义HTML解析器 IronPDF:98%+浏览器保真度 CSS3/HTML5支持 完全支持 有限(仅基本CSS) IronPDF:现代Web标准 JavaScript执行 完整的JavaScript支持 计划中但未实现 IronPDF:动态内容就绪 Web字体 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:更多渠道 最佳适用 使用用例 现代Web应用,复杂的HTML/CSS 简单文档,基本布局 依赖于上下文 class="table-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: 支持使用的IDE包括 Microsoft Visual Studio 和 JetBrains Rider & ReSharper 操作系统和处理器: 支持包括 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。 Features 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框架渲染 现代Web开发严重依赖于CSS框架,例如Bootstrap,用于快速的UI开发和响应式设计。 PDF生成库必须准确转换这些框架,以保持专业文档的质量和设计一致性。 IronPDF。: Full Bootstrap and Tailwind Support IronPDF的Chromium引擎提供了对所有现代CSS框架的完整支持: Bootstrap 5:完全支持flexbox和CSS Grid以及所有响应组件 Bootstrap 4:完整的卡片布局,导航系统和实用类 Tailwind CSS:所有实用优先的类和响应修饰符 Foundation:完整的网格和组件系统 Modern CSS3:Flexbox,网格,自定义属性,动画,和变换 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 Grid:使用CSS Grid的现代布局系统将无法正确渲染 JavaScript限制:相比现代浏览器,JavaScript执行很少 Bootstrap 3最大化:只有较旧的基于表的Bootstrap版本可以可靠工作 复杂布局失败:现代响应设计需要广泛的变通办法 开发人员报告的Spire.PDF HTML转换问题: Bootstrap导航栏渲染中存在布局问题 含flexbox的卡片组件未正确对齐 响应式网格断点在PDF输出中被忽略 现代CSS功能需要手动CSS重写或替代方案 开发影响:使用Bootstrap 4+或现代CSS框架的应用使用Spire.PDF会面临重大挑战。 开发团队必须: 保持专门用于PDF生成的简化CSS(双倍维护) 降级为Bootstrap 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执行和现代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利用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盖章和条码集成。 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优化用于简单内容,如静态表格或基本样式文本,使其在这些用例中快速而有效。 然而,当处理包含现代CSS3功能、JavaScript或响应布局的复杂HTML时,IronPDF的基于Chrome的引擎更能提供准确的结果,但渲染时间略高。 内存使用 由于其更简单的渲染引擎,Spire.PDF通常使用更少的内存(80-120MB)相比IronPDF(150-200MB)。然而,这也影响了渲染保真度和功能支持。 对于内存至关重要且HTML复杂性较低的应用程序,Spire.PDF可能更具优势。 线程和异步支持 IronPDF提供原生的async/await支持,例如RenderHtmlAsPdfAsync()方法,使其适用于现代.NET应用程序。 Spire.PDF需要手动的线程管理与STA线程,增加了多线程应用的复杂性。 定价和许可模型如何比较? IronPDF。 Pricing and Licensing IronPDF提供多种级别和附加功能供您购买许可证。 开发者还可以购买Iron Suite,可以以两个产品的价格获得Iron Software的所有产品。 如果您尚未准备好购买许可证,IronPDF提供免费试用,为期30天。 价格(截至2025年): 永久许可证:根据您的团队规模、项目需求和地点数量提供一系列永久许可证。 每种许可证类型都配有电子邮件支持。 Lite License: 该许可费用为$799,支持一个开发者、一个位置和一个项目。 Plus许可证:支持三名开发人员、三个地点和三个项目,这是从Lite许可证的下一步,费用是$1,199。 Plus许可证除了电子邮件支持外,还提供聊天支持和电话支持。 专业许可证:适用于较大团队的许可证,支持十名开发人员、十个地点和十个项目,费用是$2,399。 该许可证提供与之前的层级相同的联系方式支持渠道,并额外支持屏幕共享。 免版税再分发: IronPDF的许可还提供免版税的再分发覆盖,额外费用为$2,399 连续产品支持:IronPDF提供持续的产品更新、安全功能升级以及来自其工程团队的支持,费用为$999/年或一次性购买$1,999即可获得5年覆盖。 Iron Suite: 以 $1,498 的价格,您将获得Iron Software产品的全面访问,包括IronPDF、IronOCR、IronWord、IronXL、IronBarcode、IronQR、IronZIP、IronPrint 和 IronWebScraper。 IronPDF提供灵活的许可选项,带有永久许可证和包括全面支持 Spire.PDF定价与许可 Spire.PDF根据您的需求提供一系列许可证。 免费版本:Spire.PDF提供了一个免费版本,限制为10个PDF页面; 超过此限制,您需要购买付费许可证。 开发者订阅:价格为每年$999到$1,199不等,视您需要的支持类型而定,此选项适用于不需要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 for .NET文档与支持 论坛: Spire.PDF 有一个论坛,用户可以在其中互动,分享关于 Spire.PDF 的使用技巧、用法和遇到的问题。 代码示例: Spire.PDF 在其网站上提供代码示例,以展示如何在各种用例中使用产品。 新闻通讯: Spire.PDF 提供开发者新闻通讯,用于发布更新公告和其他重要信息。 支持: 支持主要通过论坛提供,不同许可证版本可以按需支付附加费用获取邮件/电话支持。 常见的开发者体验和反馈是什么? 基于社区反馈和实际使用情况: IronPDF。 Feedback 开发者一致赞扬 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 文档时提升效率,这是首选。 Spire.PDF 功能全面,但可能需要在复杂任务上付出更多努力,且缺乏现代网络应用要求的渲染精度。 对于从事现代网络技术开发、需要准确的 HTML 到 PDF 转换,或需要可靠的跨平台支持的开发者来说,IronPDF 提供了一个更完整的解决方案。 其基于 Chrome 的渲染引擎、直观的 API 和全面的支持使其特别适合企业应用和复杂文档生成场景。 你可以试用 30 天免费试用 来了解他们的可用特性。 [{i:(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 包含内置的 DOCX 到 PDF 转换功能,通过其 DocxToPdfRenderer 类,无需额外的库。 IronPDF的许可选项是什么? IronPDF 提供的永久许可证起价为 749 美元,适用于单个开发者,包括所有功能和支持。它还提供 Iron Suite 套装,包括 9 个产品,售价 1,498 美元。 IronPDF提供什么样的支持? IronPDF 提供 24/5 的工程支持,通过电子邮件、聊天、电话和屏幕共享,以确保所有许可证的全面帮助。 如何确保使用 IronPDF 提供的 PDF 安全和加密? IronPDF 支持通过其 SecuritySettings 类进行 AES-256 加密和密码保护,允许对 PDF 权限进行精细控制。 IronPDF 是否适合渲染具有现代 CSS 的复杂网页? 是的,IronPDF 使用全 Chrome 渲染引擎,支持 CSS3、flexbox、grid 布局、网页字体和 JavaScript,非常适合复杂网页。 IronPDF 免费版有哪些限制? IronPDF 提供功能齐全的 30 天免费试用版,没有页面限制,提供充足的机会测试其所有功能,然后再购买。 IronPDF 如何确保高质量的 HTML 渲染? IronPDF 提供与 Chrome 浏览器输出相匹配的像素完美渲染,确保高保真度和对原始 HTML 内容的准确再现。 Jacob Mellor 立即与工程团队聊天 首席技术官 Jacob Mellor 是 Iron Software 的首席技术官,是 C# PDF 技术的先锋工程师。作为 Iron Software 核心代码库的原始开发者,自公司成立以来,他就塑造了公司的产品架构,并与首席执行官 Cameron Rimington 一起将其转变成一家公司,拥有50多人,服务于 NASA、特斯拉和全球政府机构。Jacob 拥有曼彻斯特大学 (1998-2001) 的一级荣誉土木工程学士学位。1999 年在伦敦创办了自己的第一家软件公司,并于 2005 年创建了他的第一个 .NET 组件后,他专注于解决微软生态系统中的复杂问题。他的旗舰 IronPDF 和 IronSuite .NET 库在全球已获得超过 3000 万次的 NuGet 安装,其基础代码继续为全球使用的开发者工具提供支持。拥有 25 年商业经验和 41 年编程经验的 Jacob 仍专注于推动企业级 C#、Java 和 Python PDF 技术的创新,同时指导下一代技术领导者。 相关文章 已发布十一月 13, 2025 比较 C# HTML 到 PDF 开源与 IronPDF 比较开源 HTML 到 PDF 库与 IronPDF for C#。发现哪个解决方案为您的 .NET 项目提供最佳的 PDF 生成能力。 阅读更多 已发布十月 27, 2025 哪种 ASP.NET Core PDF 库性价比最高? 发现适合ASP.NET Core应用程序的最佳PDF库。比较IronPDF的Chrome引擎与Aspose和Syncfusion的替代品。 阅读更多 已发布十月 27, 2025 如何使用 Aspose C# 与 IronPDF 创作 PDF 通过此逐步指南,学习如何使用 Aspose C# 与 IronPDF 创建 PDF,专为开发人员设计。 阅读更多 ActivePDF与IronPDF:HTML到PDF教程及全面对比PDFSharp HTML 到 PDF 示例和教...
已发布十一月 13, 2025 比较 C# HTML 到 PDF 开源与 IronPDF 比较开源 HTML 到 PDF 库与 IronPDF for C#。发现哪个解决方案为您的 .NET 项目提供最佳的 PDF 生成能力。 阅读更多
已发布十月 27, 2025 哪种 ASP.NET Core PDF 库性价比最高? 发现适合ASP.NET Core应用程序的最佳PDF库。比较IronPDF的Chrome引擎与Aspose和Syncfusion的替代品。 阅读更多
已发布十月 27, 2025 如何使用 Aspose C# 与 IronPDF 创作 PDF 通过此逐步指南,学习如何使用 Aspose C# 与 IronPDF 创建 PDF,专为开发人员设计。 阅读更多