Click here to Skip to main content
16,015,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have URL if i open in IE popup window is displaying to download pdf file. How to programatically download file and save the pdf file in my local folder in c# ASP.NET
Thanks in advance.







Regards
Ganesh.
Posted

Hi...
See this one,may its useful to u.
In aspx:
XML
<div>
        <asp:linkbutton id="LinkButtonDownloadPdf" runat="server" text="Download PDF" style="color: Navy;
            font-weight: bold;" onclick="LinkButtonDownloadPdf_Click" />
</div>

In aspx.cs:
C#
protected void LinkButtonDownloadPdf_Click(object sender, EventArgs e)
{
        Response.ContentType = "Application/pdf";
        Response.AppendHeader("Content-Disposition", "attachment; filename=Test_PDF.pdf");
        Response.TransmitFile(Server.MapPath("~/Files/Test_PDF.pdf"));
        Response.End();
}

Thank u.
 
Share this answer
 
Pdf can be downloaded in two ways in asp.net they are:

*) Using Script.
*) Using third party pdf creation dll files (iTextSharp).

1) Lets us first see pdf download using scripting
Step 1: Write the following script in ur .aspx page.

HTML
<script type="text/javascript">
       function PrintGridData() {
           var prtGrid = document.getElementById('<%=GridView1.ClientID %>');
           prtGrid.border = 0;
           var prtwin = window.open('', 'PrintGridViewData', 'left=100,top=100,width=1000,height=1000,tollbar=0,scrollbars=1,status=0,resizable=1');
           prtwin.document.write(prtGrid.outerHTML);
           prtwin.document.close();
           prtwin.focus();
           prtwin.print();
           prtwin.close();
       }
   </script>


Step 2: Now add a simple button to call the above function.

HTML
<input type="button" id="btnPrint" value="Convert To PDF"  önclick="PrintGridData()" />


2) Now Lets download pdf using Third party dll

Step 1: Download the iTextSharp dll from the following website
[iTextSharp pdf dll Download]

Step 2: Extract the dll and add it to your project by following these simple steps
right click on references (Solution Explorer)
-> add reference
-> navigate to where the dll is extracted
-> Select the dll and click ok.

Step 3: Now add a button to your aspx page as follows:
ASP.NET
<asp:button id="pdf" runat="server" text="Convert to PDF" onclick="pdf_Click" xmlns:asp="#unknown" />


Step 4: Now in c# code add the following namespaces

C#
using System;
using System.IO;
using System.Web;
using System.Web.UI;
using iTextSharp.text;
using iTextSharp.text.pdf;
using MySql.Data.MySqlClient;
using System.Web.UI.WebControls;
using iTextSharp.text.html.simpleparser;


step 5: Now write the code for button click(pdf_Click) as follows
C#
protected void pdf_Click(object sender, EventArgs e)
        {
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", "PDF.pdf"));
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            StringWriter writer = new StringWriter();
            HtmlTextWriter textwriter = new HtmlTextWriter(writer);
            this.Page.RenderControl(textwriter);

            StringReader reader = new StringReader(writer.ToString());
            Document pdfdoc = new Document(PageSize.A4, 20f, 20f, 20f, 20f);
            //pdfdoc.AddHeader("Headers","User Password Type");
            HTMLWorker worker = new HTMLWorker(pdfdoc);
            PdfWriter.GetInstance(pdfdoc, Response.OutputStream);
            pdfdoc.Open();
            worker.Parse(reader);
            pdfdoc.Close();

            Response.Write(pdfdoc);
            Response.End();
        }

Happy coding.
 
Share this answer
 
v2
Comments
ErBhati 28-Jan-14 0:56am    
I have used this code but at line

worker.Parse(reader);

i find error "Invalid URI: The hostname could not be parsed."

pls help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900