Posts tagged ‘open pdf file’

Open PDF File in Web Browser using C#

Using below code, no need to open file physically. We can also protect file to open from authorize access.


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

<html xmlns=””&gt;
<head runat=”server”>
<form id=”form1″ runat=”server”>
<h3 style=”color: #0000FF”>Open PDF files using ASP.NET</h3>
<asp:Button ID=”btnpdf” runat=”server” Text=”Click for open PDF file” Font-Bold=”True” OnClick=”btnpdf_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 OpenPDF : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)

protected void btnpdf_Click(object sender, EventArgs e)
string FilePath = Server.MapPath(“test.pdf”);
WebClient User = new WebClient();
Byte[] FileBuffer = User.DownloadData(FilePath);
if (FileBuffer != null)
Response.ContentType = “application/pdf”;
Response.AddHeader(“content-length”, FileBuffer.Length.ToString());

If this blog is helpful to you, Pls. click on like it.