使用IRONPDF 如何使用IronPDF构建Blazor PDF查看器 Curtis Chau 已更新:2026年2月27日 下载 IronPDF NuGet 下载 DLL 下载 Windows 安装程序 免费试用 LLM副本 LLM副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在 Grok 中打开 向 Grok 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 Blazor PDF查看器通过将PDF文档转换为base64数据URI并在<iframe> 元素中加载结果来内联显示PDF文档。 IronPDF的ChromePdfRenderer 将HTML字符串、实时URL或动态内容转换为PDF字节,支持Blazor Server和Blazor WebAssembly应用程序实现完整的PDF生成和显示能力,无需外部查看器插件。 业务应用程序通常需要显示发票、合同和报告,而无需将用户重定向到单独的标签页或依赖于不同设备的浏览器PDF支持。 Blazor的组件模型使得在服务器上生成PDF、对其进行编码并将其流式传输到任何页面组件变得简单,只要库可靠地处理转换即可。 本指南涵盖安装、基于URL和HTML的渲染、带有页眉和页脚的定制、使用JavaScript互操作进行浏览器下载、Blazor Server和Blazor WebAssembly方法的比较,以及四个扩展操作:合并、注释、密码保护和用户上传文件显示。 每种技术都提供了Razor组件和等效的顶级C#示例。 开始一个免费的IronPDF试用版,以便跟随本指南中的示例。 如何在Blazor项目中开始使用IronPDF? 开始需要安装NuGet包并将许可证密钥添加到Program.cs。 从Package Manager Console安装IronPDF: Install-Package IronPdf 或者,可以在NuGet Package Manager UI中搜索"IronPdf"并选择最新版本。 使用 NuGet 安装 PM > Install-Package IronPdf 在 IronPDF 上查看 NuGet 快速安装。超过 1000 万次下载,它正以 C# 改变 PDF 开发。 您也可以下载 DLL 或 Windows 安装程序。 安装后,在进行任何PDF操作之前,将您的许可证密钥添加到Program.cs。 IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY-HERE"; IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY-HERE"; $vbLabelText $csharpLabel IronPDF兼容.NET 10、.NET 9、.NET 8、.NET 6和.NET Framework 4.6.2及更高版本。 对于开发和测试,该库可以在没有许可证密钥的情况下运行,但会在生成的PDF上添加水印。 免费试用许可证可以在评估期间去除水印。 IronPDF支持Blazor Server和Blazor WebAssembly项目。 在Blazor Server中,渲染引擎直接在服务器上运行。 在Blazor WebAssembly中,PDF生成需要服务器端API端点; 本指南后面的架构部分解释了这两种方法。 如何在 Blazor 中显示来自 URL 的 PDF 文件? 创建Blazor PDF查看器的最直接方法是将URL转换为PDF并在<iframe>中显示。 IronPDF的ChromePdfRenderer抓取网页并使用与驱动Google Chrome相同的Chrome渲染引擎将其转换为PDF格式,忠实保留CSS、JavaScript输出和布局。 Razor组件方法 以下Razor组件将URL转换为PDF并内联显示。GeneratePdf方法在Blazor Server应用程序的服务器上运行,因此完整的Chrome渲染引擎可用: @page "/pdfviewer" @using IronPdf <h3>PDF Viewer</h3> <button @onclick="GeneratePdf" class="btn btn-primary">Load PDF</button> @if (!string.IsNullOrEmpty(pdfDataUri)) { <iframe src="@pdfDataUri" style="width:100%; height:600px; border:1px solid #ccc; margin-top:20px;"></iframe> } @code { private string pdfDataUri = string.Empty; private async Task GeneratePdf() { var renderer = new ChromePdfRenderer(); // Convert the URL to PDF using the Chrome rendering engine var pdf = await renderer.RenderUrlAsPdfAsync("https://ironpdf.com"); // Encode the PDF bytes as a base64 data URI for iframe display var base64 = Convert.ToBase64String(pdf.BinaryData); pdfDataUri = $"data:application/pdf;base64,{base64}"; } } 顶级C#示例 对于后台服务、控制台应用或服务器端API端点,相同的转换将在任何组件上下文之外使用相同的API调用: using IronPdf; var renderer = new ChromePdfRenderer(); // Fetch and convert the target URL to a PDF document var pdf = await renderer.RenderUrlAsPdfAsync("https://ironpdf.com"); // Save to disk or use BinaryData for in-memory operations pdf.SaveAs("output.pdf"); byte[] pdfBytes = pdf.BinaryData; using IronPdf; var renderer = new ChromePdfRenderer(); // Fetch and convert the target URL to a PDF document var pdf = await renderer.RenderUrlAsPdfAsync("https://ironpdf.com"); // Save to disk or use BinaryData for in-memory operations pdf.SaveAs("output.pdf"); byte[] pdfBytes = pdf.BinaryData; $vbLabelText $csharpLabel PdfDocument对象,包含渲染输出。 BinaryData属性公开原始PDF字节,用于存储、流式传输或显示。 <iframe>使用内建的浏览器工具条显示输出,提供缩放、导航和打印功能。 如何自定义 PDF 生成? IronPDF通过ChromePdfRenderOptions类提供输出控制。 您可以设置纸张大小、调整边距,并为每页添加文本或HTML页眉和页脚。 渲染选项指南涵盖所有可用属性的完整列表。 Razor组件方法 以下组件配置A4纸张并设置边距,为每页添加页眉和页脚文本。 在调用任何渲染方法之前,分配RenderingOptions为它们在渲染器实例中全局应用: @page "/pdfcustom" @using IronPdf <h3>Customized PDF Viewer</h3> <button @onclick="GenerateCustomizedPdf" class="btn btn-primary">Generate Customized PDF</button> @if (!string.IsNullOrEmpty(pdfDataUri)) { <iframe src="@pdfDataUri" style="width:100%; height:600px; border:1px solid #ccc; margin-top:20px;"></iframe> } @code { private string pdfDataUri = string.Empty; private async Task GenerateCustomizedPdf() { var renderer = new ChromePdfRenderer { RenderingOptions = new ChromePdfRenderOptions { PaperSize = IronPdf.Rendering.PdfPaperSize.A4, MarginTop = 25, MarginBottom = 25, MarginLeft = 20, MarginRight = 20, // Header with dynamic date replacement TextHeader = new TextHeaderFooter { CenterText = "Monthly Report - {date}", FontSize = 12 }, // Footer with page numbering TextFooter = new TextHeaderFooter { LeftText = "Confidential", RightText = "Page {page} of {total-pages}", FontSize = 10 } } }; var pdf = await renderer.RenderUrlAsPdfAsync("https://example.com/report"); pdfDataUri = $"data:application/pdf;base64,{Convert.ToBase64String(pdf.BinaryData)}"; } } 顶级C#示例 相同的选项适用于任何.NET上下文。 这种模式在ASP.NET Core最小API或计划报告生成器中工作良好: using IronPdf; using IronPdf.Rendering; var renderer = new ChromePdfRenderer { RenderingOptions = new ChromePdfRenderOptions { PaperSize = PdfPaperSize.A4, MarginTop = 25, MarginBottom = 25, TextHeader = new TextHeaderFooter { CenterText = "Report - {date}", FontSize = 12 }, TextFooter = new TextHeaderFooter { RightText = "Page {page} of {total-pages}", FontSize = 10 } } }; var pdf = await renderer.RenderUrlAsPdfAsync("https://example.com/report"); pdf.SaveAs("customized-report.pdf"); using IronPdf; using IronPdf.Rendering; var renderer = new ChromePdfRenderer { RenderingOptions = new ChromePdfRenderOptions { PaperSize = PdfPaperSize.A4, MarginTop = 25, MarginBottom = 25, TextHeader = new TextHeaderFooter { CenterText = "Report - {date}", FontSize = 12 }, TextFooter = new TextHeaderFooter { RightText = "Page {page} of {total-pages}", FontSize = 10 } } }; var pdf = await renderer.RenderUrlAsPdfAsync("https://example.com/report"); pdf.SaveAs("customized-report.pdf"); $vbLabelText $csharpLabel 模板变量包括TextFooter。 页眉和页脚指南包含两种方法的完整示例。 启用 PDF 下载的最佳方法是什么? 在<iframe>中显示PDF可以处理查看,但用户经常需要下载文件。JavaScript互操作从.NET字节流触发浏览器下载。 有关其他下载和导出模式,请参见导出和保存PDF指南。 Razor组件方法 将IJSRuntime注入组件并调用JavaScript辅助函数以启动下载。 DotNetStreamReference流式传输PDF字节,而无需一次将整个文件加载到JavaScript内存中。 @page "/pdfdownload" @using IronPdf @inject IJSRuntime JSRuntime <h3>Download PDF</h3> <button @onclick="DownloadPdf" class="btn btn-success">Download PDF</button> @code { private async Task DownloadPdf() { var renderer = new ChromePdfRenderer(); var pdf = await renderer.RenderHtmlAsPdfAsync("<h1>Invoice</h1><p>Total: $1,299</p>"); // Stream the PDF bytes to the browser as a downloadable file using var streamRef = new DotNetStreamReference(stream: new MemoryStream(pdf.BinaryData)); await JSRuntime.InvokeVoidAsync("downloadFileFromStream", "invoice.pdf", streamRef); } } 将此JavaScript函数添加到您的App.razor文件中,如微软的Blazor JavaScript互操作文档中所述: window.downloadFileFromStream = async (fileName, contentStreamReference) => { const arrayBuffer = await contentStreamReference.arrayBuffer(); const blob = new Blob([arrayBuffer]); const url = URL.createObjectURL(blob); const anchorElement = document.createElement('a'); anchorElement.href = url; anchorElement.download = fileName ?? ''; anchorElement.click(); anchorElement.remove(); URL.revokeObjectURL(url); }; window.downloadFileFromStream = async (fileName, contentStreamReference) => { const arrayBuffer = await contentStreamReference.arrayBuffer(); const blob = new Blob([arrayBuffer]); const url = URL.createObjectURL(blob); const anchorElement = document.createElement('a'); anchorElement.href = url; anchorElement.download = fileName ?? ''; anchorElement.click(); anchorElement.remove(); URL.revokeObjectURL(url); }; JAVASCRIPT 顶级C#示例 在服务器端API端点,直接使用Results.File返回PDF字节。 浏览器接收文件并带有正确的Content-Disposition头,自动触发下载: using IronPdf; // ASP.NET Core minimal API endpoint app.MapGet("/api/pdf/invoice", async () => { var renderer = new ChromePdfRenderer(); var pdf = await renderer.RenderHtmlAsPdfAsync("<h1>Invoice</h1><p>Total: $1,299</p>"); // Return with file download headers return Results.File(pdf.BinaryData, "application/pdf", "invoice.pdf"); }); using IronPdf; // ASP.NET Core minimal API endpoint app.MapGet("/api/pdf/invoice", async () => { var renderer = new ChromePdfRenderer(); var pdf = await renderer.RenderHtmlAsPdfAsync("<h1>Invoice</h1><p>Total: $1,299</p>"); // Return with file download headers return Results.File(pdf.BinaryData, "application/pdf", "invoice.pdf"); }); $vbLabelText $csharpLabel 如何从 Razor 组件生成 PDF? 从HTML生成PDF可以完全控制布局、数据绑定和样式。 这种方法适合发票、报告和任何从实时应用程序数据构建的文档。 有关更高级的渲染技术,请参见HTML到PDF转换指南。 Razor组件方法 以下组件从C#数据构建一个发票HTML字符串并将其转换为PDF。 ChromePdfRenderer将HTML字符串视为网页,应用所有CSS并使用Chrome引擎渲染它: @page "/invoicedemo" @using IronPdf <h3>Invoice Generator</h3> <button @onclick="GenerateInvoice" class="btn btn-primary">Generate Invoice PDF</button> @if (!string.IsNullOrEmpty(pdfDataUri)) { <iframe src="@pdfDataUri" style="width:100%; height:600px; border:1px solid #ccc; margin-top:20px;"></iframe> } @code { private string pdfDataUri = string.Empty; private async Task GenerateInvoice() { var invoiceHtml = $@" <html> <head> <style> body {{ font-family: Arial, sans-serif; }} .header {{ background-color: #f0f0f0; padding: 20px; }} .invoice-table {{ width: 100%; border-collapse: collapse; }} .invoice-table th, .invoice-table td {{ border: 1px solid #ddd; padding: 8px; }} .total {{ font-weight: bold; font-size: 18px; }} </style> </head> <body> <div class='header'> <h1>Invoice #INV-2025-001</h1> <p>Date: {DateTime.Now:MM/dd/yyyy}</p> </div> <table class='invoice-table'> <thead> <tr> <th>Item</th><th>Quantity</th><th>Price</th><th>Total</th> </tr> </thead> <tbody> <tr> <td>IronPDF License</td><td>1</td><td>$749</td><td>$749</td> </tr> <tr> <td>Priority Support</td><td>1</td><td>$250</td><td>$250</td> </tr> </tbody> </table> <p class='total'>Total Amount: $999</p> </body> </html>"; var renderer = new ChromePdfRenderer(); var pdf = await renderer.RenderHtmlAsPdfAsync(invoiceHtml); pdfDataUri = $"data:application/pdf;base64,{Convert.ToBase64String(pdf.BinaryData)}"; } } 顶级C#示例 相同的HTML字符串方法在任何.NET上下文中都有效,包括控制台应用、后台服务和API端点。 C#字符串插值或模板库在将字符串传递给渲染器之前插入动态数据: using IronPdf; var html = """ <html> <body> <h1>Invoice #INV-2025-001</h1> <table> <tr><th>Item</th><th>Total</th></tr> <tr><td>IronPDF License</td><td>$749</td></tr> <tr><td>Priority Support</td><td>$250</td></tr> </table> <p><strong>Total: $999</strong></p> </body> </html> """; var renderer = new ChromePdfRenderer(); var pdf = await renderer.RenderHtmlAsPdfAsync(html); pdf.SaveAs("invoice.pdf"); using IronPdf; var html = """ <html> <body> <h1>Invoice #INV-2025-001</h1> <table> <tr><th>Item</th><th>Total</th></tr> <tr><td>IronPDF License</td><td>$749</td></tr> <tr><td>Priority Support</td><td>$250</td></tr> </table> <p><strong>Total: $999</strong></p> </body> </html> """; var renderer = new ChromePdfRenderer(); var pdf = await renderer.RenderHtmlAsPdfAsync(html); pdf.SaveAs("invoice.pdf"); $vbLabelText $csharpLabel RenderHtmlAsPdfAsync接受任何有效的HTML字符串,包括内嵌CSS和嵌入式JavaScript。 实现自动处理布局、字体渲染和分页中断。 Blazor Server PDF查看器与Blazor WebAssembly有何不同? 托管模型决定了PDF生成在哪里运行以及字节如何到达浏览器。 了解这一区别可避免在构建Blazor PDF查看器时的常见架构错误。 Blazor Server在服务器上执行所有C#代码。 ChromePdfRenderer在服务器端运行,生成的字节通过现有的SignalR连接推送到浏览器。 这是最简单的集成路径,不需要除本节前面所示内容之外的其他API端点或网络调用。 Blazor WebAssembly使用WASM在浏览器的沙箱中运行C#。 IronPDF的渲染引擎依赖于本机二进制文件,这些文件无法在浏览器沙箱中运行,因此在WASM项目中无法直接使用ChromePdfRenderer。 正确的方法是调用一个服务器端API端点,该端点执行PDF生成并作为响应返回字节。 为Blazor WebAssembly设置PDF生成API 在服务器上,定义一个最小API端点生成并返回PDF: // Program.cs (ASP.NET Core host project) using IronPdf; IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY-HERE"; app.MapGet("/api/pdf/report", async () => { var renderer = new ChromePdfRenderer(); var pdf = await renderer.RenderHtmlAsPdfAsync("<h1>Quarterly Report</h1><p>Generated server-side.</p>"); // Return PDF bytes with file download headers return Results.File(pdf.BinaryData, "application/pdf", "report.pdf"); }); // Program.cs (ASP.NET Core host project) using IronPdf; IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY-HERE"; app.MapGet("/api/pdf/report", async () => { var renderer = new ChromePdfRenderer(); var pdf = await renderer.RenderHtmlAsPdfAsync("<h1>Quarterly Report</h1><p>Generated server-side.</p>"); // Return PDF bytes with file download headers return Results.File(pdf.BinaryData, "application/pdf", "report.pdf"); }); $vbLabelText $csharpLabel 在WASM客户端,注入HttpClient并调用API端点。 Blazor WASM托管项目模板预先配置了HttpClient以目标服务器的基本地址: @page "/wasm-pdf-viewer" @inject HttpClient Http <h3>PDF Viewer</h3> <button @onclick="LoadPdf" class="btn btn-primary">Load Report</button> @if (!string.IsNullOrEmpty(pdfDataUri)) { <iframe src="@pdfDataUri" style="width:100%; height:600px;"></iframe> } @code { private string pdfDataUri = string.Empty; private async Task LoadPdf() { // Fetch PDF bytes from the server-side generation endpoint var bytes = await Http.GetByteArrayAsync("/api/pdf/report"); pdfDataUri = $"data:application/pdf;base64,{Convert.ToBase64String(bytes)}"; } } 这种模式将所有繁重的渲染工作保持在服务器上,而WASM客户端仅处理显示。 对于生产使用,向API端点添加身份验证并将生成的PDF内容范围限制为经过身份验证的用户数据。 我还能执行哪些其他 PDF 操作? IronPDF的API扩展到基本查看之外。 接下来的几个部分涵盖Blazor文档工作流中常见的四个操作:合并多个文档、添加注释、应用密码保护和显示用户上传的文件。 如何合并多个PDF文档? 合并将多个PdfDocument实例组合成一个文件,对于汇编报告节、附加附件或连接用户选择的文件很有用。 合并和拆分PDF指南涵盖页面级插入和拆分操作。 using IronPdf; var renderer = new ChromePdfRenderer(); // Generate two separate sections as individual PDF documents var section1 = await renderer.RenderHtmlAsPdfAsync("<h1>Section 1: Overview</h1>"); var section2 = await renderer.RenderHtmlAsPdfAsync("<h1>Section 2: Details</h1>"); // Merge into a single document preserving all pages var merged = PdfDocument.Merge(section1, section2); merged.SaveAs("combined-report.pdf"); using IronPdf; var renderer = new ChromePdfRenderer(); // Generate two separate sections as individual PDF documents var section1 = await renderer.RenderHtmlAsPdfAsync("<h1>Section 1: Overview</h1>"); var section2 = await renderer.RenderHtmlAsPdfAsync("<h1>Section 2: Details</h1>"); // Merge into a single document preserving all pages var merged = PdfDocument.Merge(section1, section2); merged.SaveAs("combined-report.pdf"); $vbLabelText $csharpLabel 要在Blazor组件中显示合并的文档,请将merged.BinaryData传递给之前部分的base64数据URI模式。 合并的PdfDocument对象在编码显示之前也接受进一步的操作(水印、安全设置或其他页面附加)。 如何向PDF添加注释? 注释将审核员备注和评论附加到特定页面位置,而不更改基础文档内容。 IronPDF支持文本注释、自由文本框和其他标记类型。有关注释属性的完整列表,请参见注释指南。 using IronPdf; using IronPdf.Annotations; var renderer = new ChromePdfRenderer(); var pdf = await renderer.RenderHtmlAsPdfAsync("<h1>Contract Document</h1><p>Review required on clause 3.</p>"); // Add a text annotation to page 0 at position (50, 650) var annotation = new TextAnnotation(pageIndex: 0) { Title = "Reviewer Note", Contents = "Please confirm clause 3 before signing.", X = 50, Y = 650, Width = 200, Height = 50, Printable = false, OpenByDefault = true }; pdf.Annotations.Add(annotation); pdf.SaveAs("annotated-contract.pdf"); using IronPdf; using IronPdf.Annotations; var renderer = new ChromePdfRenderer(); var pdf = await renderer.RenderHtmlAsPdfAsync("<h1>Contract Document</h1><p>Review required on clause 3.</p>"); // Add a text annotation to page 0 at position (50, 650) var annotation = new TextAnnotation(pageIndex: 0) { Title = "Reviewer Note", Contents = "Please confirm clause 3 before signing.", X = 50, Y = 650, Width = 200, Height = 50, Printable = false, OpenByDefault = true }; pdf.Annotations.Add(annotation); pdf.SaveAs("annotated-contract.pdf"); $vbLabelText $csharpLabel 当PDF在任何标准查看器中打开,包括浏览器<iframe>显示时,注释都将保留。 对于Blazor应用程序,在服务器端运行注释逻辑并返回pdf.BinaryData给组件显示。 如何对PDF应用密码保护? 密码保护限制对财务报告或人力资源记录等敏感文档的访问。 IronPDF支持用户密码(打开文档时所需)和拥有者密码(修改权限时所需)。 PDF安全指南列出了所有可用的权限标志。 using IronPdf; var renderer = new ChromePdfRenderer(); var pdf = await renderer.RenderHtmlAsPdfAsync("<h1>Confidential Report</h1>"); // Set the password required to open the document pdf.Password = "user-open-password"; // Set the owner password to control editing and printing rights pdf.SecuritySettings.OwnerPassword = "owner-edit-password"; pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights; pdf.SecuritySettings.AllowUserCopyPasteContent = false; pdf.SaveAs("protected-report.pdf"); using IronPdf; var renderer = new ChromePdfRenderer(); var pdf = await renderer.RenderHtmlAsPdfAsync("<h1>Confidential Report</h1>"); // Set the password required to open the document pdf.Password = "user-open-password"; // Set the owner password to control editing and printing rights pdf.SecuritySettings.OwnerPassword = "owner-edit-password"; pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights; pdf.SecuritySettings.AllowUserCopyPasteContent = false; pdf.SaveAs("protected-report.pdf"); $vbLabelText $csharpLabel 密码保护的PDF在浏览器<iframe>中显示密码提示。 此方法适用于通过下载分发的文档; 对于无提示的内联显示,仅对通过下载路由返回的文档应用密码。 如何显示用户上传的PDF? 显示用户上传的PDF需要读取传入的文件字节并将其编码为数据URI。 以下上传组件使用Blazor的InputFile控件捕获文件,然后直接显示它而不进行重新渲染: @page "/upload-viewer" @using IronPdf <h3>Upload and View a PDF</h3> <InputFile OnChange="LoadUploadedPdf" accept=".pdf" /> @if (!string.IsNullOrEmpty(pdfDataUri)) { <iframe src="@pdfDataUri" style="width:100%; height:600px; margin-top:20px;"></iframe> } @code { private string pdfDataUri = string.Empty; private async Task LoadUploadedPdf(InputFileChangeEventArgs e) { using var stream = e.File.OpenReadStream(maxAllowedSize: 10 * 1024 * 1024); using var ms = new MemoryStream(); await stream.CopyToAsync(ms); var bytes = ms.ToArray(); // Encode the uploaded PDF bytes directly for display pdfDataUri = $"data:application/pdf;base64,{Convert.ToBase64String(bytes)}"; } } 对于需要在显示前进行服务器端处理的上传PDF,例如加水印、页面提取或重新加密,首先将字节加载到PdfDocument中: var pdf = new PdfDocument(bytes); // Apply operations, then re-encode pdfDataUri = $"data:application/pdf;base64,{Convert.ToBase64String(pdf.BinaryData)}"; var pdf = new PdfDocument(bytes); // Apply operations, then re-encode pdfDataUri = $"data:application/pdf;base64,{Convert.ToBase64String(pdf.BinaryData)}"; $vbLabelText $csharpLabel 这保持了相同的组件结构,同时在上传的文件上启用了完整的IronPDF API。 我的下一步是什么? 本指南涵盖了使用IronPDF的BlazorPDF查看器的完整工作流程:在.NET 10上安装、URL和HTML渲染、输出定制带有页眉和页脚、用于浏览器下载的JavaScript互操作、Blazor Server和Blazor WebAssembly之间的架构差异,以及四个文档操作:合并、注释、密码保护和用户上传。 要扩展此基础,请探索这些资源: HTML到PDF教程:高级渲染、CSS媒体查询和JavaScript执行策略 PDF权限和密码:完整的安全设置和权限标志 合并和拆分PDF:页面级文档汇编和拆分 水印指南:为生成的文档添加文本和图像水印 IronPDF API参考:完整的类和方法文档 获取您的免费试用许可证以去除水印并在您的Blazor应用程序中测试IronPDF。 IronPDF支持.NET 10、ASP.NET Core、Blazor Server和托管Blazor WebAssembly项目,无需额外配置。 有关其他集成指导,请参见微软官方的Blazor文档。 常见问题解答 什么是 Blazor PDF 查看器? Blazor PDF查看器是一个组件,用于在Blazor Server或WebAssembly应用程序中内嵌显示PDF文档。通常将PDF字节转换为base64数据URI,并将在iFrame元素中呈现,提供用户一个内置的浏览器工具栏用于缩放、导航和打印。 如何在Blazor Server应用程序中显示PDF? 通过NuGet安装IronPDF,将您的许可证密钥添加到Program.cs,然后使用ChromePdfRenderer从URL或HTML字符串生成PDF字节。将字节编码为base64数据URI,并将其分配给Razor组件中iframe的src属性。 IronPDF可以在Blazor WebAssembly项目中运行吗? IronPDF的渲染引擎需要原生二进制文件,无法在浏览器的WASM沙箱中运行。对于Blazor WebAssembly项目,创建一个服务器端ASP.NET Core API端点,用IronPDF生成PDF并返回字节。WASM客户端通过HttpClient调用该端点并显示结果。 如何在Blazor中触发PDF下载? 将IJSRuntime注入到组件中,使用IronPDF生成PDF字节,将其包装在DotNetStreamReference中,并使用InvokeVoidAsync调用JavaScript函数。JavaScript函数创建Blob URL并点击锚元素以触发浏览器下载。 使用IronPDF进行Blazor PDF查看有哪些优势? IronPDF使用Chrome渲染引擎,精确地将HTML、CSS和JavaScript输出转换为PDF格式。它支持.NET 10,在Blazor Server和WebAssembly架构中工作,提供单一API用于PDF生成、合并、注释、密码保护和用户上传处理。 如何在Blazor中向生成的PDF添加页眉和页脚? 在调用渲染方法之前,设置ChromePdfRenderer的RenderingOptions属性。使用TextHeader和TextFooter用于纯文本页眉和页脚,带有模板变量如{page}、{total-pages}和{date}。对于基于HTML的布局,使用HtmlHeader和HtmlFooter。 如何在Blazor中合并多个PDF文档? 使用ChromePdfRenderer将每个文档生成为PdfDocument实例,然后调用PdfDocument.Merge(pdf1, pdf2)来合并它们。将合并文档的BinaryData传递到Blazor组件的base64数据URI中以显示合并结果。 我可以在Blazor中显示用户上传的PDF而不保存到磁盘吗? 是的。使用Blazor的InputFile组件将上传的文件读入MemoryStream,将字节转换为base64数据URI,并将其分配给iFrame的src属性。无需文件系统写入。对于服务器端处理,在编码之前将字节加载到PdfDocument实例中。 如何对在Blazor中生成的PDF应用密码保护? 生成PdfDocument后,设置Password属性用于用户打开密码,并使用SecuritySettings.OwnerPassword设置所有者密码。在保存或编码文档之前,使用SecuritySettings.AllowUserPrinting和AllowUserCopyPasteContent来控制权限。 IronPDF 是否兼容 .NET 10 的 Blazor PDF 查看器项目? 是的。IronPDF支持.NET 10、.NET 9、.NET 8、.NET 6和.NET Framework 4.6.2及以上版本。在面向.NET 10的Blazor应用程序中使用IronPDF无需特殊配置。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 相关文章 已更新2026年3月1日 如何在.NET中使用IronPDF创建PDF文件(C#教程) 发现为开发人员创建PDF文件的有效方法。提升您的编码技能并简化您的项目。立即阅读文章! 阅读更多 已更新2026年2月27日 如何在C#中合并PDF文件 使用IronPDF合并PDF VB NET。学习使用简单的VB.NET代码将多个PDF文件合并为一个文档。包括逐步示例。 阅读更多 已更新2026年3月1日 面向 .NET 10 开发人员的 C# PDFWriter 教程 使用这份逐步指南了解如何高效地使用C# PDFWriter创建PDF。阅读文章提升您的技能! 阅读更多 如何从ASP.NET Core中的PDF文件读取数据ASP.NET的PDF查看器:如何在....