【问题标题】:ItextSharp Error : Document has no pagesItextSharp 错误:文档没有页面
【发布时间】: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


【解决方案1】:

为此需要采取两个步骤:

1) 检查 ITextSharp dll 是否为最新版本

2) 使用链接按钮而不是按钮,不要使用超链接,因为它会导致超链接按钮出错。

【讨论】:

    猜你喜欢
    • 2014-07-27
    • 2014-07-18
    • 2015-03-18
    • 2012-04-03
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    相关资源
    最近更新 更多