<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>
|
using System;
using System.Net;
|
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);
}
}
|
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
|
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|
|


Subscribe by RSS
Subscribe by Email
13 comments :
nice post..But if User do not want to let the others to download/Save .pdf file from Website..then...How is it possible??
how to redirect to siteAnalytics.pdf..where we have to create pdf file ....
Thanks.... but how i can ad text in pdf files using itextsharp.dlll
How to disable to Right Click and print option and control in key on pdf files
in this page "facebook" pops up while on mouseover effect and then gets actual position after mouseover completes.I want to add such effect in my website but can not identify which control is used over there.plz let me know about it with detail.
How to disable right click in pdf file on browser
How to display xml file in browser using asp.net with c#..
sir how can i get data from web site but this site is open only in Internet explorer and then how can i automatically open internet and goto loggin using sending credential after that i want to extract data and store into data base using C# lang .please help me ti achive
Sir,i dont want to download pdf/doc files,only want to view the files on click event of a button that is stored in sql server 2008 in bytes..type of column in sql server is varbinary(max) ....pls help...
its very useful to me sir. how to diable the save option in the pdf file.
thanks in advance.
hi, its working only for pdf file, but wants to show all file as like doc, xls file.
Hello,Can be set file name while saving the file using save button.