Skip to main content

Open PDF Files in Web Brower Using ASP.NET

Step 1: First of all create new website 
Step2: Add existing pdf file to your website
Step3:  After that open Default.aspx page and now write the following code 
  
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Open PDF File in Web Browser 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>
 
Now open Default.aspx code behind file and add the following namespaces

C# Code

using System;
using System.Net;
Once namespaces added write the following code 
protected void Page_Load(object sender, EventArgs e)
{
}
// First Way to show PDF in browser
protected void btnOpen_Click(object sender, EventArgs e)
{
Response.Redirect("SiteAnalytics.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("SiteAnalytics.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);
}
}


VB.NET Code

Imports System.Net
Partial Class VBCodetoShowPDF
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs)
End Sub
' First Way to show PDF in browser
Protected Sub btnOpen_Click(sender As Object, e As EventArgs)
Response.Redirect("SiteAnalytics.pdf")
End Sub
' 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 Sub btnpdf_Click(sender As Object, e As EventArgs)
Dim path As String = Server.MapPath("SiteAnalytics.pdf")
Dim client As New WebClient()
Dim buffer As [Byte]() = client.DownloadData(path)
If buffer IsNot Nothing Then
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", buffer.Length.ToString())
Response.BinaryWrite(buffer)
End If
End Sub
End Class

Demo 





Download Sample Code Attached






Note: The content is taken from http://www.aspdotnet-suresh.com/2012/11/aspnet-open-pdf-file-in-web-browser.html#comment-734013350831950013 

And i heartly thanks to Suresh sir to post such a fine  article

Comments

Popular posts from this blog

Working with Ajax UpdatePanel in asp.net

During work with our applications if we entered any values in textbox controls and click on a button in form we will see full postback of our page and we will lost all the controls values whatever we entered previously this happend because of postback. If we want to avoid this full postback of page and round trip to server we need to write much code instead of writing much code we can use ajax updatepanel control. Ajax updatepanel will help us to avoid full postback of the page i.e., avoid refresh of the whole page content with postback and stop flickering of the page which is associated with a postback and allows only partial postbacks. By using Ajax updatepanel we can refresh only required part of page instead of refreshing whole page. Ajax updatepanel works on very smooth concepts. but in some cases we got stuck while using Ajax updatepanel. But you need not to worry. Here i am showing you how to resolve the issues for different cases Lets start with very basic use of A

connect ftp with gridview / display files in gridview from ftp or server

HTML Markup Below is the HTML Markup of the page, where I have an ASP.Net control  FileUpload  to upload files, a  Button control to trigger file uploads and an ASP.Net GridView control to display the files from folder. < asp : FileUpload   ID ="FileUpload1"   runat ="server"   /> < asp : Button   ID ="btnUpload"   runat ="server"   Text ="Upload"   OnClick ="UploadFile"   /> < hr   /> < asp : GridView   ID ="GridView1"   runat ="server"   AutoGenerateColumns ="false"   EmptyDataText   =   "No files uploaded">      < Columns >          < asp : BoundField   DataField ="Text"   HeaderText ="File Name"   />          < asp : TemplateField >              < ItemTemplate >                  < asp : LinkButton   ID ="lnkDownload"   Text   =   "Download"   CommandArgument   =   ' &

Google Chart - Simple API and even an ASP.Net Control

Visualization: Column Chart A vertical bar chart that is rendered within the browser using   SVG   or   VML . Displays tips when hovering over bars. For a horizontal version of this chart, see the   Bar Chart . By: Google This is the new version of the column chart. For tips on how to migrate to the new charts, please see the  release notes  page. If you need to access the older version, you can find the documentation  here . Example Code it yourself on the  Visualization Playground <html>   <head>     <script type = "text/javascript" src = "https://www.google.com/jsapi" ></script>     <script type = "text/javascript" >       google . load ( "visualization" , "1" , { packages :[ "corechart" ]});       google . setOnLoadCallback ( drawChart );       function drawChart () {         var data = google . visualization . arrayToDataTable ([           [ 'Year'