How to Create a PDF Viewer in ASP.NET Core
Building a web application in .NET often requires displaying PDF documents directly in the browser. Whether it’s invoices, reports, or interactive PDF forms, end users expect a seamless document viewer experience without needing Adobe Acrobat Reader or other third-party browser tools.
In this tutorial, we’ll show you how to implement a PDF viewer for ASP.NET and .NET Core using IronPDF. This robust PDF viewer control allows developers to create, render, and display PDF files within their .NET applications, providing a professional solution for handling business-critical documents.
How Does PDF Viewing Work in ASP.NET Core?
Modern browsers act as a built-in PDF viewer. When you serve a PDF file with the correct MIME type (application/pdf), the browser will automatically display it inline. This means you don’t need external plugins to view PDF documents or display PDF files. The key lies in generating high-quality PDFs and configuring the correct response headers.
IronPDF is a frequently updated .NET PDF library that excels at creating pixel-perfect PDF pages from HTML, Razor views, or existing documents. Its Chrome-based rendering ensures accurate CSS, JavaScript, and image support, giving your end users a viewing experience similar to a desktop PDF viewer.
Setting Up Your ASP.NET Core Project
Start by creating a new ASP.NET Core MVC application:
dotnet new mvc -n PdfViewerApp
cd PdfViewerAppThis will scaffold a basic .NET application with MVC support. You can then install the IronPDF NuGet packages required for PDF processing and rendering.
How to Install and Configure IronPDF?
Installing IronPDF takes just a few steps. Open the Package Manager Console in Visual Studio and run the following:
Install-Package IronPdf
Or using the .NET CLI:
dotnet add package IronPdfConfigure IronPDF in your Program.cs or Startup.cs:
// Add this at application startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"; // Use your trial or commercial key// Add this at application startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"; // Use your trial or commercial keyIRON VB CONVERTER ERROR developers@ironsoftware.comOnce installed, set up your license key in Program.cs. IronPDF’s documentation provides additional information and examples for configuration.
How to Generate PDFs for Viewing?
IronPDF lets you create PDF files from raw HTML, web pages, or Razor views. The generated PDF document can then be displayed inline with just a few lines of C# source code. For example, using a ChromePdfRenderer, you can render HTML and return it to the browser as a PDF file that displays within your ASP.NET PDF viewer control.
This approach avoids forcing a download, giving users the ability to view, print, search, and save PDFs directly inside your web form or Blazor project.
Creating PDFs from HTML Strings
The simplest approach converts HTML directly to PDF:
using IronPdf;
public class PdfController : Controller
{
public IActionResult GenerateFromHtml()
{
var renderer = new ChromePdfRenderer();
// Create PDF from HTML
var html = @"
<html>
<head>
<style>
body { font-family: Arial; padding: 20px; }
h1 { color: #333; }
</style>
</head>
<body>
<h1>Sample PDF Document</h1>
<p>This PDF was generated using IronPDF in ASP.NET Core.</p>
</body>
</html>";
var pdf = renderer.RenderHtmlAsPdf(html);
// Return PDF to browser for viewing
return File(pdf.BinaryData, "application/pdf");
}
}using IronPdf;
public class PdfController : Controller
{
public IActionResult GenerateFromHtml()
{
var renderer = new ChromePdfRenderer();
// Create PDF from HTML
var html = @"
<html>
<head>
<style>
body { font-family: Arial; padding: 20px; }
h1 { color: #333; }
</style>
</head>
<body>
<h1>Sample PDF Document</h1>
<p>This PDF was generated using IronPDF in ASP.NET Core.</p>
</body>
</html>";
var pdf = renderer.RenderHtmlAsPdf(html);
// Return PDF to browser for viewing
return File(pdf.BinaryData, "application/pdf");
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comThis example shows how to generate a PDF directly from an HTML string. A ChromePdfRenderer is created, which uses Chromium for accurate rendering. The HTML (with inline CSS) is passed to RenderHtmlAsPdf, producing a PdfDocument. Returning the file with the application/pdf MIME type ensures the browser displays it inline. This is useful for generating reports or invoices dynamically in ASP.NET Core.
Generating PDFs from URLs
Convert existing web pages into PDFs:
public IActionResult GenerateFromUrl(string url = "https://ironpdf.com")
{
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.WaitFor.JavaScript(3000);
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
var pdf = renderer.RenderUrlAsPdf(url);
Response.Headers.Add("Content-Disposition", "inline; filename=webpage.pdf");
return File(pdf.BinaryData, "application/pdf");
}public IActionResult GenerateFromUrl(string url = "https://ironpdf.com")
{
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.WaitFor.JavaScript(3000);
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
var pdf = renderer.RenderUrlAsPdf(url);
Response.Headers.Add("Content-Disposition", "inline; filename=webpage.pdf");
return File(pdf.BinaryData, "application/pdf");
}IRON VB CONVERTER ERROR developers@ironsoftware.comHere, IronPDF converts a live web page into a PDF. The RenderUrlAsPdf method fetches the page, applies styles and scripts, and outputs a polished PDF. Setting the Content-Disposition header to inline makes the file open in the browser’s PDF viewer. This is ideal for archiving or sharing web content as a PDF document.
Output

Converting Razor Views to PDF
Transform your Razor views into PDFs dynamically:
public async Task<IActionResult> ViewToPdf()
{
// Render the view to HTML string first
var htmlContent = await RenderViewToString("Invoice", invoiceModel);
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
return File(pdf.BinaryData, "application/pdf");
}
private async Task<string> RenderViewToString(string viewName, object model)
{
ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.ToString();
}
}public async Task<IActionResult> ViewToPdf()
{
// Render the view to HTML string first
var htmlContent = await RenderViewToString("Invoice", invoiceModel);
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
return File(pdf.BinaryData, "application/pdf");
}
private async Task<string> RenderViewToString(string viewName, object model)
{
ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.ToString();
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comThis snippet turns Razor views into PDFs. The view is first rendered into an HTML string with RenderViewToString, then converted using RenderHtmlAsPdf. The result is returned as a browser-viewable file. This lets you reuse Razor templates (like invoices) for both web display and PDF generation, ensuring consistency.
How to Display PDFs in the Browser?
The key to displaying PDFs inline (rather than downloading) lies in setting the correct response headers.
Basic Inline Display
public IActionResult ViewPdf()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
Response.Headers.Add("Content-Disposition", "inline");
return File(pdf.BinaryData, "application/pdf");
}public IActionResult ViewPdf()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
Response.Headers.Add("Content-Disposition", "inline");
return File(pdf.BinaryData, "application/pdf");
}IRON VB CONVERTER ERROR developers@ironsoftware.comThis example forces a PDF to display inline instead of downloading. A simple HTML string is converted with RenderHtmlAsPdf, and the Content-Disposition header is set to inline. It’s a quick way to preview generated PDF files directly in the browser.
Output

Dynamic PDF Loading
Load different PDFs without page refresh:
[HttpGet]
public IActionResult GetPdfList()
{
var pdfs = new List<object>
{
new { id = 1, name = "Report 1" },
new { id = 2, name = "Report 2" }
};
return Json(pdfs);
}
[HttpGet]
public IActionResult GetPdf(int id)
{
var renderer = new ChromePdfRenderer();
var html = $"<h1>Report {id}</h1><p>Content for report {id}</p>";
var pdf = renderer.RenderHtmlAsPdf(html);
return File(pdf.BinaryData, "application/pdf");
}
// Load PDF dynamically
function loadPdf(pdfId) {
const frame = document.getElementById('pdfFrame');
frame.src = `/Pdf/GetPdf?id=${pdfId}`;
}[HttpGet]
public IActionResult GetPdfList()
{
var pdfs = new List<object>
{
new { id = 1, name = "Report 1" },
new { id = 2, name = "Report 2" }
};
return Json(pdfs);
}
[HttpGet]
public IActionResult GetPdf(int id)
{
var renderer = new ChromePdfRenderer();
var html = $"<h1>Report {id}</h1><p>Content for report {id}</p>";
var pdf = renderer.RenderHtmlAsPdf(html);
return File(pdf.BinaryData, "application/pdf");
}
// Load PDF dynamically
function loadPdf(pdfId) {
const frame = document.getElementById('pdfFrame');
frame.src = `/Pdf/GetPdf?id=${pdfId}`;
}IRON VB CONVERTER ERROR developers@ironsoftware.comThis code dynamically loads different PDFs into the viewer. The controller provides a list of PDFs and generates them on demand. The JavaScript loadPdf function updates the <iframe> source without reloading the page, enabling quick switching between documents.
What About Deployment Considerations?
IronPDF works across ASP.NET, ASP.NET Core, and Blazor Server projects. You can host on a server, deploy to Azure, or containerize with Docker. Since it is a frequently updated library with active support, you can rely on it for enterprise-grade document processing.
Azure Deployment
When deploying to Azure App Service, IronPDF works seamlessly with minimal configuration. Ensure your App Service plan is at least the B1 tier for optimal performance. IronPDF automatically handles the Chrome rendering engine deployment. Please refer here for our documentation on selecting the proper Azure tier.
Docker Support
For containerized deployments, IronPDF provides Docker support. Add this to your Dockerfile:
# Install IronPDF dependencies
RUN apt-get update && apt-get install -y libgdiplusPerformance Tips
- Cache generated PDFs when content doesn't change frequently
- Use async methods for better scalability
- Set appropriate timeouts for complex PDF generation
Common Issues and Solutions
PDF Not Displaying Inline
If PDFs download instead of displaying:
// Ensure Content-Disposition is set correctly
Response.Headers.Add("Content-Disposition", "inline; filename=document.pdf");// Ensure Content-Disposition is set correctly
Response.Headers.Add("Content-Disposition", "inline; filename=document.pdf");IRON VB CONVERTER ERROR developers@ironsoftware.comCross-Origin Issues
When loading PDFs from different domains:
// Add CORS headers if needed
Response.Headers.Add("Access-Control-Allow-Origin", "*");// Add CORS headers if needed
Response.Headers.Add("Access-Control-Allow-Origin", "*");IRON VB CONVERTER ERROR developers@ironsoftware.comLarge PDF Files
For large documents, consider streaming:
public async Task<IActionResult> StreamPdf()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(largeHtmlContent);
var stream = new MemoryStream(pdf.BinaryData);
return new FileStreamResult(stream, "application/pdf");
}public async Task<IActionResult> StreamPdf()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(largeHtmlContent);
var stream = new MemoryStream(pdf.BinaryData);
return new FileStreamResult(stream, "application/pdf");
}IRON VB CONVERTER ERROR developers@ironsoftware.comConclusion
Creating a PDF viewer in ASP.NET Core doesn’t have to be complex. By combining IronPDF’s rendering engine with the browser’s default PDF viewer, you get a professional, user-friendly solution for displaying, editing, and handling PDF files online. Whether you need to render invoices, integrate dynamic reports, or build a document viewer for mobile apps, this approach offers the right balance of features, scalability, and performance.
Looking for a complete tutorial with working source code? Download our free sample project and explore how IronPDF can help you integrate a modern, secure, and flexible PDF viewer for ASP.NET today.
Ready to implement PDF viewing in your ASP.NET Core application? Start with a free trial of IronPDF and transform your web application's document handling capabilities today.
Frequently Asked Questions
What is the best way to display PDF documents in an ASP.NET application?
Using IronPDF allows developers to easily render PDF documents directly in the browser within an ASP.NET application, providing a seamless user experience without the need for third-party tools like Adobe Acrobat Reader.
Can IronPDF be used to enhance PDF viewing in web applications?
Yes, IronPDF is designed to enhance PDF viewing by allowing smooth integration into web applications, ensuring that users can view PDF documents with ease and efficiency.
Do I need additional tools to view PDFs when using IronPDF in ASP.NET?
No, IronPDF eliminates the need for additional tools like Adobe Acrobat Reader, offering a built-in solution for viewing PDFs directly in ASP.NET applications.
How can I implement a PDF viewer in my .NET web application?
By integrating IronPDF into your .NET web application, you can implement a powerful PDF viewer that supports various PDF functionalities and offers a user-friendly interface.
Why should I use IronPDF for PDF viewing in ASP.NET?
IronPDF provides a reliable and efficient solution for PDF viewing in ASP.NET applications, enabling developers to offer users a seamless experience without relying on external plugins.
Is it possible to display interactive PDF forms using IronPDF?
Yes, IronPDF supports the display of interactive PDF forms, allowing users to fill out and interact with PDF documents directly within the browser.
What types of PDF documents can be viewed using IronPDF?
IronPDF can handle various types of PDF documents, including invoices, reports, and interactive forms, making it a versatile tool for any ASP.NET application.






