【问题标题】:ExceptionConverter: java.io.IOException: The document has no pages. am using iTextExceptionConverter: java.io.IOException: 文档没有页面。我正在使用 iText
【发布时间】:2011-07-25 12:34:53
【问题描述】:

当我执行下面的代码时

File f = new File("c:/sample.pdf");
PdfWriter.getInstance(document, new FileOutputStream(f));
document.open();
System.out.println("opening the document..");
PdfPTable headerTable=new PdfPTable(9);
PdfPCell cellValue = new PdfPCell(new Paragraph("Header 1"));
cellValue.setColspan(1);
headerTable.addCell(cellValue);
cellValue = new PdfPCell(new Paragraph("Header 2"));
headerTable.addCell(cellValue);
cellValue = new PdfPCell(new Paragraph("Header 3"));
headerTable.addCell(cellValue);
cellValue = new PdfPCell(new Paragraph("Header 4"));
headerTable.addCell(cellValue);

PdfPTable subHeaderTable = new PdfPTable(3);
PdfPCell subHeadingCell = new PdfPCell(new Paragraph("Header 5"));
subHeadingCell.setColspan(3);
subHeaderTable.addCell(subHeadingCell);
subHeaderTable.addCell("Sub heading 1");
subHeaderTable.addCell("Sub heading 2"); 
subHeaderTable.addCell("Sub heading 3");

headerTable.addCell(subHeaderTable);

document.add(headerTable);
document.close();

我得到以下异常。请帮忙

ExceptionConverter: java.io.IOException: The document has no pages.
    at com.lowagie.text.pdf.PdfPages.writePageTree(Unknown Source)
    at com.lowagie.text.pdf.PdfWriter.close(Unknown Source)
    at com.lowagie.text.pdf.PdfDocument.close(Unknown Source)
    at com.lowagie.text.Document.close(Unknown Source)

请帮助朋友。提前致谢

【问题讨论】:

  • 你应该编译你的代码并设置调试标志。这样会更容易发现问题。

标签: java itext


【解决方案1】:

好的,我为你试了一下。我之前的回答不正确,首先声明文件也有效。我认为您的表格声明是错误的。您将其设置为 9 列,但您只填充了其中的 5 列。如果您将 headerTable 的列大小更改为 5,应该可以修复它。

【讨论】:

  • 感谢 Aries51 的回答 :)
【解决方案2】:

我猜 Aries51 的解决方案对你有用。另外一点:您似乎根本没有发现异常。围绕主方法(或抛出主方法)中的所有内容进行大尝试不是使用异常的方法。例如,您应该围绕 Aries51 的 PdfWriter.getInstance(document, new FileOutputStream("c:/sample.pdf")); 建议包装一个 try-catch,因为在某些时候您将用用户在运行时输入的字符串替换静态的 c:/... 示例字符串。一个异常应该告诉你该文件是可写的还是它是否存在(用户可以输入假的)。

【讨论】:

  • 感谢您的回复。我使用了 Aries 解决方案,仍然无法正常工作。我猜是,因为我给了 PdfPTable headerTable=new PdfPTable(9);它没有页面空间来打印标题的其余部分。如果我将值从 9 更改为 2,同样有效。所以请告诉我如何减小单元格的大小(如果我的猜测是正确的)
【解决方案3】:

当编译器没有获得任何有意义的信息写入您的文件时,您会收到此错误。我建议尝试在open()之后添加这一行

document.add(new Chunk(""));

它应该可以工作

【讨论】:

  • 请改进您的代码格式,使其更易于阅读。
  • "the compiler" - 你肯定是指 document,而不是 compiler...
【解决方案4】:

尝试替换

File f = new File("c:/sample.pdf");
PdfWriter.getInstance(document, new FileOutputStream(f));

通过

PdfWriter.getInstance(document, new FileOutputStream("c:/sample.pdf"));

除此之外我看不出有什么不妥……

【讨论】:

    【解决方案5】:

    我也遇到了同样的问题。 我的 pdf 在 netbeans 中创建得很好,但可执行 jar 没有这样做。

    最后调试我的代码我发现我用来添加到我的pdf中的文件“watermark.png”没有放在一起“distbuild.jar”。

    我将文件“watermark.png”与该可执行 jar 文件一起放入,现在错误已解决。

    检查您是否也将所有文件都包含在 jar 文件中。

    【讨论】:

      【解决方案6】:

      我不小心尝试关闭 PDF 两次时收到此错误。消除其中一个调用的简单解决方案。

      【讨论】:

        【解决方案7】:

        表声明错误。

        PdfPTable patTable = AgsiPdfPUtil.getTable(10);//声明表

        你声明了 10 列,所以你应该使用 10 列

        【讨论】:

        • 嗨 - 感谢您的帮助。但这与已经接受的答案相同吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多