The bad news: in general, it's not possible to get the real width of the table until it has been placed. It highly depends on the layout area.
The good news: one can simulate table's layout and check the occupied place:
Rectangle area = new Rectangle(0, 0, 500, 500);
LayoutResult result = table.createRendererSubTree().setParent(doc.getRenderer()).layout(new LayoutContext(new LayoutArea(0, area)));
Rectangle tableArea = result.getOccupiedArea().getBBox();
In the code above, I've created a renderer for the table (createRendererSubTree()
), set its parent (setParent(doc.getRenderer())
- this line is important for correct properties processing) and layouted it (layout(new LayoutContext(new LayoutArea(0, area)))
;
UPD: I've read your initial question and have another solution for you.
Suppose that you don't have any area to place, but want to see how much space the table could occupy.
Use the next snippet then:
MinMaxWidth minMaxWidth = ((TableRenderer)table.createRendererSubTree().setParent(doc.getRenderer())).getMinMaxWidth();
float minWidth = minMaxWidth.getMinWidth();
float maxWidth = minMaxWidth.getMaxWidth();
In the code above I've calculated the min and the max width of the table.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…