iText - 图像打破单元格对齐

iText - image breaks cell alignment

我有一个 table,其中第一行只有图像,第二行只有图像的描述。我通过创建一个具有图像数量大小(列)的 table 来处理这个问题,然后用大小为 1 的 tables 填充单元格(2 行 = 第一行图像,第二行描述).将第一行的单元格对齐方式设置为居中后,第二行不会应用对齐方式,描述停留在左侧...这是一个错误吗?

    Integer size = filepathArray.length;
    PdfPTable pdfPTable = new PdfPTable(size); 
    for (int i = 0; i < size; i++) {
        PdfPTable inner = new PdfPTable(1);
        try {
            PdfPCell image = new PdfPCell();
            PdfPCell description = new PdfPCell();
            PdfPCell cell = new PdfPCell();
            image.setImage(Image.getInstance(getImageAsByteArray(filepathArray[i])));
            image.setFixedHeight(32);
            image.setBorder(Rectangle.NO_BORDER);
            image.setHorizontalAlignment(Element.ALIGN_CENTER);
            inner.addCell(image);
            description.addElement(new Chunk(filepathArray[i], FontFactory.getFont("Arial", 8)));
            description.setBorder(Rectangle.NO_BORDER);
            description.setHorizontalAlignment(Element.ALIGN_CENTER);
            inner.addCell(description);
            cell = new PdfPCell();
            cell.addElement(inner); // needed to actually remove the border from the cell which contains the inner table because tables have no setter for the border
            cell.setBorder(Rectangle.NO_BORDER);
            pdfPTable.addCell(cell);
        } catch (Exception e) {
        }
    }
    pdfPTable.setHorizontalAlignment(Element.ALIGN_LEFT);

结果:图片居中,文字不居中,不行,我都试过了! addElement() 还删除了所有先前设置的对齐方式(table 和单元格元素,这是一个错误吗?)所以我必须在将内容添加到单元格或 table.[=11 之后设置对齐方式=]

这是错误的:

PdfPCell description = new PdfPCell();
description.addElement(new Chunk(filepathArray[i], FontFactory.getFont("Arial", 8)));
description.setHorizontalAlignment(Element.ALIGN_CENTER);

错了,因为你混合了文本模式:

PdfPCell description = new PdfPCell(new Phrase(filepathArray[i], FontFactory.getFont("Arial", 8)));
description.setHorizontalAlignment(Element.ALIGN_CENTER);

使用复合模式:

PdfPCell description = new PdfPCell();
Paragraph p = new Paragraph(filepathArray[i], FontFactory.getFont("Arial", 8));
p.setAlignment(Element.ALIGN_CENTER);
description.addElement(p);

似乎您已经尝试了所有方法,但没有使用文档中解释的方法 ;-)