How to convert gridview to pdf in asp.net

Asked By dheeraj srivastava
12-Jan-10 08:11 AM
Earn up to 0 extra points for answering this tough question.

Its not working for me.

I tried this code.

Response.ContentType = "application/pdf";
        Response.Charset = "";
        this.EnableViewState = false;
        System.IO.StringWriter tw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
        grdtariff.RenderControl(hw);
        //Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=test.pdf");
        //Response.ContentType = "application/pdf";
        Response.Write(tw.ToString());
        Response.End();

Pl provide me the working solution.

display error :adobe reader could not open 'test[1].pdf' because it is either not supported file type or because the file has been damaged

 

Thanks,

  GridView to PDF

F Cali replied to dheeraj srivastava
12-Jan-10 08:48 AM

Here's a similar code from this link:

http://www.aspsnippets.com/Articles/Export-GridView-with-Images-from-database-to-Word-Excel-and-PDF-Formats.aspx

private void PDF_Export()

{

    Response.ContentType = "application/pdf";

    Response.AddHeader("content-disposition",

        "attachment;filename=GridViewExport.pdf");

    Response.Cache.SetCacheability(HttpCacheability.NoCache);

    StringWriter sw = new StringWriter();

    HtmlTextWriter hw = new HtmlTextWriter(sw);

    GridView1.AllowPaging = false;

    GridView1.DataBind();

    GridView1.RenderControl(hw);

    StringReader sr = new StringReader(sw.ToString());

    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);

    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

    pdfDoc.Open();

    htmlparser.Parse(sr);

    pdfDoc.Close();

    Response.Write(pdfDoc);

    Response.End(); 

}

 

The code is similar to yours.  What is needed is the iTextSharp library.

Also, the code you have will work for Excel (after making some changes) but I don't think it will work for PDF.

Regards,
SQL Server Helper

  re - convert gridview to pdf in asp.net

Lalit M. replied to dheeraj srivastava
12-Jan-10 12:04 PM
i think problem in pdf software
please re-install or repair you pdf (acrobat reader/writer)

for information
 
Create New Account