Support all possible alignment values for docx tables. (#848)

Besides the start/center/end values specified in the OOXML standard,
Word also uses "left" and "right" as values. We need to support this
to prevent POI code from crashing with such documents.

Fixes: https://bz.apache.org/bugzilla/show_bug.cgi?id=69744
This commit is contained in:
Jacobo Aragunde Pérez 2025-07-17 13:11:46 +02:00 committed by GitHub
parent f3bf31ac5d
commit cceccc4bae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 38 additions and 4 deletions

View File

@ -26,10 +26,12 @@ import java.util.Map;
* Sets alignment values allowed for Tables and Table Rows
*/
public enum TableRowAlign {
LEFT(STJcTable.INT_START),
LEFT(STJcTable.INT_LEFT),
START(STJcTable.INT_START),
CENTER(STJcTable.INT_CENTER),
RIGHT(STJcTable.INT_END);
RIGHT(STJcTable.INT_RIGHT),
END(STJcTable.INT_END);
private static final Map<Integer, TableRowAlign> imap;

View File

@ -44,6 +44,7 @@ import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlException;
import org.junit.jupiter.api.Test;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJcTable;
class TestXWPFBugs {
private static final POIDataSamples samples = POIDataSamples.getDocumentInstance();
@ -248,4 +249,29 @@ class TestXWPFBugs {
assertEquals(ParagraphAlignment.LEFT, leftParagraph.getAlignment()); // LEFT is the real alignment value.
}
}
@Test
public void testTableRightAlign() throws Exception {
// Document contains all possible values for table alignment, including null.
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("table-alignment.docx")) {
XWPFTable tbl0 = doc.getTableArray(0);
assertNull(tbl0.getTableAlignment());
assertFalse(tbl0.getCTTbl().getTblPr().isSetJc());
XWPFTable tbl1 = doc.getTableArray(1);
assertEquals(TableRowAlign.LEFT, tbl1.getTableAlignment());
assertEquals(STJcTable.LEFT, tbl1.getCTTbl().getTblPr().getJc().xgetVal().getEnumValue());
XWPFTable tbl2 = doc.getTableArray(2);
assertEquals(TableRowAlign.START, tbl2.getTableAlignment());
assertEquals(STJcTable.START, tbl2.getCTTbl().getTblPr().getJc().xgetVal().getEnumValue());
XWPFTable tbl3 = doc.getTableArray(3);
assertEquals(TableRowAlign.CENTER, tbl3.getTableAlignment());
assertEquals(STJcTable.CENTER, tbl3.getCTTbl().getTblPr().getJc().xgetVal().getEnumValue());
XWPFTable tbl4 = doc.getTableArray(4);
assertEquals(TableRowAlign.RIGHT, tbl4.getTableAlignment());
assertEquals(STJcTable.RIGHT, tbl4.getCTTbl().getTblPr().getJc().xgetVal().getEnumValue());
XWPFTable tbl5 = doc.getTableArray(5);
assertEquals(TableRowAlign.END, tbl5.getTableAlignment());
assertEquals(STJcTable.END, tbl5.getCTTbl().getTblPr().getJc().xgetVal().getEnumValue());
}
}
}

View File

@ -571,14 +571,20 @@ class TestXWPFTable {
void testSetGetTableAlignment() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
XWPFTable tbl = doc.createTable(1, 1);
tbl.setTableAlignment(TableRowAlign.START);
assertEquals(TableRowAlign.START, tbl.getTableAlignment());
assertEquals(STJcTable.INT_START, tbl.getTableAlignment().getValue());
tbl.setTableAlignment(TableRowAlign.LEFT);
assertEquals(TableRowAlign.LEFT, tbl.getTableAlignment());
assertEquals(STJcTable.INT_START, tbl.getTableAlignment().getValue());
assertEquals(STJcTable.INT_LEFT, tbl.getTableAlignment().getValue());
tbl.setTableAlignment(TableRowAlign.CENTER);
assertEquals(TableRowAlign.CENTER, tbl.getTableAlignment());
assertEquals(STJcTable.INT_CENTER, tbl.getTableAlignment().getValue());
tbl.setTableAlignment(TableRowAlign.RIGHT);
assertEquals(TableRowAlign.RIGHT, tbl.getTableAlignment());
assertEquals(STJcTable.INT_RIGHT, tbl.getTableAlignment().getValue());
tbl.setTableAlignment(TableRowAlign.END);
assertEquals(TableRowAlign.END, tbl.getTableAlignment());
assertEquals(STJcTable.INT_END, tbl.getTableAlignment().getValue());
tbl.removeTableAlignment();
assertNull(tbl.getTableAlignment());

Binary file not shown.