PdfPTable构造函数中的列数必须大于零 [英] The number of columns in PdfPTable constructor must be greater than zero
本文介绍了PdfPTable构造函数中的列数必须大于零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 ItextSharp 将html保存为PDF。我收到此错误: PdfPTable构造函数中的列数必须大于零。
我的代码片段:
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:
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();
推荐答案
我认为你的创作顺序是错误的。
I think your order of creation is wrong.
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();
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);
文件pdfDoc =新文件(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); //错误应该消失
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
如果您使用此编码,则必须删除(tr td标签),因为您的编码和压缩行和列
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
I想想哟ur html内容的表标签没有tr标签。至少需要一行。
I think your html content have the table tag with no tr tag. Minimum one row required.
这篇关于PdfPTable构造函数中的列数必须大于零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文