ExceptionConverter: java.io.IOException: The document has no pages. am using iText

孤街浪徒 提交于 2019-12-22 05:55:20

问题


when i execute the below code

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();

I get below exception. please help

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)

PLEASE HELP FRIENDS. THANKS IN ADVANCE


回答1:


Okay so I tried it out for you. My previous answer was incorrect, declaring the file first works as well. I think that your table declaration is wrong. You set it to 9 columns, but you only fill 5 of them. If you would change your columnssize of the headerTable to 5 that should fix it.




回答2:


I guess Aries51's solution worked for you. One additional note: you dont seem to catch your exceptions at all. A big try-catch around everything in your main-method (or a throwing main-method) is not the way to use exceptions. For example you should wrap a try-catch around Aries51's suggestion of PdfWriter.getInstance(document, new FileOutputStream("c:/sample.pdf")); because at some point you will replace the static c:/... sample string with a string the user enters at runtime. An exception should tell you if that file is writable or if it exists at all (user can enter bogus).




回答3:


You get this error when the compiler does not get any meaningful information to write to your file. I suggest trying to add this line after open()

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

It should work




回答4:


Try replacing

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

by

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

I don't see anything wrong besides that...




回答5:


I was facing the same problem . My pdf was created fine in netbeans but the executable jar was not doing so.

Finally debugging my codes I found that the file "watermark.png" which i was using to add in my pdf was not put together the "distbuild.jar" .

I put the file "watermark.png" with that executable jar file and now the error is resolved.

Check if you are also having all your files with the jar file.




回答6:


I received this error when accidentally trying to close the PDF twice. Simple solution to eliminate one of the calls.




回答7:


table declaration wrong.

PdfPTable patTable = AgsiPdfPUtil.getTable(10);//declared table

you declared 10 columns so you should use 10 columns



来源:https://stackoverflow.com/questions/6816195/exceptionconverter-java-io-ioexception-the-document-has-no-pages-am-using-ite

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!