【发布时间】:2015-09-10 17:12:48
【问题描述】:
我有一张表,其中第一行只有图像,第二行只有图像的描述。我通过创建一个大小为图像数量(列)的表格来处理这个问题,然后用大小为 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);
结果:图片居中,文字不居中,没办法,我都试过了!此外 addElement() 会删除所有先前设置的对齐方式(表格和单元格元素,这是一个错误吗?)所以我必须在将内容添加到单元格或表格后设置对齐方式。
【问题讨论】: