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
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.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With