首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ExceptionConverter: java.io.IOException:文档没有页面。我正在使用iText

ExceptionConverter: java.io.IOException:文档没有页面。我正在使用iText
EN

Stack Overflow用户
提问于 2011-07-25 20:34:54
回答 6查看 46.4K关注 0票数 6

当我执行以下代码时

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)

请帮助朋友们。提前感谢

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-07-26 20:06:49

好的,我为你试过了。我之前的回答是错误的,声明文件优先也是有效的。我认为您的表声明是错误的。您将其设置为9列,但只填充了其中的5列。如果您想将headerTable的列大小更改为5,应该可以解决这个问题。

票数 13
EN

Stack Overflow用户

发布于 2011-07-25 22:10:10

我猜Aries51的解决方案对你有效。另请注意:您似乎根本没有捕捉到您的异常。围绕main-method (或抛出main-method)中的所有内容进行大的try-catch不是使用异常的方式。例如,您应该在Aries51的PdfWriter.getInstance(document, new FileOutputStream("c:/sample.pdf"));建议中包含一个try-catch,因为在某些情况下,您将用用户在运行时输入的字符串替换静态c:/...示例字符串。一个异常应该告诉你该文件是否可写,或者它是否存在(用户可以输入bogus)。

票数 2
EN

Stack Overflow用户

发布于 2018-07-29 04:20:13

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

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

它应该是可行的

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6816195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档