Java iText-图像中断单元格对齐

Java iText-图像中断单元格对齐,java,pdf,itext,Java,Pdf,Itext,我有一个表格,第一行只有图像,第二行只有图像的描述。我通过创建一个具有图像数量大小(列)的表,然后用大小为1的表(2行=第一行图像,第二行描述)填充单元格来处理这个问题。将第一行的单元格对齐设置为居中后,第二行将不应用对齐,说明保留在左侧。。。这是虫子吗 Integer size = filepathArray.length; PdfPTable pdfPTable = new PdfPTable(size); for (int i = 0; i < size;

我有一个表格,第一行只有图像,第二行只有图像的描述。我通过创建一个具有图像数量大小(列)的表,然后用大小为1的表(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);
Integer size=filepathArray.length;
PdfPTable PdfPTable=新PdfPTable(尺寸);
对于(int i=0;i
结果:图像居中,文本不居中,不可能,我什么都试过了!此外,addElement()删除所有以前设置的对齐方式(表和单元格元素,这是错误吗?),因此我必须在将内容添加到单元格或表后设置对齐方式。

这是错误的:

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