Posts tagged ‘open pdf file’

Open PDF File in Web Browser using C# Asp.net

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

OpenPDF.aspx

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

<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<h3 style=”color: #0000FF”>Open PDF files using ASP.NET</h3>
<div>
<asp:Button ID=”btnpdf” runat=”server” Text=”Click for open PDF file” Font-Bold=”True” OnClick=”btnpdf_Click” />
</div>
</form>
</body>
</html>

OpenPDF.aspx.cs

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());
Response.BinaryWrite(FileBuffer);
}
}
}

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