Apache POI Word XWPF table 方向和对齐方式

Apache POI Word XWPF table direction and alignment

我想创建一个法线 table 并设置它的方向 'right to left' “可以使用此选项设置 Table Direction", and to make it's alignment 'left to right' which can be set with this option Table Alignment

我试过这个:

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

据我所知,table 方向的部分阻止了任何对齐的发生。

Table 方向不会阻止对齐的发生,它会反转对齐的影响。因此,在您的情况下,如果将 table 方向设置为 Right-to-Left,则必须将 table 对齐方式设置为 Right 才能显示左对齐。所有具有左右可视化效果的 table 属性都是如此,例如,左侧边框现在将显示在右侧,反之亦然。

参见 ECMA-376 1st edition Part 4 部分 2.4.23 第 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).

ECMA-376 1st edition Part 4第2.4.1节第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]