Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to save a html as PDF using ItextSharp. I am getting this error:The number of columns in PdfPTable constructor must be greater than zero.
My Code Snippet:
C#
Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=ulcact.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            frmPrintHtml.RenderControl(hw);
            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);//error occrs in this line
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();
Posted
Updated 21-Mar-12 18:25pm
v3

I think your order of creation is wrong.

C#
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=ulcact.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
frmPrintHtml.RenderControl(hw);
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
StringReader sr = new StringReader(sw.ToString());
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
htmlparser.Parse(sr);//error should be gone
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
 
Share this answer
 
Comments
maajanes 22-Mar-12 1:42am    
No. Same error occuring.
[no name] 22-Mar-12 9:34am    
The code I posted works fine for me.
At this line..
htmlparser.Parse(sr);//error occrs in this line
..what is debugger telling you about sr?

Are you trying to pdf a gridview?
Er_Bhupendra 15-Feb-13 2:31am    
i was copied your code and implement it in my project, but still i am facing the error.
The number of columns in PdfPTable constructor must be greater than zero.
the below is my code.
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=ulcact.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
StringReader sr = new StringReader(sw.ToString());
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
htmlparser.Parse(sr);//error should be gone
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

Reply me as soon as possible,
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=ulcact.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
frmPrintHtml.RenderControl(hw);
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
StringReader sr = new StringReader(sw.ToString());
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
htmlparser.Parse(sr);//error should be gone
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();


If You use this coding than you must remove (tr td tag ) in that your coding and compress your row and column
 
Share this answer
 
v2
I think your html content have the table tag with no tr tag. Minimum one row required.
 
Share this answer
 
Comments
kavithakesavan 12-Aug-13 10:36am    
Hi,

I'm facing the same issue , when i use the above code And also i added static <tr> & <td> tags jus to make sure that there should be minimum one row in a table
I used this.Page.RenderControl(hw);

Error throws in parse statement

...This error driving me nuts ..
natarjan 16-Aug-13 4:45am    
Take the source code and debug it yourself or post your sample code to simulate this bug. will try to give the solution for this problem.

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