Click here to Skip to main content
16,011,680 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want gridview to pdf but have a error
this error:Line 608: pdfDoc.Close();
Exception Details: System.IO.IOException: The document has no pages.
i am use this code:


C#
GVAS.Visible = false;
       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);
       GVAS.AllowPaging = false;
       GVAS.DataBind();
       GVAS.RenderControl(hw);
       StringReader sr = new System.IO.StringReader(sw.ToString());
       Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 10f, 10f, 10f, 0f);
       HTMLWorker htmlparser = new iTextSharp.text.html.simpleparser.HTMLWorker(pdfDoc);
       PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
       pdfDoc.Open();
       htmlparser.Parse(sr);
       pdfDoc.Close();
       Response.Write(pdfDoc);
       Response.End();
Posted

Without itextsharp...........
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);//this is the error line
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();  
}
 
Share this answer
 
v2
by............ JAYDEEP SARKAR

ADD THESE NAMESPACE TO THE ASPX.CS FILE

using System.Text;
using System.IO;
using System.Threading;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

ADD THIS CODE FOR RENDER (it is used for RUNAT server tag error !)

public override void VerifyRenderingInServerForm(Control control)
{

}
at last use this code to save gridview as PDF at the button click event

protected void Button1_Click(object sender, EventArgs e)
{
*** use sql query ****

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Data.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();
}


Don't forget to put the sql query before this code under button click event.
 
Share this answer
 
Hi...
See this link, its may helpful to u.
http://www.aspdotnet-suresh.com/2011/04/how-to-export-gridview-data-to-pdf.html[^]
Thank u.
 
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