Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

itext

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

like image 207
Arun Avatar asked Jul 25 '11 12:07

Arun


3 Answers

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.

like image 149
Aries51 Avatar answered Oct 19 '22 23:10

Aries51


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).

like image 20
Bernd Elkemann Avatar answered Oct 20 '22 00:10

Bernd Elkemann


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

like image 3
Malala Avatar answered Oct 19 '22 23:10

Malala