Search code examples
javaapache-poixwpf

Apache POI Word XWPF table direction and alignment


I want to create a normal table and to set it's direction 'right to left' "which can be set with this option Table Direction", and to make it's alignment 'left to right' which can be set with this option Table Alignment

I have tried this:

XWPFTable myTable = myDocument.createTable();
CTTbl cttblp = myTable.getCTTbl();
CTTblPr cttblpr;
cttblpr = (cttblp.getTblPr() == null ? cttblp.addNewTblPr() : cttblp.getTblPr());

//table direction
cttblpr.addNewBidiVisual().setVal(STOnOff.ON);

//table alignment
CTJc ctjc = (cttblpr.isSetJc() ? cttblpr.getJc() : cttblpr.addNewJc());
ctjc.setVal(STJc.LEFT);

What I have understood that the part of table direction prevents any alignment to take place.


Solution

  • Table direction does not prevent alignment from taking place, it reverses the affect of alignment. So in your case if you set table direction to Right-to-Left, then you have to set the table alignment to Right in order for it to display left aligned. This is true of all table attributes with a left and right visualization, for example left borders will now be displayed on the right, and vice versa.

    See ECMA-376 1st edition Part 4 section 2.4.23 Line 16

    If this property [jc (Table Alignment)] is omitted on a table, then the justification shall be determined by the associated table style. If this property is not specified in the style hierarchy, then the table shall be left justified with zero indentation from the leading margin (the left margin in a left-to-right table or the right margin in a right-to-left table).

    And ECMA-376 1st edition Part 4 section 2.4.1 Line 7

    When this property [bidiVisual (Visually Right to Left Table)] is specified, then the ordering of all cells (and table-level properties) in this table shall be applied to the table assuming that the table is a normal left to right table, but the table cells shall be displayed in a right to left direction. [Example: A left border on the first table cell shall be displayed on the right side of that cell (which would be the rightmost cell) in a visually right to left table. end example]