Embed a PDF on a ASP.NET page
Categories:
Embedding PDFs in ASP.NET Pages: A Comprehensive Guide

Learn various methods to display PDF documents directly within your ASP.NET web applications, from simple links to advanced viewer controls.
Embedding PDF documents directly into an ASP.NET web page can significantly enhance user experience by allowing users to view content without leaving your site. This article explores several common approaches, ranging from basic HTML embedding to more robust server-side solutions and third-party viewers. We'll cover the pros and cons of each method and provide practical code examples to help you implement them effectively.
Method 1: Basic HTML Embedding with <iframe>
or <embed>
The simplest way to display a PDF is by using standard HTML tags like <iframe>
or <embed>
. These tags instruct the browser to render the PDF using its built-in viewer or a plugin. While straightforward, this method relies heavily on client-side browser capabilities and installed plugins, which can lead to inconsistent user experiences across different browsers and devices.
<iframe src="/path/to/your/document.pdf" width="100%" height="600px" type="application/pdf">
<p>It appears your browser does not support iframes. You can <a href="/path/to/your/document.pdf">download the PDF</a> instead.</p>
</iframe>
<!-- Alternatively, using <embed> tag -->
<embed src="/path/to/your/document.pdf" type="application/pdf" width="100%" height="600px" />
Embedding a PDF using <iframe>
or <embed>
in HTML
<iframe>
and <embed>
tags offer limited control over the PDF viewer's appearance and functionality. Browser compatibility and the presence of PDF plugins can vary, potentially leading to a suboptimal user experience for some visitors. Always provide a download link as a fallback.Method 2: Serving PDFs via Generic Handler (.ashx
)
For more control, especially when dealing with dynamically generated or secured PDFs, you can serve the PDF content through a generic handler (.ashx
). This allows you to perform server-side logic, such as authentication, authorization, or dynamic content generation, before streaming the PDF to the client. The handler sets the appropriate Content-Type
header, prompting the browser to display the PDF.
// MyPdfHandler.ashx.cs
using System.Web;
using System.IO;
public class MyPdfHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
// Example: Get PDF path from query string or database
string pdfFileName = context.Request.QueryString["file"];
string pdfPath = context.Server.MapPath($"~/Pdfs/{pdfFileName}.pdf");
if (File.Exists(pdfPath))
{
context.Response.ContentType = "application/pdf";
context.Response.AppendHeader("Content-Disposition", $"inline; filename={pdfFileName}.pdf");
context.Response.TransmitFile(pdfPath);
}
else
{
context.Response.StatusCode = 404;
context.Response.StatusDescription = "File Not Found";
}
}
public bool IsReusable
{
get { return false; }
}
}
C# code for a generic handler (.ashx
) to serve PDF files
<!-- In your ASP.NET page (.aspx or .cshtml) -->
<iframe src="/MyPdfHandler.ashx?file=SampleDocument" width="100%" height="600px" type="application/pdf">
<p>Your browser does not support iframes. Please <a href="/MyPdfHandler.ashx?file=SampleDocument">download the PDF</a>.</p>
</iframe>
Using the generic handler with an <iframe>
flowchart TD A[User Request ASP.NET Page] --> B{ASP.NET Page Loads} B --> C[HTML `<iframe>` or `<embed>` tag] C --> D[Browser Requests PDF URL] D --> E{PDF URL Points to .ashx Handler} E --> F[Handler: Authenticate/Authorize?] F -- Yes --> G[Handler: Read PDF File] F -- No --> H[Handler: Return 401/403] G --> I[Handler: Set Content-Type: application/pdf] I --> J[Handler: Stream PDF to Browser] J --> K[Browser Displays PDF] H --> L[Browser Shows Error]
Workflow for serving PDFs via a generic handler in ASP.NET
Method 3: Using Third-Party PDF Viewers
For a more consistent and feature-rich experience, consider integrating a third-party PDF viewer library. These libraries often provide a unified look and feel across browsers, advanced features like annotations, search, and custom toolbars, and better control over the viewing environment. Popular options include PDF.js (Mozilla's JavaScript library) or commercial components like PSPDFKit, Syncfusion, or Telerik PDF Viewer.
1. Choose a Viewer
Select a third-party PDF viewer library that best fits your project's requirements and budget. For this example, we'll consider PDF.js.
2. Download and Integrate PDF.js
Download the stable build of PDF.js from its official GitHub repository. Extract the files and place them in a suitable directory within your ASP.NET project (e.g., /Scripts/pdfjs
).
3. Create a Viewer Page
Create an ASP.NET page (e.g., PdfViewer.aspx
or PdfViewer.cshtml
) that will host the PDF.js viewer. This page will include the necessary HTML, CSS, and JavaScript to initialize the viewer.
4. Configure PDF.js
In your viewer page's JavaScript, configure PDF.js to load your desired PDF. You'll typically pass the PDF's URL to the viewer. This URL can be a direct path or point to your generic handler for dynamic content.
5. Embed the Viewer Page
On your main ASP.NET page, embed the PdfViewer.aspx
(or PdfViewer.cshtml
) using an <iframe>
tag, pointing its src
to your viewer page with the PDF URL as a query parameter.
While a full implementation of PDF.js is extensive, the core idea involves including its JavaScript and CSS files, then using its API to render a PDF onto an HTML <canvas>
element. This provides a highly customizable viewing experience.