Click here to Skip to main content
15,902,763 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
How to Export gridview to Pdf without using any third party tools in asp.net.
Thank U in advance

Edit -

I am getting this error for the following code.
C#
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 type or namespace name Document could not be found (are you missing a using directive or an assembly reference?)
Posted
Updated 1-Oct-11 2:25am
v2
Comments
RaisKazi 1-Oct-11 8:26am    
Moved Code and Error Detail of OP from Solution section to Question section.

 
Share this answer
 
 
Share this answer
 
C#
protected void Button4_Click(object sender, EventArgs e)
    {
        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();

       
    }
    public override void VerifyRenderingInServerForm(Control control)
    {

        /* Verifies that the control is rendered */

    }

kindly add last event or it will give an error
 
Share this answer
 
v2
 
Share this answer
 
Comments
[no name] 23-Jul-14 20:56pm    
Resurrecting ancient already answered posting in order to post links to your own website is spam.
 
Share this answer
 

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