Skip to footer content
USING IRONPDF

How to Create a PDF Viewer in ASP .NET Web Application with IronPDF

Creating web applications for displaying PDF documents is straightforward. Whether you're showcasing invoices, reports, or interactive forms, users anticipate a smooth, in-browser document viewing experience—no need for Adobe Acrobat Reader or additional third-party tools.

IronPDF makes this incredibly straightforward. This frequently updated .NET library lets you create, render, and display PDF files within your ASP.NET Core project with just a few lines of code. Let's dive into how you can implement a professional PDF viewer control that handles everything from simple HTML conversion to complex Razor views.

How Does Browser-Based PDF Viewing Work?

Here's the good news: modern browsers already include a built-in PDF viewer. When your server sends a PDF file with the correct MIME type (application/pdf), the browser automatically displays it inline. This means you don't need external plugins or complex client-side JavaScript libraries to view PDF documents.

The key lies in generating high-quality PDF documents and configuring the correct response headers. IronPDF handles the heavy lifting on the server side, using its Chrome-based rendering engine to create pixel-perfect PDF pages from HTML, CSS, and JavaScript. The result? Your users get a native document viewer experience with features such as text selection, search, print, and download, all without any additional UI configuration. You can easily embed and display the content.

This approach works seamlessly across all .NET applications, whether you're building with ASP.NET Core MVC, Razor views, or even legacy web folder projects.

How to Install and Configure the PDF Viewer Control?

Getting started with IronPDF in Visual Studio takes just a few steps. Open your ASP.NET Core project and install the NuGet package:

Install-Package IronPdf

Next, add your license key configuration in Program.cs:

IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
$vbLabelText   $csharpLabel

That's all the setup you need for IronPDF to fully integrate within your .NET applications. Refer to IronPDF's documentation for additional configuration options for advanced scenarios like Azure deployment or Docker containers. You can start with a free trial to explore all the features before committing.

How to Generate and Display PDF Documents from HTML?

The simplest way to create a PDF viewer in your ASP.NET Core web application is by converting HTML strings directly. Here's a complete controller example:

using IronPdf;
using Microsoft.AspNetCore.Mvc;

