使用IRONPDF 如何在 ASP.NET Core 檢視器中顯示、保存和打印 PDF Curtis Chau 發表日期:11月 10, 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 How to Display, Save, and Print PDFs in an ASP.NET Core Viewer Displaying PDF documents directly in web browsers has become an essential requirement for modern ASP.NET Core web applications. Whether you're generating invoices, reports, or contracts, users expect seamless PDF viewing without needing to download PDF files or install Adobe Acrobat Reader or other plugins. This tutorial demonstrates how IronPDF simplifies PDF file display, saving, and printing in your ASP.NET Core PDF viewer through its powerful Chrome-based rendering engine. How Do Browsers Handle PDF Viewing in ASP.NET Core? Modern browsers include built-in PDF viewers that activate when they receive a PDF file with the correct MIME type (application/pdf). When your ASP.NET Core web application returns a PDF document with appropriate headers, the browser automatically displays it inline. This eliminates the need for external plugins, Adobe Acrobat Reader, or complex JavaScript libraries. According to MDN Web Docs, proper header configuration is essential for controlling how browsers handle file downloads and displays. IronPDF leverages this browser capability by generating high-quality PDF files server-side using its ChromePdfRenderer class. The renderer uses a complete Chrome engine under the hood, ensuring your PDF documents display exactly as intended with full support for modern CSS, JavaScript, digital signatures, and web fonts. Unlike simple document viewers, IronPDF provides complete control over PDF processing and rendering. What Tools Do You Need to Display/View PDF Files in ASP.NET Core? Setting up IronPDF in your ASP.NET Core PDF viewer project takes just a few steps. First, create a new project in Visual Studio or via the command line. Open Visual Studio and select the ASP.NET Core Web Application project template: dotnet new mvc -n PdfViewerApp cd PdfViewerApp dotnet new mvc -n PdfViewerApp cd PdfViewerApp SHELL Installing the IronPDF .NET Package Install IronPDF via NuGet Package Manager in your .NET project: Install-Package IronPdf Or in Solution Explorer, right-click on your project and select "Manage NuGet Packages." Choose the appropriate package source and search for IronPDF. That's all the setup required. IronPDF works seamlessly with ASP.NET Core 3.1 and later (long-term support versions), including .NET 6, 7, and 8. The library is frequently updated to ensure compatibility with the latest frameworks. For detailed installation instructions and additional information, visit the IronPDF installation guide. The package includes all necessary components for PDF generation, editing, and processing. How Can You Display PDF Files in the Browser Using ASP.NET Core? Creating and displaying PDF files in the browser requires just a few lines of code. Here's a complete controller action that generates a PDF document from HTML and displays it inline. This code snippet demonstrates the core functionality: using IronPdf; using Microsoft.AspNetCore.Mvc; public class PdfController : Controller { public IActionResult ViewPdf() { var renderer = new ChromePdfRenderer(); // Configure rendering options for the PDF viewer renderer.RenderingOptions.PrintHtmlBackgrounds = true; renderer.RenderingOptions.CreatePdfFormsFromHtml = true; renderer.RenderingOptions.EnableJavaScript = true; // Generate PDF from HTML string var html = @" <html> <head> <style> body { font-family: Arial, sans-serif; padding: 20px; } h1 { color: #2c3e50; } .content { line-height: 1.6; width: 100%; } </style> </head> <body> <h1>Invoice #12345</h1> <div class='content'> <p>Date: " + DateTime.Now.ToString("yyyy-MM-dd") + @"</p> <p>Thank you for your business!</p> </div> </body> </html>"; var pdf = renderer.RenderHtmlAsPdf(html); // Return PDF for inline viewing in the browser return File(pdf.BinaryData, "application/pdf"); } } using IronPdf; using Microsoft.AspNetCore.Mvc; public class PdfController : Controller { public IActionResult ViewPdf() { var renderer = new ChromePdfRenderer(); // Configure rendering options for the PDF viewer renderer.RenderingOptions.PrintHtmlBackgrounds = true; renderer.RenderingOptions.CreatePdfFormsFromHtml = true; renderer.RenderingOptions.EnableJavaScript = true; // Generate PDF from HTML string var html = @" <html> <head> <style> body { font-family: Arial, sans-serif; padding: 20px; } h1 { color: #2c3e50; } .content { line-height: 1.6; width: 100%; } </style> </head> <body> <h1>Invoice #12345</h1> <div class='content'> <p>Date: " + DateTime.Now.ToString("yyyy-MM-dd") + @"</p> <p>Thank you for your business!</p> </div> </body> </html>"; var pdf = renderer.RenderHtmlAsPdf(html); // Return PDF for inline viewing in the browser return File(pdf.BinaryData, "application/pdf"); } } IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel The above code creates a ChromePdfRenderer instance and configures it to include background colors and convert HTML forms to PDF forms. The RenderHtmlAsPdf method transforms the HTML string into a PDF file. The following code shows how returning the PDF with the application/pdf MIME type tells the browser to display it inline rather than downloading it. This server-side approach ensures consistent rendering across all browsers and platforms. Output For existing HTML files or Razor pages, you can use alternative rendering methods to view PDF content: // Render from URL - useful for complex pages var pdf = renderer.RenderUrlAsPdf("https://example.com/invoice"); // Render from HTML file in the same location var pdf = renderer.RenderHtmlFileAsPdf("Views/Invoice.html"); // Render from wwwroot folder var pdf = renderer.RenderHtmlFileAsPdf("wwwroot/templates/report.html"); // Render from URL - useful for complex pages var pdf = renderer.RenderUrlAsPdf("https://example.com/invoice"); // Render from HTML file in the same location var pdf = renderer.RenderHtmlFileAsPdf("Views/Invoice.html"); // Render from wwwroot folder var pdf = renderer.RenderHtmlFileAsPdf("wwwroot/templates/report.html"); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel These methods provide flexibility in how you source your HTML content while maintaining the same high-quality PDF rendering output. You can also load existing PDF documents, edit PDF files, and even work with Word (DOCX files) and Excel formats using IronPDF's comprehensive features. Learn more about HTML to PDF conversion options in the documentation. For advanced processing and editing capabilities, refer to the API reference. How Do Users Save PDF Documents from the Browser? To enable users to download PDF files instead of viewing them inline, modify the Content-Disposition header. This feature is essential for web applications where users need to save documents for offline access: public IActionResult DownloadPdf() { var renderer = new ChromePdfRenderer(); // Create PDF with CSS styling and images var html = @"<h1>Download Me</h1> <img src='logo.png' width='100' />"; var pdf = renderer.RenderHtmlAsPdf(html, @"wwwroot/images"); // Force download with custom filename return File(pdf.BinaryData, "application/pdf", "invoice-2024.pdf"); } public IActionResult DownloadPdf() { var renderer = new ChromePdfRenderer(); // Create PDF with CSS styling and images var html = @"<h1>Download Me</h1> <img src='logo.png' width='100' />"; var pdf = renderer.RenderHtmlAsPdf(html, @"wwwroot/images"); // Force download with custom filename return File(pdf.BinaryData, "application/pdf", "invoice-2024.pdf"); } IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel Adding the filename parameter automatically sets the Content-Disposition header to "attachment," prompting the browser to download the file. Users can also save PDF files displayed inline using their browser's built-in save functionality, accessible through Ctrl+S or the browser's PDF viewer toolbar. The default behavior allows users to choose their preferred folder location. Output For better memory efficiency when handling large PDF documents, use streams: public IActionResult StreamPdf() { var renderer = new ChromePdfRenderer(); // Load and process HTML with images var html = "<h1>Streamed Content</h1>"; var pdf = renderer.RenderHtmlAsPdf(html); // Stream the PDF file to the browser var stream = pdf.Stream; stream.Position = 0; return File(stream, "application/pdf", "document.pdf"); } public IActionResult StreamPdf() { var renderer = new ChromePdfRenderer(); // Load and process HTML with images var html = "<h1>Streamed Content</h1>"; var pdf = renderer.RenderHtmlAsPdf(html); // Stream the PDF file to the browser var stream = pdf.Stream; stream.Position = 0; return File(stream, "application/pdf", "document.pdf"); } IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel This approach reduces memory consumption by streaming the PDF file directly to the response without creating intermediate byte arrays. The above code demonstrates efficient file handling for large documents. You can also load existing PDF files from the wwwroot folder or other locations, edit them, and stream the modified versions. For more advanced PDF document manipulation and image processing, explore the PdfDocument API reference. The component supports various editing operations including text selection, form filling, and adding digital signatures. Can Users Print PDF Documents Directly from ASP.NET Core Web Applications? IronPDF optimizes PDF files for printing by configuring the appropriate CSS media type and page settings. This feature ensures professional output whether users print to physical printers or save as PDF: public IActionResult PrintablePdf() { var renderer = new ChromePdfRenderer(); // Configure printing options for the PDF viewer renderer.RenderingOptions.CssMediaType = PdfCssMediaType.Print; renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Portrait; renderer.RenderingOptions.MarginTop = 25; renderer.RenderingOptions.MarginBottom = 25; renderer.RenderingOptions.MarginLeft = 25; renderer.RenderingOptions.MarginRight = 25; // Load HTML with print-specific CSS var html = @"<h1>Print-Optimized Document</h1> <p>This document is optimized for printing.</p>"; var pdf = renderer.RenderHtmlAsPdf(html); // Return the PDF file for viewing and printing return File(pdf.BinaryData, "application/pdf"); } public IActionResult PrintablePdf() { var renderer = new ChromePdfRenderer(); // Configure printing options for the PDF viewer renderer.RenderingOptions.CssMediaType = PdfCssMediaType.Print; renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Portrait; renderer.RenderingOptions.MarginTop = 25; renderer.RenderingOptions.MarginBottom = 25; renderer.RenderingOptions.MarginLeft = 25; renderer.RenderingOptions.MarginRight = 25; // Load HTML with print-specific CSS var html = @"<h1>Print-Optimized Document</h1> <p>This document is optimized for printing.</p>"; var pdf = renderer.RenderHtmlAsPdf(html); // Return the PDF file for viewing and printing return File(pdf.BinaryData, "application/pdf"); } IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel Setting CssMediaType to Print applies print-specific CSS styles, ensuring the PDF document looks correct when printed. The margin settings provide proper spacing for physical paper. The following code shows how users can print PDF files directly from their browser's PDF viewer using the standard print dialog, maintaining full control over printer selection and settings. Learn more about PDF rendering options to fine-tune your output. The core PDF viewer component handles all printing details automatically. Cross-Platform and Container Support IronPDF runs seamlessly across Windows, Linux, macOS, Docker containers, and cloud platforms like Azure and AWS. This cross-platform compatibility ensures your ASP.NET Core PDF viewer solution works consistently regardless of deployment environment. The library handles platform-specific rendering details internally, so the same code works everywhere without modification. The server-side processing ensures consistent PDF generation across all platforms. Whether deployed on Windows servers or Linux containers, the component maintains the same rendering quality. The library automatically manages path differences between operating systems, so files in the wwwroot folder or other locations are handled correctly. For containerized deployments, check the Docker deployment guide. The package includes all necessary dependencies for each platform, and setup requires no additional configuration beyond standard ASP.NET Core requirements. Get started with a free trial and transform your document viewer capabilities today. Summary IronPDF transforms PDF handling in ASP.NET Core web applications by combining server-side generation with browser-native viewing. With just a few lines of code, you can create professional PDF documents from HTML, display PDF files inline, enable downloads, and optimize for printing. The Chrome-based rendering engine ensures pixel-perfect accuracy across all platforms, eliminating the need for Adobe Acrobat Reader or other third-party viewers. This ASP.NET Core PDF viewer solution provides comprehensive features including form filling, text selection, digital signatures, and the ability to edit PDF files. The component also supports converting Word documents (DOCX files), Excel spreadsheets, and images to PDF. Whether you're building a simple document viewer or a complex document management system, IronPDF provides the tools and support you need. The library's tag helper integration and extensive documentation make implementation straightforward. Your project's home page can display PDF files directly, while backend processing handles complex PDF generation tasks. The viewer maintains consistent behavior whether loaded from the wwwroot folder, generated dynamically, or retrieved from external sources. With built-in theme support and customizable width settings, you can match your application's design perfectly. Ready to implement PDF viewing in your .NET Core Web Application? For production use, licenses start at $749 and include comprehensive support and updates. Visit the documentation for detailed API references and advanced features. Refer to our extensive code examples to quickly implement PDF functionality in your ASP.NET Core projects. 常見問題解答 如何在 ASP.NET Core 應用程式中顯示 PDF 檔案? 您可以使用 IronPDF 在 ASP.NET Core 應用程式中顯示 PDF,它提供了直接在您的應用程式中呈現 PDF 檔案的功能。 在 ASP.NET Core 中使用 IronPDF 保存 PDF 檔案的步驟是什麼? 若要在 ASP.NET Core 中儲存 PDF,可以使用 IronPDF 的內建方法將文件轉換為 PDF 格式,然後將其寫入檔案系統或流。 是否可以在 ASP.NET Core 應用程式中列印 PDF 檔案? 是的,IronPDF 允許您使用其渲染和列印功能,直接從 ASP.NET Core 應用程式列印 PDF 檔案。 在 ASP.NET Core 中使用 IronPDF 有哪些優點? IronPDF 與 ASP.NET Core 無縫集成,只需極少的設定即可輕鬆操作 PDF,例如檢視、儲存和列印。 IronPDF 能否在 ASP.NET Core 中處理大型 PDF 文件? 是的,IronPDF 經過最佳化,能夠高效處理大型 PDF 文檔,即使是複雜的文件也能確保流暢的效能。 IronPDF 是否支援 ASP.NET Core 中的 PDF 註解和評論? IronPDF 支援在 PDF 中新增和讀取註釋和評論,從而增強 ASP.NET Core 應用程式中 PDF 文件的互動性和可用性。 如何將 IronPDF 整合到現有的 ASP.NET Core 專案中? 您可以透過安裝 IronPDF NuGet 套件並使用其 API 來管理 PDF,從而將 IronPDF 整合到您的 ASP.NET Core 專案中。 使用 IronPDF 和 ASP.NET Core 有什麼具體的系統需求嗎? IronPDF 需要 .NET Core 或 .NET 5+,並支援所有主流作業系統,使其成為 ASP.NET Core 專案的靈活選擇。 IronPDF能否在ASP.NET Core中將其他文件格式轉換為PDF? 是的,IronPDF 可以在 ASP.NET Core 環境中將各種文件格式(例如 HTML、圖像和文件)轉換為 PDF。 IronPDF 為 ASP.NET Core 開發人員提供哪些類型的支援? IronPDF 提供全面的文件、程式碼範例和快速回應的支持,以協助 ASP.NET Core 開發人員實現 PDF 功能。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 發表日期 11月 13, 2025 如何在 C# 中合併兩個 PDF 位元組數組 使用 IronPDF 在 C# 中合併兩個 PDF 位元組數組。學習如何透過簡單的程式碼範例,將來自位元組數組、記憶體流和資料庫的多個 PDF 文件合併在一起。 閱讀更多 發表日期 11月 13, 2025 如何在 ASP.NET MVC 中創建 PDF 檢視器 為 ASP.NET MVC 應用程式構建一個強大的 PDF 檢視器。顯示 PDF 文件,將視圖轉換為 PDF,使用 IronPDF 添加互動功能。 閱讀更多 發表日期 11月 13, 2025 如何建立 .NET HTML 轉 PDF 轉換器 學習如何在.NET中使用IronPDF將HTML轉換為PDF。 閱讀更多 如何在 ASP.NET MVC 中創建 PDF 檢視器如何在 Blazor 中在新標籤中...
發表日期 11月 13, 2025 如何在 C# 中合併兩個 PDF 位元組數組 使用 IronPDF 在 C# 中合併兩個 PDF 位元組數組。學習如何透過簡單的程式碼範例,將來自位元組數組、記憶體流和資料庫的多個 PDF 文件合併在一起。 閱讀更多
發表日期 11月 13, 2025 如何在 ASP.NET MVC 中創建 PDF 檢視器 為 ASP.NET MVC 應用程式構建一個強大的 PDF 檢視器。顯示 PDF 文件,將視圖轉換為 PDF,使用 IronPDF 添加互動功能。 閱讀更多