【发布时间】:2012-08-22 16:34:56
【问题描述】:
我正在使用 itextsharp 将我的 asp.net 页面转换为 pdf。在按钮上单击我正在编写以下代码:
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);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
但我收到错误:“文档没有页面” 请帮我摆脱这个错误并生成pdf。
【问题讨论】:
-
你能创建一个模板pdf文件吗?
-
在此链接上有一个解决方案:velocityreviews.com/forums/… 但是页面内容以 pdf 的纯格式转换。有没有办法保持 pdf 中的原始标记(颜色等)?
-
检查 sw.ToString() 是否为 string.Empty。
-
@ketan 我认为这个问题需要一个很好理解的答案 - 它首先需要额外的细节,因为 OP 已经有半年没来了,所以这些细节很可能不会出现。
标签: asp.net pdf itextsharp