Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to export GridView data to PDF and I encountered with Document has no pages. I have 3 GridViews in a page and based on the visibility of the GridView I need to export the data to PDF. I searched on google and some articles on code project but none of them helped me. Please look into my code and help me with this problem. Below is the code :


C#
Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            GridView3.DataBind();
            GridView3.RenderControl(hw);
            GridView3.HeaderRow.Style.Add("width", "15%");
            GridView3.HeaderRow.Style.Add("font-size", "10px");
            GridView3.Style.Add("text-decoration", "none");
            GridView3.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            GridView3.Style.Add("font-size", "8px");
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();

Thank You
Posted

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