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

47,880

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

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

Solution 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

Share:
47,880
Arun
Author by

Arun

Disclaimer: Opinions expressed are solely my own and do not express the views or opinions of my employer.

Updated on July 05, 2022

Comments

  • Arun
    Arun almost 2 years

    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

    • nfechner
      nfechner almost 13 years
      You should compile your code with the debug flag set. That will make it easier to find problems.
  • Arun
    Arun almost 13 years
    Thanks for your response. I used Aries solution, still its not working. What I guess is, since i have given PdfPTable headerTable=new PdfPTable(9); It doesnt have page-space to print rest of the heading.. same works if i change the value from 9 to 2.. so pls tell me how to reduce the size of the cell (if my guess is right)
  • Santosh Kathait
    Santosh Kathait over 10 years
    thanx for the answer Aries51 :)
  • Rup
    Rup almost 6 years
    Hi - thanks for trying to help. But this is the same point as the already accepted answer?
  • Nipun Sampath
    Nipun Sampath almost 6 years
    please improve your code formatting so it becomes easier to read.
  • mkl
    mkl over 5 years
    "the compiler" - you surely mean the document, not the compiler...