Description:
In this example we explain that how to Display (Show) PDF File in Web Browser using C# in Asp.Net or
how to Open PDF file in Web Browser in
Asp.Net using C#.
In previous example we already explain that to show or display PDF
file in Web Browser in MVC Razor here we explain the same thing in Asp.Net
page.
<%@
Page Language="C#" AutoEventWireup="true" CodeFile="OpenPDF.aspx.cs" Inherits="OpenPDF" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to
Open PDF File in Web Browser using C# in Asp.Net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnOpen" Text="1st Way to Show PDF In Browser" Font-Bold="true"
runat="server"
OnClick="btnOpen_Click" />
<asp:Button ID="btnpdf" Text="2nd Way to Show PDF In Browser" Font-Bold="true"
runat="server"
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 btnOpen_Click(object
sender, EventArgs e)
{
Response.Redirect("Test.pdf");
}
// Second way to
Show PDF in browser by setting Content Type of the Response object and add the
binary form of the pdf in the header
protected void btnpdf_Click(object
sender, EventArgs e)
{
string
path = Server.MapPath("Test.pdf");
WebClient
client = new WebClient();
Byte[]
buffer = client.DownloadData(path);
if
(buffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length",
buffer.Length.ToString());
Response.BinaryWrite(buffer);
}
}
}
0 comments:
Post a Comment