当我执行以下代码时
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)
请帮助朋友们。提前感谢
发布于 2011-07-26 20:06:49
好的,我为你试过了。我之前的回答是错误的,声明文件优先也是有效的。我认为您的表声明是错误的。您将其设置为9列,但只填充了其中的5列。如果您想将headerTable的列大小更改为5,应该可以解决这个问题。
发布于 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)。
发布于 2018-07-29 04:20:13
当编译器没有获得任何有意义的信息来写入您的文件时,您会得到此错误。我建议尝试在open()
之后添加这一行
document.add(new Chunk(""));
它应该是可行的
https://stackoverflow.com/questions/6816195
复制相似问题