Introduction
Description
In previous articles I explained Export Gridview data to PDF, Send values from one page to another page using QueryString,
Joins in SQL Server, Highlight Gridview records based on search and many
articles relating to Gridview,
SQL ,jQuery,asp.net,
C#,VB.NET.
Now I will explain how to open pdf file in web browser in asp.net
using C# and VB.NET.
To implement this concept first create one new website
and add one of your existing pdf file to your website after that open Default.aspx page and 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 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
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. |
|||
|
|||
40 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.
how to show multiple pdf files in a slider
i hv multiple pdf files in a folder n i need to show all files in a slider with paging(in C#). Pls give me some suggestion... Parth:)
post is good but i have a problem that i want to open docx file on the browser without using microsoft interlop library.plz help me
How can I get the page number which i'm reading from the broweser? I need the current page to be extracted as a separate pdf file
Thx for the code and sample. How would you show the PDF inside a jquery dialog box? I have the PDF bytes() but I don't know ho to show them inside a DIV/dialog box.
Thanks
i have scanned 10 personal files of employees.based on the id number the pdf link should open.thnx plz give suggestions
i want to display that data in pdf file that are show in repeater using asp .net with C#. what is the method for this.
hello I am making a project which involves the admin to view pdf files from the database and approve them...hence i am asking how can i code so that the files would be displayed in a grid view and admin can view them
NOTE- the files should be present in the database and not like the above mentioned code.
hello I am making a project which involves the admin to view pdf files from the database and approve them...hence i am asking how can i code so that the files would be displayed in a grid view and admin can view them
NOTE- the files should be present in the database and not like the above mentioned code.
how to open any format file ?
how to open two files this way?i am getting error in opened file while i am trying to open two file at the same time
How to Print PDF File using Window.Print("D:/a.pdf") ?
Is there any possibility to get the currently viewing pdf file page number in a text box. Which means when i am viewing 4th page of pdf file, my text box should show 4. after scrolling to next page, text box shd show 5.. like wise i wanted. kindly help me.. we are in urgent to get this code. we found possibility of inserting text, link and all options in pdf.. We can also able to find total pages count. But how to capture current pdf page number in text box.
How can I get the page number which i'm reading from the browser? I need the current page number to be extracted in a text box.. kindly help me with the code.
Thank you suresh so much,U r just brilliant
Great help. But it requires PDF Reader to be installed on client machine. Also I dont want anyone to download my files. Is there any pdf viewer which can help me in this?
Thanks in advance
nice post..But if User do not want to let the others to download/Save .pdf file from Website..then...How is it possible??
it not working with update panel
But its giving an error when size of pdf file is large
How to stay on same page and open a dialogue to save pdf file.Please reply fast urgent.
Thanks in advance
It is working nice.
But I want open pdf file in panel using choose file control instead of iframe src in asp.net using c#
hi, can you please help me?? I'm newbie in ASP.net. I'm trying to open a PDF file using ASP.net but I can't seem to work it out. I want to view the PDF file which is stored in (1) local machine and (2) web server. Thanks.
Thank you so much for posting this! I was using this on IIS6 without issue but when I moved to IIS7.5 we began seeing problems with Internet Explorer displaying gibberish when trying to load another PDF or navigate to another page. Found that I needed to add the following at the end of the above to correct the problem:
Response.End()
The page where the report is triggered is closed as soon as the user closes the report in the browser.
Awesome post - helped me get rolling on a back office application I am developing for my business. Thanks a ton.
1)How do i disable Save/print option for pdf file? i want the user can only view the file...
2)How can i view doc/docx/xls file also in browser?
Please help. Thanks in advance
Have tried in Mozila fire fox and IE. this is not working.Do you have any solution ?
Note: Only a member of this blog may post a comment.