How to Create a PDF Viewer in ASP.NET Core: Figure 1
To implement a PDF viewer in ASP.NET Core, use IronPDF to generate PDFs from HTML, URLs, or Razor views. Serve them with proper headers for inline browser display, providing a smooth document viewing experience without external plugins or downloads.
Building a web application in .NET often requires displaying PDF documents directly in the browser. Whether it's invoices, reports, or interactive PDF forms, users expect a smooth document viewer experience without needing Adobe Acrobat Reader or other third-party browser tools.
In this tutorial, you'll learn how to implement a PDF viewer for ASP.NET and .NET Core using IronPDF. This reliable 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 built-in PDF viewers. When you serve a PDF file with the correct MIME type (application/pdf), the browser automatically displays 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 users a viewing experience similar to a desktop PDF viewer.
Why Should You Set Up an ASP.NET Core Project First?
Start by creating a new ASP.NET Core MVC application:
dotnet new mvc -n PdfViewerApp
cd PdfViewerAppThis scaffolds a basic .NET application with MVC support. You can then install the IronPDF NuGet packages required for PDF processing and rendering.
How Do You Install and Configure IronPDF?
Installing IronPDF takes just a few steps. Open the Package Manager Console in Visual Studio and run:
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 keyOnce installed, set up your license key in Program.cs. IronPDF's documentation provides additional information and examples for configuration. For deployment, ensure you're using the correct installation method for your target environment.
How Can You 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.
How Do You Create 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");
}
}This 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.
When Should You Generate PDFs from URLs?
Convert existing web pages into PDFs:
public IActionResult GenerateFromUrl(string url = "___PROTECTED_URL_133___")
{
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 = "___PROTECTED_URL_133___")
{
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");
}Here, 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. The JavaScript rendering options ensure dynamic content loads properly. This is ideal for archiving or sharing web content as a PDF document.
Output

How Can You Convert 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();
}
}This 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. For MVC Framework projects, similar approaches work with minor adjustments.
How Do You Display PDFs in the Browser?
The key to displaying PDFs inline (rather than downloading) lies in setting the correct response headers. Modern browsers support inline PDF viewing when configured properly.
What's the Basic Approach for 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");
}This 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. You can improve this with headers and footers or watermarks.
Output

How Can You Enable 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}`;
}This 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. Consider implementing async methods for better performance with large documents.
How Do You Implement Advanced Viewing Features?
Improve your PDF viewer with additional functionality:
public class AdvancedPdfController : Controller
{
// Add zoom controls
public IActionResult ViewWithZoom()
{
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.ViewPortWidth = 1024;
renderer.RenderingOptions.Zoom = 150; // 150% zoom
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
return File(pdf.BinaryData, "application/pdf");
}
// Enable text search
public IActionResult SearchablePdf()
{
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.CreatePdfFormsFromHtml = true;
var pdf = renderer.RenderHtmlAsPdf(htmlWithForms);
// PDF text is searchable by default
return File(pdf.BinaryData, "application/pdf");
}
}public class AdvancedPdfController : Controller
{
// Add zoom controls
public IActionResult ViewWithZoom()
{
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.ViewPortWidth = 1024;
renderer.RenderingOptions.Zoom = 150; // 150% zoom
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
return File(pdf.BinaryData, "application/pdf");
}
// Enable text search
public IActionResult SearchablePdf()
{
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.CreatePdfFormsFromHtml = true;
var pdf = renderer.RenderHtmlAsPdf(htmlWithForms);
// PDF text is searchable by default
return File(pdf.BinaryData, "application/pdf");
}
}These examples demonstrate viewport control and form creation. The rendering options provide fine-grained control over PDF generation. Enable text extraction for search functionality and form fields for interactive 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's a frequently updated library with active support, you can rely on it for professional document processing.
How Do You Deploy to Azure?
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. Consider using Azure Functions for serverless PDF generation.
What About 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 libgdiplusFor Linux deployments, additional dependencies may be required. Check the installation guide for platform-specific requirements. Consider using IronPDF Slim for reduced container sizes.
What Are Key Performance Tips?
- Cache generated PDFs when content doesn't change frequently using memory streams
- Use async methods for better scalability
- Set appropriate timeouts for complex PDF generation
- Implement compression to reduce file sizes
- Consider linearization for faster web viewing
- Use parallel processing for batch operations
How Do You Troubleshoot Common Issues?
Why Isn't Your PDF 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");Check browser compatibility and ensure the MIME type is correct. Some older browsers may require additional configuration.
How Do You Handle Cross-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", "*");For secure environments, configure CORS policies appropriately. Consider authentication requirements for protected documents.
What About Large 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");
}Implement pagination for very large documents. Use compression to reduce bandwidth. Consider async rendering for improved performance.
What Are the Next Steps?
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. Check our API reference for detailed documentation and code examples.
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.
Does IronPDF support .NET 10 for PDF generation and viewing?
Yes. IronPDF fully supports .NET 10, along with earlier versions like .NET 9, 8, 7, 6, and .NET Core and Framework. This means you can use IronPDF in modern .NET 10 ASP.NET or Blazor applications to generate, serve, and display PDFs inline in the browser. (ironpdf.com)









