Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi Experts,

I am trying to export the Gridview data into pdf format I am getting this error "Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'" how to solve this issue.

Here is the code I am using.

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

Kindly Share your thoughts.
Posted
Updated 8-Mar-13 0:47am
v8
Comments
kishore sharma 12-Mar-13 8:05am    
why dont you check with jquery datatable tools there you will have client side export to excel,export to pdf also, link is here try this
http://datatables.net/extras/tabletools/

There are two ways 1) get the result in to crystal report and export to PDF method 2) Use any third party control to export. there is no direct way
 
Share this answer
 
u have to make some changes in code..use following code
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);

     gvdetails.AllowPaging = false;
     gvdetails.DataBind();
     gvdetails.RenderBeginTag(hw);
     gvdetails.HeaderRow.RenderControl(hw);
     foreach (GridViewRow row in gvdetails.Rows)
     {
         row.RenderControl(hw);
     }
     gvdetails.FooterRow.RenderControl(hw);
     gvdetails.RenderEndTag(hw);
     gvdetails.Caption = "Your caption";
     gvdetails.HeaderRow.Style.Add("width", "15%");
     gvdetails.HeaderRow.Style.Add("font-size", "10px");
     gvdetails.Style.Add("text-decoration", "none");
     gvdetails.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
     gvdetails.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();

i have refer answer 5 from following link.
http://stackoverflow.com/questions/6343630/gridview-must-be-placed-inside-a-form-tag-with-runat-server-even-after-the-gri[^]

or in ur code page add following code block and use ur code as it is
C#
public override void VerifyRenderingInServerForm(Control control)
   {
       /* Verifies that the control is rendered */
   }
 
Share this answer
 
v3
Comments
krish2013 11-Mar-13 0:44am    
Hi Pallavi,
I am Using your's code But I am Getting Same Error.Please tell me how to resolve that issue.
Pallavi Waikar 11-Mar-13 1:51am    
check updated answer...to know more about VerifyRenderingInServerForm go through this link http://msdn.microsoft.com/en-us/library/system.web.ui.page.verifyrenderinginserverform.aspx...if u still facing problem then point me out by reply...because it is working for me
Try like this :

protected void imgExcel_Click(object sender, ImageClickEventArgs e)
{
gvEmp.AllowPaging = false;
gvEmp.DataBind();
Response.ClearContent();
Response.AddHeader("Content-disposition", string.Format("attachment;filename={0}", "Emp"));
Response.Charset = "";
Response.ContentType = "application/pdf";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvEmp.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}

public override void VerifyRenderingInServerForm(Control control)
{
// base.VerifyRenderingInServerForm(control);
}
 
Share this answer
 
Comments
krish2013 11-Mar-13 2:46am    
Hi,
It's working fine but my issue is not resolved.Please tell me how to solve this issue
@Krish,
If you have any special symbols in your gridview in that time only we will get error.
@Page Directive u can change some modifications like this


]]>

add EnableEventValidation="false" then only your problem will rectify.Try like this.Hopes it will work to you..


Thanks&Regards
Hari
 
Share this answer
 
StringReader sr = new StringReader(sw.ToString());

replace the code as


StreamReader sr=new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(sw.ToString())));
 
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