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

10 jQuery Custom Scrollbar Plugins

10 jQuery Custom Scrollbar Plugins If you ever wanted to add some custom scrollbars to your website, to scroll the contents and the default browser scrollbars just doesn’t match up with your design, than make sure you check this list of 10 jQuery custom scrollbar plugins. Hope you find the following information helpful. 1. jScrollPane – custom cross-browser scrollbars Kelvin Luck’s jScrollPane was originally developed in December 2006. It is a jQuery plugin which provides you with custom scrollbars which work consistently across all modern browsers. You can style the scrollbars using simple CSS and they degrade gracefully where JavaScript is disabled. 2. Plugin JQuery : Scrollbar This page is written in french so use Google’s translate service to translate this page to your preferred language. Download is available for the plugin.  The purpose of this plugin is to add a scrollbar to the item of your choice, to view any content which is larger than the size – vizibl...

List of Best and Worst practices for SEO

Keywords 1 Keywords in <title> tag This is one of the most important places to have a keyword because what is written inside the <title> tag shows in search results as your page title. The title tag must be short (6 or 7 words at most) and the the keyword must be near the beginning. +3 2 Keywords in URL Keywords in URLs help a lot - e.g. -  http://domainname.com/seo-services.html , where “SEO services” is the keyword phrase you attempt to rank well for. But if you don't have the keywords in other parts of the document, don't rely on having them in the URL. +3 3 Keyword density in document text Another very important factor you need to  check . 3-7 % for major keywords is best, 1-2 for minor. Keyword density of over 10% is suspicious and looks more like keyword stuffing, than a naturally written text. +3 4 Keywords in anchor text Also very important, especially for  the anchor text of inbound links , because if you have the...

(Solved) Failure sending mail in Godaddy Server

(Solved) Failure sending mail in Godaddy Server I know that this is an old thread.  At the same time, I worked on this problem for two weeks.  I wish that I had the final answer a couple of weeks ago! If you have shared hosting at GoDaddy.com, you need to use these credentials WHILE your are developing your application: In the namespaces you have to add using System.Net.Mail; or if you are not using any .cs file the you can insert namespace as <% @ Import Namespace ="System.Net.Mail" %> //for mail(from,to,subject,message) MailMessage msg = new MailMessage("frommailid@yourdomain.com", " tomailid@yourdomain.com ", "hello" , "your message"); SmtpClient smtp = new SmtpClient (); smtp . Host = "smtpout.secureserver.net" ; // Only works locally (development time) smtp . EnableSsl = false ; smtp . UseDefaultCredentials = false ; smtp . Credentials = new System . Net . NetworkCredential ( "...