itext7-单元格内的嵌套表未对齐

时间:2019-08-10 02:55:33

标签: java itext7

我正在尝试创建一个表,该表的嵌套表与其单元格的右边缘对齐,但是即使将HorizontalAlignment添加到包含其单元格的单元格中,它也保持在左侧。

Table billingTable = new Table(2).useAllAvailableWidth();
Cell billedPartyCell = new Cell().setBorder(Border.NO_BORDER).setWidth(UnitValue.createPercentValue(50));
Cell billingInfoCell = new Cell().setBorder(Border.NO_BORDER).setWidth(UnitValue.createPercentValue(50))
        .setHorizontalAlignment(HorizontalAlignment.RIGHT); // <- not having any effect

// invoice table is the smaller (darker grey) table inside billingCell
Table invoiceTable = new Table(2).setBorder(Border.NO_BORDER).setBackgroundColor(COLOR_MID_GREY);

// redacted for brevity

billingInfoCell.setBackgroundColor(COLOR_LT_GREY);
billingInfoCell.add(invoiceTable);

billingTable.setMarginTop(SPACING_MARGIN);
billingTable.addCell(billedPartyCell).addCell(billingInfoCell);

灰色阴影用于显示表格和单元格的实际位置:

正在产生什么: enter image description here

我追求的是: enter image description here

因此,基本上,我要问的是我是否缺少某些东西,这些东西会使嵌套表正确地向右对齐。谢谢大家。

1 个答案:

答案 0 :(得分:1)

此修复非常简单-您应将水平对齐方式属性应用于要添加到单元格中的子元素(在这种情况下,应用于表格),而不是单元格本身。

您可以使用以下行来完成此操作:

invoiceTable.setHorizontalAlignment(HorizontalAlignment.RIGHT);