Why is content missing in my table?

Category: 
Tags: tablesmissing contentcomplete rowiText 5

Here's my scenario: I create a PdfPTable with 6 columns. However, when I add only 1 or 2 cells to the table, it's not rendering those cells. It only renders the cells if I add 6 or a multiple of 6 cells.

I know that makes total sense but in my case, I am adding image cells based on an imagelist that may not contain 6 or 12 or 18 images. The number of images can be 1 or 7 or anything. Below is my Q> snippet:

try {
    Document document = new Document(PageSize.A4.rotate());
    PdfWriter writer = PdfWriter.getInstance(document,
        new FileOutputStream(new File(sourcePath, AppText.FILE_NAME)));
    PdfPTable table = new PdfPTable(6);
    document.open();
    Paragraph paragraph = new Paragraph("Diary Report");
    paragraph.setSpacingAfter(50);
    paragraph.setSpacingBefore(50);
    document.add(paragraph);
    table.setHorizontalAlignment(Element.ALIGN_LEFT);
    for (String imageFile : imageFiles) {
        Image image = Image.getInstance(new File(imageFile).getAbsolutePath());
        PdfPCell cell = new PdfPCell(image, true);
        cell.setPadding(10);
        table.addCell(cell);
    }
    document.add(table);
    document.close();
    return "successful";
} catch (DocumentException | IOException e) {
    e.printStackTrace();
}
Any help would be so great.

Posted on StackOverflow on Nov 20, 2015 by prashantwosti

iText ignores every row that isn't complete. When you define a table with 6 columns and you only add 1 (or 2 or 3 or 4 or 5) cell(s), then the row isn't rendered.

If you want to avoid this behavior, you have to complete the row. This can be done using a single line:

table.completeRow();

Now iText will add empty cells to complete the row.