Last post Sep 02, 2010 04:17 PM by nagappmking
Feb 14, 2008 01:52 PM|kanaghaaj|LINK
I want to open a .PDF file on button click or hyperlink.
please help me.
Feb 14, 2008 04:24 PM|markarmitage|LINK
Where is the PDF? On your local machine, on a web server or stored in a database?
If you are using an <asp:Button or <asp:Hyperlink then you can double click it in design mode to create an event in the code behind. Any code to open the PDF can then be added in the event.
Feb 14, 2008 04:35 PM|lspence|LINK
If you have the PDF file on your local machine or stored on a server you can add the path to the button's click event or in the HyperLink's NavigateUrl. You can do something as simple as the following assuming you have acrobat installed.
or using System.Net;string pdfPath = Server.MapPath("~/SomePDFFile.pdf");WebClient client = new WebClient();Byte buffer = client.DownloadData(pdfPath);Response.ContentType = "application/pdf";Response.AddHeader("content-length", buffer.Length.ToString());Response.BinaryWrite(buffer);
using System.Net;string pdfPath = Server.MapPath("~/SomePDFFile.pdf");WebClient client = new WebClient();Byte buffer = client.DownloadData(pdfPath);Response.ContentType = "application/pdf";Response.AddHeader("content-length", buffer.Length.ToString());Response.BinaryWrite(buffer);
Feb 14, 2008 04:44 PM|Haissam|LINK
If you mean you want to allow the user to view the file check below link
Download files in ASP.NET
Feb 14, 2008 04:53 PM|bnarasimha|LINK
Feb 15, 2008 05:26 AM|kanaghaaj|LINK
what if i dont have acrobat installed? will it automatically download acrobat?
Feb 15, 2008 05:53 AM|Suprotim Agarwal|LINK
Here's some script used from dithered.com
var acrobat=new Object();
if (navigator.plugins && navigator.plugins.length)
for ( var x = 0, l = navigator.plugins.length; x < l; ++x )
if (navigator.plugins[x].description.indexOf('Adobe Acrobat') != -1)
if (acrobat.version.toString().length == 1) acrobat.version+='.0';
else if (window.ActiveXObject)
for (x=2; x<10; x++)
Check the version and redirect the user to the approriate location if adobe is not installed. AFAIK, this code is not 'truly' cross-browser.
Feb 22, 2009 02:14 AM|sueBee2|LINK
I was able to get Process.Start() to work in VS2005 built-in server but not IIS. I tried many options using Process.StartInfo but no luck. Have you been able to get this to work in IIS?
Nov 27, 2009 07:07 AM|mohammed askar|LINK
Jan 20, 2010 05:56 AM|RMhelaman|LINK
i got your codes benificiary for my problem, but i have a question regarding the Server.MapPath(?) there if i give the Pdf name, then it shows the same PDf file for all of my records in my gridveiw, instead, i want to have a folder which will be containing
all the PDF files for each single record, and i want the LinkField to open an aproperiate PDF file for a specific Record!
thanks in Advance
Jan 20, 2010 01:41 PM|lspence|LINK
You will need to iterate through the folder that contains your PDFs to display each one. Here's a sample that adds them to a ListBox and when you select a PDF it gets displayed.
<asp:Label ID="Label1" runat="server" Text="PDF Files" />
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True"
private string path = @"c:\PDF_Files\";
protected void Page_Load(object sender, EventArgs e)
DirectoryInfo di = new DirectoryInfo(path);
FileInfo fi = di.GetFiles();
foreach (FileInfo file in fi)
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
WebClient client = new WebClient();
Byte buffer = client.DownloadData(path + ListBox1.SelectedValue);
Response.ContentType = "application/pdf";
Jan 21, 2010 04:52 AM|RMhelaman|LINK
Thanks so much friend!
your codes really helped me, but not for my current prblm, i used it in a different application.
the PDFs working for my web page which has a GridView in it. what i did is:
I stored all the PDF files in a hard drive and then took a copy and paste it the "C:\Documents and Settings\mhelaman\My Documents\Visual Studio 2008\Projects\MoIFacSurvey"
which is the folder which contains all the pages of my project.
problem was, if i stroed the PDFs in my Solution Explorer, then the website was getting loaded, now when i put them there in the Visual Studio 2008 folder, Will it again make my website loaded, or it is fine???
Thats my question, hope you can answer...
Jan 21, 2010 01:54 PM|lspence|LINK
I'm not sure I completely follow you, but no matter where you store them, they will not impact your website. They will only get loaded when you select them from within your GridView, which in turn will reference the folder location you have them stored
in. If you are asking whether they will be loaded into memory when you start your website up, the answer is no. Does that answer your question?
Jan 24, 2010 06:14 AM|RMhelaman|LINK
that was what i was aksing. but good that the answer is NO.
Thanks for you support.
Dear, I got another problem.
I got a TreeView in my web page, and beside i've put a GridView, the TreeView contains Countries and Provinces (ChildNode), what i want is that by clicking any provinces (ChildNode) of the tree View, I want the GridView to show the selected Node's Details.
I mean, there should be a GridView by default, containing one of the TreeView Node's data, and then by clicking on any of the other nodes, the GridView should change the data accordingly,
Is it something like i should define SQL query for each Node? if yes, shall i write the codes under "TreeView1.SelectedNodeChanged" ? if I try this, then i am not able to define a particular node.
Mar 10, 2010 08:19 PM|deedee1969|LINK
Thank you, Thank U THANK U !!!
This worked perfectly. But now how do I force the user to close the PDF before going back to do something else in the webpage?
I am getting greedy now.
BTW, I am using VB.NET not C#
May 07, 2010 08:52 AM|surena|LINK
The problem scenerio is that i have some doc files residing at "F:\\test\\*".
I want to list out and open doc files from my asp.net application on hyperlink click, language is C#.
I went through your pdf example but it results into error
An invalid character was found in text content. Line 1, Position 1
mail me at email@example.com
May 07, 2010 09:35 AM|ashwoody|LINK
This article should get you started:
Although it is in VB, you should be able to convert easy enough
Jun 21, 2010 09:01 PM|AbidCharlotte49er|LINK
I'm using this snippet this way. I'm using a FileUpload control to upload files to a dir and show the uploaded files in ListBox user can double click the file name in the Listbox and open the file he uploaded & When he tries to upload a new file the code
in the Page_Load executed and the Selection in Listbox is still there Itried ClearSelection() , SelectionIndex = -1. But the item is still selected. so it won't allow me to upload a new file in there. Please tell me if I'm missing anything here. Thanks
01.if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "click")
03. int idx = LstBxUploadedFiles.SelectedIndex;
04. ListItem item = LstBxUploadedFiles.SelectedItem;
06. DirectoryInfo di = new DirectoryInfo(ConfigurationManager.AppSettings["DocumentsLocation"] + "\\" + (string)Session["CreatedFolderName"]);
07. FileInfo files = di.GetFiles();
08. foreach (FileInfo fi in files)
10. if (fi.Name == item.Text)
13. Response.AddHeader("Content-Disposition", "attachment; filename=" + fi.Name);
14. Response.AddHeader("Content-Length", fi.Length.ToString());
15. string CId = Request["__EVENTTARGET"];
26. LstBxUploadedFiles.Attributes.Add("ondblclick", ClientScript.GetPostBackEventReference(LstBxUploadedFiles, "click"));
Aug 07, 2010 02:04 PM|rafemuhammed|LINK
Note: If you installed pdf plugin to your browser, it will automatically displayed.
The above code will display save dialog to save pdf even if you have a browser with pdf plugin installed. So that the user can download directly instead of displaying it in browser and then saving..
Best of luck
Sep 02, 2010 04:17 PM|nagappmking|LINK
You can use HTML tag called <iframe> and src attribute as, pdf path this will display the pdf automatically, there are some attributes with URL called toolbar=no&menu=no etc. These will hide the pdf toolbar and menu options. If you want to show pdf for terms
and conditions kind, then this will be very useful.