为什么我的 table 中缺少内容?

Why is content missing in my table?

这是我的场景:我创建了一个包含 6 列的 PdfPTable。但是,当我只向 table 添加 1 或 2 个单元格时,它不会渲染这些单元格。如果我添加 6 个或 6 个单元格的倍数,它只会呈现单元格。

我知道这完全有道理,但就我而言,我正在添加基于 imagelist 的图像单元格,它可能不包含 6 或 12 或 18 张图像。图像的数量可以是 1 或 7 或任何东西。以下是我的片段:

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();
}

任何帮助都会非常有用。

如文档所述,iText 会忽略不完整的每一行。当您定义一个包含 6 列的 table 并且您只添加 1 个(或 2 个或 3 个或 4 个或 5 个)单元格时,则不会呈现该行。

如果您想避免这种行为,您必须完成该行。这可以使用一行来完成:

table.completeRow();

现在 iText 将添加空单元格来完成该行。