public class PdfController : Controller
{
    public IActionResult DisplayFromHtml()
    {
        var renderer = new ChromePdfRenderer();
        // Create PDF from HTML with CSS styling
        var html = @"
            <html>
            <head>
                <style>
                    body { font-family: Arial; padding: 20px; }
                    h1 { color: #2c3e50; }
                    .content { line-height: 1.6; }
                </style>
            </head>
            <body>
                <h1>Sample PDF Document</h1>
                <p class='content'>This PDF was generated using IronPDF in ASP.NET Core.</p>
            </body>
            </html>";
        var pdf = renderer.RenderHtmlAsPdf(html);
        // Display PDF inline in browser
        Response.Headers.Add("Content-Disposition", "inline; filename=document.pdf");
        return File(pdf.BinaryData, "application/pdf");
    }
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;

public class PdfController : Controller
{
    public IActionResult DisplayFromHtml()
    {
        var renderer = new ChromePdfRenderer();
        // Create PDF from HTML with CSS styling
        var html = @"
            <html>
            <head>
                <style>
                    body { font-family: Arial; padding: 20px; }
                    h1 { color: #2c3e50; }
                    .content { line-height: 1.6; }
                </style>
            </head>
            <body>
                <h1>Sample PDF Document</h1>
                <p class='content'>This PDF was generated using IronPDF in ASP.NET Core.</p>
            </body>
            </html>";
        var pdf = renderer.RenderHtmlAsPdf(html);
        // Display PDF inline in browser
        Response.Headers.Add("Content-Disposition", "inline; filename=document.pdf");
        return File(pdf.BinaryData, "application/pdf");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output PDF Viewed in Browser

How to Create a PDF Viewer in ASP .NET Web Application with IronPDF: Image 1 - Image 1 of 3 related to How to Create a PDF Viewer in ASP .NET Web Application with IronPDF

The ChromePdfRenderer class uses Chromium for accurate rendering, ensuring your CSS, JavaScript, and images display exactly as intended. Setting Content-Disposition to inline tells the browser to display the PDF rather than download it.

This creates a seamless viewing experience allowing users to access the document directly within your web UI. For documents with complex layouts, you can often specify rendering settings like width and height within the HTML or using RenderingOptions.

For more HTML conversion options, check out the HTML string to PDF guide.

How to Render PDF Files from URLs and Razor Views?

IronPDF can also convert live web pages into PDF documents. This is perfect for generating reports or archiving web content:

public IActionResult RenderFromUrl(string url = "https://en.wikipedia.org/wiki/Main_Page")
{
    var renderer = new ChromePdfRenderer();
    renderer.RenderingOptions.EnableJavaScript = true;
    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 RenderFromUrl(string url = "https://en.wikipedia.org/wiki/Main_Page")
{
    var renderer = new ChromePdfRenderer();
    renderer.RenderingOptions.EnableJavaScript = true;
    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.com
$vbLabelText   $csharpLabel

Browser Document Viewer Displaying Generated PDF

How to Create a PDF Viewer in ASP .NET Web Application with IronPDF: Image 2 - Image 2 of 3 related to How to Create a PDF Viewer in ASP .NET Web Application with IronPDF

For converting Razor views to PDF, you'll need a helper method to render the view as HTML first. This approach lets you reuse your existing templates for both web display and PDF generation:

public async Task<IActionResult> ViewToPdf()
{
    var invoiceModel = new InvoiceModel
    {
        InvoiceNumber = 1001,
        InvoiceDate = DateTime.Now,
        CustomerName = "Acme Corp.",
        Items = new List<ItemModel>
        {
            new ItemModel { Description = "Product A", Quantity = 2, UnitPrice = 50.00m },
            new ItemModel { Description = "Service B", Quantity = 1, UnitPrice = 150.00m }
        }
    };
    invoiceModel.TotalAmount = invoiceModel.Items.Sum(i => i.LineTotal);
    // NOTE: RenderViewToStringAsync uses the Request services and ActionContext
    var htmlContent = await RenderViewToStringAsync("Invoice", invoiceModel);
    var renderer = new IronPdf.ChromePdfRenderer();
    var pdf = renderer.RenderHtmlAsPdf(htmlContent);
    return File(pdf.BinaryData, "application/pdf");
}
private async Task<string> RenderViewToStringAsync(string viewName, object model)
{
    // Use the current HttpContext's service provider (guaranteed available during a request)
    var actionContext = new ActionContext(HttpContext, RouteData, ControllerContext.ActionDescriptor);
    var viewEngine = HttpContext.RequestServices.GetRequiredService<IRazorViewEngine>();
    var tempDataFactory = HttpContext.RequestServices.GetRequiredService<ITempDataDictionaryFactory>();
    var tempData = tempDataFactory.GetTempData(HttpContext);
    ViewData.Model = model;
    var viewResult = viewEngine.FindView(actionContext, viewName, isMainPage: false);
    if (!viewResult.Success)
    {
        // Helpful error for debugging
        var searchedLocations = string.Join(Environment.NewLine, viewResult.SearchedLocations ?? Array.Empty<string>());
        throw new InvalidOperationException($"Couldn't find view '{viewName}'. Searched locations:{Environment.NewLine}{searchedLocations}");
    }
    await using var writer = new StringWriter();
    var viewContext = new ViewContext(
        actionContext,
        viewResult.View,
        ViewData,
        tempData,
        writer,
        new HtmlHelperOptions()
    );
    await viewResult.View.RenderAsync(viewContext);
    return writer.ToString();
}
public async Task<IActionResult> ViewToPdf()
{
    var invoiceModel = new InvoiceModel
    {
        InvoiceNumber = 1001,
        InvoiceDate = DateTime.Now,
        CustomerName = "Acme Corp.",
        Items = new List<ItemModel>
        {
            new ItemModel { Description = "Product A", Quantity = 2, UnitPrice = 50.00m },
            new ItemModel { Description = "Service B", Quantity = 1, UnitPrice = 150.00m }
        }
    };
    invoiceModel.TotalAmount = invoiceModel.Items.Sum(i => i.LineTotal);
    // NOTE: RenderViewToStringAsync uses the Request services and ActionContext
    var htmlContent = await RenderViewToStringAsync("Invoice", invoiceModel);
    var renderer = new IronPdf.ChromePdfRenderer();
    var pdf = renderer.RenderHtmlAsPdf(htmlContent);
    return File(pdf.BinaryData, "application/pdf");
}
private async Task<string> RenderViewToStringAsync(string viewName, object model)
{
    // Use the current HttpContext's service provider (guaranteed available during a request)
    var actionContext = new ActionContext(HttpContext, RouteData, ControllerContext.ActionDescriptor);
    var viewEngine = HttpContext.RequestServices.GetRequiredService<IRazorViewEngine>();
    var tempDataFactory = HttpContext.RequestServices.GetRequiredService<ITempDataDictionaryFactory>();
    var tempData = tempDataFactory.GetTempData(HttpContext);
    ViewData.Model = model;
    var viewResult = viewEngine.FindView(actionContext, viewName, isMainPage: false);
    if (!viewResult.Success)
    {
        // Helpful error for debugging
        var searchedLocations = string.Join(Environment.NewLine, viewResult.SearchedLocations ?? Array.Empty<string>());
        throw new InvalidOperationException($"Couldn't find view '{viewName}'. Searched locations:{Environment.NewLine}{searchedLocations}");
    }
    await using var writer = new StringWriter();
    var viewContext = new ViewContext(
        actionContext,
        viewResult.View,
        ViewData,
        tempData,
        writer,
        new HtmlHelperOptions()
    );
    await viewResult.View.RenderAsync(viewContext);
    return writer.ToString();
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output Viewed PDF

How to Create a PDF Viewer in ASP .NET Web Application with IronPDF: Image 3 - PDF rendered and Viewed in the browser

This helper method ensures you have a clean path to rendering your Razor view content into an HTML string, which IronPDF can then convert. The Razor to PDF tutorial covers this workflow in detail.

How to Handle Large PDF Files with Streaming?

When processing large PDF documents, streaming improves performance and reduces memory usage. Using a MemoryStream allows you to serve files efficiently:

public IActionResult StreamLargePdf()
{
    var renderer = new ChromePdfRenderer();
    var pdf = renderer.RenderHtmlAsPdf("<h1>Large Document</h1><p>Content here...</p>");
    var stream = new MemoryStream(pdf.BinaryData);
    return new FileStreamResult(stream, "application/pdf");
}
public IActionResult StreamLargePdf()
{
    var renderer = new ChromePdfRenderer();
    var pdf = renderer.RenderHtmlAsPdf("<h1>Large Document</h1><p>Content here...</p>");
    var stream = new MemoryStream(pdf.BinaryData);
    return new FileStreamResult(stream, "application/pdf");
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

For legacy ASP.NET Web Forms projects, you can implement similar functionality using event handlers:

protected void btnGeneratePdf_Click(object sender, EventArgs e)
{
    var renderer = new ChromePdfRenderer();
    var pdf = renderer.RenderHtmlAsPdf("<h1>Web Form PDF</h1>");
    Response.ContentType = "application/pdf";
    Response.BinaryWrite(pdf.BinaryData);
    Response.End();
}
protected void btnGeneratePdf_Click(object sender, EventArgs e)
{
    var renderer = new ChromePdfRenderer();
    var pdf = renderer.RenderHtmlAsPdf("<h1>Web Form PDF</h1>");
    Response.ContentType = "application/pdf";
    Response.BinaryWrite(pdf.BinaryData);
    Response.End();
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

What Features Does This PDF Viewer Control Support?

By leveraging the browser's native PDF viewer, your users get a rich set of features automatically: text selection for copying content, search functionality to find specific data, print options for physical copies, and download capabilities for offline access.

This also handles scenarios where users need to upload an existing document for viewing, editing, or merging. IronPDF's server-side processing handles JavaScript execution, CSS rendering, and forms—including interactive elements that users can fill out.

The framework supports tag helper integration, custom page sizes, headers and footers, watermarks, and even editing capabilities. For a complete feature overview, explore the IronPDF features page.

Conclusion

Creating a PDF viewer in an ASP.NET web application is remarkably simple with IronPDF. By combining its powerful rendering engine with the browser's default PDF viewer, you get a professional solution for displaying, processing, and handling PDF files in your web applications. Whether you're converting HTML, URLs, or Razor views, the implementation requires minimal code while delivering maximum functionality.

Ready to implement PDF viewing in your project? Start with a free trial of IronPDF and transform how your .NET Core web application handles documents. For production deployments, check out the licensing options that fit your team's needs. Want to see more of how IronPDF excels at conversion examples? Explore how IronPDF handles DOCX to PDF conversion, Image to PDF, and more in the helpful how-to guides.

Get stated with IronPDF now.
green arrow pointer

Frequently Asked Questions

How can I create a PDF viewer in an ASP.NET web application?

You can create a PDF viewer in an ASP.NET web application using IronPDF. It enables you to display PDF documents directly within your application, offering a seamless viewing experience without requiring external tools like Adobe Acrobat Reader.

What are the benefits of using IronPDF for PDF viewing in ASP.NET?

IronPDF provides a smooth and integrated PDF viewing experience in ASP.NET applications. It allows you to display documents inline, supports various file types, and eliminates the need for third-party PDF viewers, enhancing user experience.

Can I display interactive PDF forms in my ASP.NET web application?

Yes, with IronPDF, you can display interactive PDF forms within your ASP.NET web application. It supports the rendering of form fields and interactive elements, allowing users to interact with documents directly in the browser.

Is it possible to showcase invoices and reports using IronPDF in ASP.NET?

Absolutely, IronPDF is well-suited for displaying invoices, reports, and other document types in ASP.NET applications. It ensures your documents are rendered accurately and efficiently within the web application.

Do I need Adobe Acrobat Reader to view PDFs in ASP.NET applications using IronPDF?

No, you do not need Adobe Acrobat Reader to view PDFs in ASP.NET applications when using IronPDF. It allows you to render and view PDFs directly in the browser without third-party dependencies.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More