Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ExceptionConverter:java.io.IOException:文档没有页面。我正在使用iText_Java_Itext - Fatal编程技术网

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

ExceptionConverter:java.io.IOException:文档没有页面。我正在使用iText,java,itext,Java,Itext,当我执行下面的代码时 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 Para

当我执行下面的代码时

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)
请帮助朋友。提前感谢

尝试更换

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


除此之外,我看不出有什么问题……

我想Aries51的解决方案对您有效。另外一个注意事项:您似乎根本没有捕捉到您的异常。围绕main方法(或抛出main方法)中的所有内容进行大量尝试捕获并不是使用异常的方式。例如,您应该围绕Aries51关于
PdfWriter.getInstance(文档,新文件输出流(“c:/sample.pdf”))的建议编写一个try catchc:/…
示例字符串。异常应该告诉您该文件是否可写或是否存在(用户可以输入伪文件)。

好的,所以我为您试用了它。我之前的回答是不正确的,声明文件优先也有效。我认为你的表格申报是错误的。将其设置为9列,但只填充其中5列。如果您将标题栏的大小更改为5,则应该可以解决此问题。

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

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

我把文件“watermark.png”和那个可执行的jar文件放在一起,现在错误被解决了


检查是否所有文件都与jar文件在一起。

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

表声明错误

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


您声明了10列,因此应该使用10列

如果编译器没有获得任何有意义的信息写入文件,则会出现此错误。我建议尝试在
open()


它应该可以工作

您应该使用调试标志集编译代码。这将使您更容易发现问题。感谢您的回复。我使用了白羊座的解决方案,但仍然不起作用。我想,既然我已经给出了PdfPTable headerTable=newpdfptable(9);它没有页面空间来打印标题的其余部分。。如果我将值从9更改为2,同样有效。。因此,请告诉我如何缩小电池的大小(如果我的猜测是正确的)thanx的答案是:(51:)嗨-谢谢你的帮助。但这与已经被接受的答案是一样的?请改进您的代码格式,使其更易于阅读。“编译器”-您肯定是指文档,而不是编译器。。。
PdfWriter.getInstance(document, new FileOutputStream("c:/sample.pdf"));
document.add(new Chunk(""));