mwieczorek
mwieczorek

Reputation: 2252

Itext7 - Nested Table not aligning within Cell

I'm trying to create a table with a nested table that's aligned to the right edge of its cell, but it stays to the left despite adding HorizontalAlignment to the containing Cell.

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

The grey shading is to show where the table and cells actually lie:

What is being produced:

enter image description here

What I'm after:

enter image description here

So basically, I'm asking if there's something I'm missing that would align that nested table correctly to the right.

Upvotes: 0

Views: 442

Answers (1)

Alexey Subach
Alexey Subach

Reputation: 12302

The fix is quite straightforward - you should apply horizontal alignment property to a child element that you are adding into a cell (in this case, to the table), not to the cell itself.

You can do it with the following line:

invoiceTable.setHorizontalAlignment(HorizontalAlignment.RIGHT);

Upvotes: 1

Related Questions