【问题标题】:The document has no pages文档没有页面
【发布时间】:2011-03-14 11:22:19
【问题描述】:

我正在使用下面的代码将 gridview 导出为 PDF

form1.Controls.Clear();
form1.Controls.Add(GridView1);
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
string html = "&lt;html><body>" + sw.ToString() + "&lt;/body></html>";
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
Document document = new Document(PageSize.A4, 80, 50, 30, 65);
PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
document.AddAuthor("Ram");
document.AddSubject("Export To pdf");
document.Open();
string tempFile = Path.GetTempFileName();
using (StreamWriter tempwriter = new StreamWriter(tempFile, false))
{
    tempwriter.Write(html);
}
HtmlParser.Parse(document, tempFile);
document.Close();
writer.Close();
File.Delete(tempFile);
writer = null;
document = null;
Response.End();

我通过放置断点检查了 grridview 有 10 行。但我收到错误

document.Close();

那个

文档没有页面。

有什么建议可以解决吗?

【问题讨论】:

  • 那么问题出在哪里?

标签: asp.net gridview itextsharp


【解决方案1】:

1) 设置断点以查看您的网格视图有 10 行会有所帮助,但只能检查部分问题。您还需要检查tempFile 的内容。这就是 iText 实际使用的。如果它是空的,你会得到“doc has no pages”的异常。

2.1) HtmlParser 不再存在于 iText 中。话虽如此,我只是通过谷歌挖掘了这个代码示例:

public static void main(String[] args) throws Exception {
  Document document = new Document();
  PdfWriter.getInstance(document, new FileOutputStream("html1.pdf"));
  HtmlParser.parse(document, "example.html");
}

没有打开或关闭,只是调用 HtmlParser。 HtmlParser 很有可能会检查文档是否已经打开,如果是则不会继续...这可以解释您所看到的行为。

2.2) 现在转换 HTML 的“正确”方法是这样的:

String html = readHtml();
List<Element> objects = HTMLWorker.parseToList(new StringReader(html), null);

for (Element element : objects)
  document.add(element);

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,下面的建议帮助我的问题解决了

    文档没有页面意味着您的 GridView 数据在您导出时丢失。 因此,在导出按钮事件中,将 GridView 与数据库中的数据重新绑定

    https://www.aspforums.net/Threads/264988/Export-GridView-to-PDF-Error-Document-has-no-pages/

    【讨论】:

      猜你喜欢
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多