How to Open PDF Files in Web Brower Using ASP.NET


In this article, I will explain how to open a PDF file in a web browser using ASP.NET.

Use the following procedure.

Step 1

Open Visual Studio 2012 and click "File" -> "New" -> "web site...". A window is opened. In this window, click "Empty Web Site Application" under Visual C#.


Give the name of your application as "Open_PDF" and then click "Ok".

Step 2

After this session the project has been created, A new window is opened on the right side. This window is called the Solution Explorer. Solution Explorer contains the pdf file, css file and aspx files and looks like this:


Complete Program


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Open_PDF.aspx.cs" Inherits="Open_PDF" %>


<!DOCTYPE html>


<html xmlns="">

<head runat="server">




    <form id="form1" runat="server">

        <h3 style="color: #0000FF">Open PDF files using ASP.NET</h3>


        <asp:Button ID="bttnpdf" runat="server" Text="Click for open PDF" Font-Bold="True" OnClick="bttnpdf_Click" />







using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Net;


public partial class Open_PDF : System.Web.UI.Page


    protected void Page_Load(object sender, EventArgs e)




    protected void bttnpdf_Click(object sender, EventArgs e)


        string FilePath = Server.MapPath("javascript1-sample.pdf");

        WebClient User = new WebClient();

        Byte[] FileBuffer = User.DownloadData(FilePath);

        if (FileBuffer != null)


            Response.ContentType = "application/pdf";

            Response.AddHeader("content-length", FileBuffer.Length.ToString());






Output 1


Click on the button.

Output 2


For more information, download the attached sample application.

Up Next
    Ebook Download
    View all
    View all