Getting IOException:The document has no pages in Itext Pdf

1.1k Views Asked by At

I am trying to create a Pdf for my application. I am creating a table as follows:-

    boolean isTableHeaderCreated = false;
    PdfPTable playerTable = null;

    Set<String> keySet = playersListMap.keySet();
    for(String key:keySet) {
        List<String> playerNames = playersListMap.get(key);
        if(!isTableHeaderCreated) {
            playerTable = createPlayerTableHeaderColumns(playerNames.size()+1);
            isTableHeaderCreated = true;
        }
    }
    document.add(playerTable);
    document.close();


 private PdfPTable createPlayerTableHeaderColumns(int size) {
    // TODO Auto-generated method stub
    PdfPTable playerTable = new PdfPTable(size + 1);
    playerTable.setWidthPercentage(100);
    for(int i=0;i<=size;i++) {
        playerTable.addCell("Player "+i);
    }
    playerTable.setHeaderRows(1);
    return playerTable;
}

I have debug the code and verified that createPlayerTableHeaderColumns method is getting executed for Size = 5 and even i am adding the table in Document but still getting the below exception:

java.io.IOException: The document has no pages.
at com.itextpdf.text.pdf.PdfPages.writePageTree(PdfPages.java:113)
at com.itextpdf.text.pdf.PdfWriter.close(PdfWriter.java:1174)

I know this exception occurs when there is nothing to write in pdf but here i am writing the table in document then why i am getting this exception.

Can anybody help me on this.

Thanks

0

There are 0 best solutions below