Open PDF file on button click or hyperlink from asp.nethttp://forums.asp.net/t/1219837.aspx/1?Open+PDF+file+on+button+click+or+hyperlink+from+asp+netThu, 02 Sep 2010 15:17:09 -040012198372173110http://forums.asp.net/p/1219837/2173110.aspx/1?Open+PDF+file+on+button+click+or+hyperlink+from+asp+netOpen PDF file on button click or hyperlink from asp.net <p>I want to open a .PDF file on button click or hyperlink.</p> <p>please help me.</p> 2008-02-14T12:52:45-05:002173497http://forums.asp.net/p/1219837/2173497.aspx/1?Re+Open+PDF+file+on+button+click+or+hyperlink+from+asp+netRe: Open PDF file on button click or hyperlink from asp.net <p>Where is the PDF? On your local machine, on a web server or stored in a database?</p> <p>If you are using an &lt;asp:Button or &lt;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.&nbsp;</p> 2008-02-14T15:24:32-05:002173534http://forums.asp.net/p/1219837/2173534.aspx/1?Re+Open+PDF+file+on+button+click+or+hyperlink+from+asp+netRe: Open PDF file on button click or hyperlink from asp.net <p>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. <br> </p> <pre class="prettyprint">Response.Redirect(&quot;~/somePDFFile.pdf&quot;);</pre><pre class="prettyprint">or&nbsp;<pre class="prettyprint"><span class="kwd">using</span> System.Net;<br><br><span class="kwd">string</span> pdfPath = Server.MapPath(<span class="st">"~/SomePDFFile.pdf"</span>);<br>WebClient client = <span class="kwd">new</span> WebClient();<br>Byte[] buffer = client.DownloadData(pdfPath);<br>Response.ContentType = <span class="st">"application/pdf"</span>;<br>Response.AddHeader(<span class="st">"content-length"</span>, buffer.Length.ToString());<br>Response.BinaryWrite(buffer);</pre>&nbsp;&nbsp;</pre>&nbsp;&nbsp; 2008-02-14T15:35:56-05:002173565http://forums.asp.net/p/1219837/2173565.aspx/1?Re+Open+PDF+file+on+button+click+or+hyperlink+from+asp+netRe: Open PDF file on button click or hyperlink from asp.net <p>If you mean you want to allow the user to view the file check below link</p> <p><a href="http://dotnetslackers.com/community/blogs/haissam/archive/2007/04/03/Downloading-Files-C_2300_.aspx" target="_blank">Download files in ASP.NET</a></p> <p>HC<br> &nbsp;</p> 2008-02-14T15:44:51-05:002173608http://forums.asp.net/p/1219837/2173608.aspx/1?Re+Open+PDF+file+on+button+click+or+hyperlink+from+asp+netRe: Open PDF file on button click or hyperlink from asp.net <p>Try</p> <p>&nbsp;</p> <p>Process.Start(&quot;pdffile.pdf&quot;) ;</p> 2008-02-14T15:53:55-05:002174778http://forums.asp.net/p/1219837/2174778.aspx/1?Re+Open+PDF+file+on+button+click+or+hyperlink+from+asp+netRe: Open PDF file on button click or hyperlink from asp.net <p>what if i dont have acrobat installed? will it automatically download acrobat?</p> <p>&nbsp;</p> 2008-02-15T04:26:56-05:002174823http://forums.asp.net/p/1219837/2174823.aspx/1?Re+Open+PDF+file+on+button+click+or+hyperlink+from+asp+netRe: Open PDF file on button click or hyperlink from asp.net <p><font face="verdana,geneva">Hi,</font></p> <p><font face="verdana,geneva">Here's some script&nbsp;used&nbsp;from dithered.com</font></p> <p><font face="verdana,geneva">&lt;script language=&quot;javascript&quot;&gt;</font></p> <p><font face="verdana,geneva">function detectAcrobat()</font></p> <p><font face="verdana,geneva">{<br> var acrobat=new Object();<br> <br> acrobat.installed=false;<br> acrobat.version='0.0';<br> <br> if (navigator.plugins &amp;&amp; navigator.plugins.length)<br> {<br> for ( var x = 0, l = navigator.plugins.length; x &lt; l; &#43;&#43;x ) <br> {<br> if (navigator.plugins[x].description.indexOf('Adobe Acrobat') != -1)<br> {<br> acrobat.version=parseFloat(navigator.plugins[x].description.split('Version ')[1]);<br> <br> if (acrobat.version.toString().length == 1) acrobat.version&#43;='.0';<br> <br> acrobat.installed=true;<br> break;<br> }<br> }<br> }<br> else if (window.ActiveXObject)<br> {<br> for (x=2; x&lt;10; x&#43;&#43;)<br> {<br> try<br> {<br> oAcro=eval(&quot;new ActiveXObject('PDF.PdfCtrl.&quot;&#43;x&#43;&quot;');&quot;);<br> if (oAcro)<br> {<br> acrobat.installed=true;<br> acrobat.version=x&#43;'.0';<br> }<br> }<br> catch(e) {}<br> }<br> <br> try<br> {<br> oAcro4=new ActiveXObject('PDF.PdfCtrl.1');<br> if (oAcro4)<br> {<br> acrobat.installed=true;<br> acrobat.version='4.0';<br> }<br> }<br> catch(e) {}<br> <br> try<br> {<br> oAcro7=new ActiveXObject('AcroPDF.PDF.1');<br> if (oAcro7)<br> {<br> acrobat.installed=true;<br> acrobat.version='7.0';<br> }<br> }<br> catch(e) {}<br> <br> }<br> alert(acrobat.installed);<br> alert(acrobat.version);</font></p> <p><font face="verdana,geneva">}<br> &lt;/script&gt;<br> </font></p> <p><font face="verdana,geneva">Check the version and redirect the user to the approriate location if adobe is not installed. AFAIK, this code is not 'truly' cross-browser.</font></p> <p><font face="verdana,geneva">HTH,<br> Suprotim Agarwal</font></p> <p><font face="verdana,geneva">-----<br> </font><a href="http://www.dotnetcurry.com/"><font face="verdana,geneva">http://www.dotnetcurry.com</font></a><br> <a href="http://www.sqlservercurry.com/"><font face="verdana,geneva">http://www.sqlservercurry.com</font></a><br> <font face="verdana,geneva">-----&nbsp;</font></p> <p><font face="verdana,geneva"></font>&nbsp;</p> 2008-02-15T04:53:04-05:002957680http://forums.asp.net/p/1219837/2957680.aspx/1?Re+Open+PDF+file+on+button+click+or+hyperlink+from+asp+netRe: Open PDF file on button click or hyperlink from asp.net <p>I was able to get Process.Start() to work in VS2005 built-in server but not IIS.&nbsp; I tried many options using Process.StartInfo but no luck.&nbsp; Have you been able to get this to work in IIS?</p> 2009-02-22T01:14:14-05:003535466http://forums.asp.net/p/1219837/3535466.aspx/1?Re+Open+PDF+file+on+button+click+or+hyperlink+from+asp+netRe: Open PDF file on button click or hyperlink from asp.net <p>&nbsp;</p> <p>(http://support.microsoft.com/directory/article.asp?ID=KB;EN-US;q306654&amp;lex).</p> 2009-11-27T06:07:43-05:003628205http://forums.asp.net/p/1219837/3628205.aspx/1?Re+Open+PDF+file+on+button+click+or+hyperlink+from+asp+netRe: Open PDF file on button click or hyperlink from asp.net <p>Hello Ispence!<br> </p> <p>i got your codes benificiary for my problem,&nbsp;but i have a question regarding the Server.MapPath(?) there if i give the&nbsp;Pdf name,&nbsp;then it shows the same PDf file for all&nbsp;of my records in my gridveiw, instead, i want to have a folder which will be containing all the PDF files for&nbsp;each single record, and i want the LinkField to open an aproperiate PDF file for a specific Record!</p> <p>&nbsp;</p> <p>thanks in Advance&nbsp;</p> 2010-01-20T04:56:50-05:003629118http://forums.asp.net/p/1219837/3629118.aspx/1?Re+Open+PDF+file+on+button+click+or+hyperlink+from+asp+netRe: Open PDF file on button click or hyperlink from asp.net <p>You&nbsp;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&nbsp;a PDF&nbsp;it gets displayed.</p> <p>&lt;div&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text=&quot;PDF Files&quot; /&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;br /&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:ListBox ID=&quot;ListBox1&quot; runat=&quot;server&quot; AutoPostBack=&quot;True&quot; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; onselectedindexchanged=&quot;ListBox1_SelectedIndexChanged&quot; /&gt;<br> &lt;/div&gt;</p> <pre class="prettyprint">using System.IO; using System.Net; private string path = @&quot;c:\PDF_Files\&quot;; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { DirectoryInfo di = new DirectoryInfo(path); FileInfo[] fi = di.GetFiles(); foreach (FileInfo file in fi) { ListBox1.Items.Add(file.Name); } } } protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { WebClient client = new WebClient(); Byte[] buffer = client.DownloadData(path &#43; ListBox1.SelectedValue); Response.ContentType = &quot;application/pdf&quot;; Response.AddHeader(&quot;content-length&quot;, buffer.Length.ToString()); Response.BinaryWrite(buffer); }</pre><PRE></PRE> 2010-01-20T12:41:59-05:003630612http://forums.asp.net/p/1219837/3630612.aspx/1?Re+Open+PDF+file+on+button+click+or+hyperlink+from+asp+netRe: Open PDF file on button click or hyperlink from asp.net <p>Thanks so much&nbsp;friend!</p> <p>&nbsp;</p> <p>your codes&nbsp;really helped me, but not for my current prblm, i used it in a different&nbsp;application.&nbsp;</p> <p>the PDFs working&nbsp;for my web page which has a GridView in it. what i did is: </p> <p>I stored all the PDF files in a hard drive and then took a copy and paste&nbsp;it the &nbsp;&quot;C:\Documents and Settings\mhelaman\My Documents\Visual Studio 2008\Projects\MoIFacSurvey&quot;</p> <p>which is the folder which contains all the pages of my project.</p> <p>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???</p> <p>&nbsp;</p> <p>Thats my question, hope you can answer...</p> <p>&nbsp;</p> <p>Thanks</p> 2010-01-21T03:52:24-05:003631652http://forums.asp.net/p/1219837/3631652.aspx/1?Re+Open+PDF+file+on+button+click+or+hyperlink+from+asp+netRe: Open PDF file on button click or hyperlink from asp.net <p>I'm not sure I completely follow you, but no matter where you store them, they will not impact your website.&nbsp; 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?<br> </p> 2010-01-21T12:54:09-05:003636496http://forums.asp.net/p/1219837/3636496.aspx/1?Re+Open+PDF+file+on+button+click+or+hyperlink+from+asp+netRe: Open PDF file on button click or hyperlink from asp.net <p>Exactly Ispence!</p> <p>that was what i was aksing. but good that the&nbsp;answer is&nbsp;NO.</p> <p>Thanks for you support.</p> <p>&nbsp;</p> <p>Dear,&nbsp;I&nbsp;got&nbsp;another problem.</p> <p>I got a TreeView in my web page, and beside i've put a GridView,&nbsp;the TreeView contains Countries and Provinces (ChildNode), what i want is that&nbsp;by clicking any provinces (ChildNode) of the tree View, I want the GridView to show the selected Node's Details.</p> <p>I mean, there&nbsp;should be&nbsp;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,</p> <p>Is it something like i should define SQL query for each Node? if yes, shall i write the codes&nbsp;under &quot;TreeView1.SelectedNodeChanged&quot; ?&nbsp;if&nbsp;I try this, then i am not&nbsp;able to define a particular node.&nbsp;</p> 2010-01-24T05:14:54-05:003724226http://forums.asp.net/p/1219837/3724226.aspx/1?Re+Open+PDF+file+on+button+click+or+hyperlink+from+asp+netRe: Open PDF file on button click or hyperlink from asp.net <p>Thank you, Thank U THANK U !!!</p> <p>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?&nbsp;</p> <p>I am getting greedy now. </p> <p>BTW, I am using VB.NET not C#</p> 2010-03-10T19:19:58-05:003823038http://forums.asp.net/p/1219837/3823038.aspx/1?Re+Open+PDF+file+on+button+click+or+hyperlink+from+asp+netRe: Open PDF file on button click or hyperlink from asp.net <p>Hi...</p> <p>The problem scenerio is that i have some doc files residing at &quot;F:\\test\\*&quot;.</p> <p>I want to list out and open doc files &nbsp;from my asp.net application on hyperlink click, language is C#.</p> <p>I went through your pdf example but it results into error</p> <p style="font:normal normal bold 8pt/11pt verdana">An invalid character was found in text content. Line 1, Position 1</p> <pre style="color:#000000; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:10pt; font-weight:normal; line-height:12pt; font-style:normal; font-variant:normal; margin:8px"><span style="color:blue">?&#17;&#26;? ^</span></pre> <pre style="color:#000000; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:10pt; font-weight:normal; line-height:12pt; font-style:normal; font-variant:normal; margin:8px"><span style="color:#0000ff">Please help...</span></pre> <pre style="color:#000000; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:10pt; font-weight:normal; line-height:12pt; font-style:normal; font-variant:normal; margin:8px"><span style="color:#0000ff">mail me at sonusneh@gmail.com</span></pre> 2010-05-07T07:52:59-04:003823113http://forums.asp.net/p/1219837/3823113.aspx/1?Re+Open+PDF+file+on+button+click+or+hyperlink+from+asp+netRe: Open PDF file on button click or hyperlink from asp.net <p>This article should get you started:</p> <p><a href="http://aspnet.4guysfromrolla.com/articles/102109-1.aspx">http://aspnet.4guysfromrolla.com/articles/102109-1.aspx</a>&nbsp;</p> <p>Although it is in VB, you should be able to convert easy enough</p> 2010-05-07T08:35:46-04:003938126http://forums.asp.net/p/1219837/3938126.aspx/1?Re+Open+PDF+file+on+button+click+or+hyperlink+from+asp+netRe: Open PDF file on button click or hyperlink from asp.net <p>Hey Ispence, </p> <p>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 &amp; 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.&nbsp;But the item is still selected.&nbsp;so it won't allow me to upload a new file in there.&nbsp;Please tell me if I'm missing anything here. Thanks&nbsp;</p> <p>&nbsp;</p> <pre class="prettyprint">01.if (Request[&quot;__EVENTARGUMENT&quot;] != null &amp;&amp; Request[&quot;__EVENTARGUMENT&quot;] == &quot;click&quot;) 02. { 03. int idx = LstBxUploadedFiles.SelectedIndex; 04. ListItem item = LstBxUploadedFiles.SelectedItem; 05. 06. DirectoryInfo di = new DirectoryInfo(ConfigurationManager.AppSettings[&quot;DocumentsLocation&quot;] &#43; &quot;\\&quot; &#43; (string)Session[&quot;CreatedFolderName&quot;]); 07. FileInfo[] files = di.GetFiles(); 08. foreach (FileInfo fi in files) 09. { 10. if (fi.Name == item.Text) 11. { 12. Response.ClearContent(); 13. Response.AddHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=&quot; &#43; fi.Name); 14. Response.AddHeader(&quot;Content-Length&quot;, fi.Length.ToString()); 15. string CId = Request[&quot;__EVENTTARGET&quot;]; 16. LstBxUploadedFiles.ClearSelection(); 17. Response.TransmitFile(fi.FullName); 18. Response.End(); 19. 20. 21. } 22. } 23. 24. 25. } 26. LstBxUploadedFiles.Attributes.Add(&quot;ondblclick&quot;, ClientScript.GetPostBackEventReference(LstBxUploadedFiles, &quot;click&quot;)); 27. LstBxUploadedFiles.ClearSelection();</pre> <p><br> &nbsp;</p> 2010-06-21T20:01:09-04:004014971http://forums.asp.net/p/1219837/4014971.aspx/1?Re+Open+PDF+file+on+button+click+or+hyperlink+from+asp+netRe: Open PDF file on button click or hyperlink from asp.net &lt;div style=&quot;background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; color: black; font-family: courier new;&quot; mce_style=&quot;background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; color: black; font-family: courier new;&quot;&gt;Try this..&lt;/div&gt;&lt;div style=&quot;background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; color: black; font-family: courier new;&quot; mce_style=&quot;background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; color: black; font-family: courier new;&quot;&gt;<br> &lt;/div&gt;&lt;div style=&quot;background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; color: black; font-family: courier new;&quot; mce_style=&quot;background: white none repeat scroll 0% 0%; font-size: 10pt; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; color: black; font-family: courier new;&quot;&gt;Response.ContentType = &quot;application/pdf&quot;;<br> Response.AppendHeader(&quot;Content-Disposition&quot;,&quot;attachment; filename=testpdf.pdf&quot;);<br> Response.TransmitFile( Server.MapPath(&quot;~/testpdf.pdf&quot;) );<br> Response.End();&lt;/div&gt; <p><br> </p> <p>Note: If you installed pdf plugin to your browser, it will automatically displayed. </p> <p>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..</p> <p>Best of luck</p> <p>rafemuhammed</p> <p>MCA<br> </p> <p><br> </p> 2010-08-07T13:04:15-04:004058317http://forums.asp.net/p/1219837/4058317.aspx/1?Re+Open+PDF+file+on+button+click+or+hyperlink+from+asp+netRe: Open PDF file on button click or hyperlink from asp.net <p>You can use HTML tag called &lt;iframe&gt; and src attribute as, pdf path this will display the pdf automatically, there are some attributes with URL called toolbar=no&amp;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.</p> <p><br> </p> <p>Thanks.</p> 2010-09-02T15:17:09-04:00