diff --git a/src/main/doctypes/simplewpml/simplewpml-base.rng b/src/main/doctypes/simplewpml/simplewpml-base.rng index 7d70bee..712edf2 100644 --- a/src/main/doctypes/simplewpml/simplewpml-base.rng +++ b/src/main/doctypes/simplewpml/simplewpml-base.rng @@ -1330,11 +1330,23 @@ - - + + + +

Specifies the alignment of the table with respect to the text margins, but not the text inside the table. The default is implementation-dependent, but 'start' is typical.

+
+ + start + center + end + +
+
+ + - + diff --git a/src/main/java/org/wordinator/xml2docx/generator/DocxGenerator.java b/src/main/java/org/wordinator/xml2docx/generator/DocxGenerator.java index 9fa85c1..4326f19 100644 --- a/src/main/java/org/wordinator/xml2docx/generator/DocxGenerator.java +++ b/src/main/java/org/wordinator/xml2docx/generator/DocxGenerator.java @@ -45,6 +45,7 @@ import org.apache.poi.xwpf.usermodel.BreakType; import org.apache.poi.xwpf.usermodel.IBodyElement; import org.apache.poi.xwpf.usermodel.ParagraphAlignment; +import org.apache.poi.xwpf.usermodel.TableRowAlign; import org.apache.poi.xwpf.usermodel.UnderlinePatterns; import org.apache.poi.xwpf.usermodel.XWPFAbstractFootnoteEndnote; import org.apache.poi.xwpf.usermodel.XWPFAbstractNum; @@ -2707,8 +2708,7 @@ private void makeTable(XWPFTable table, XmlObject xml) throws DocxGenerationExce setTableIndents(table, cursor); setTableLayout(table, cursor); - - + setTableAlign(table, cursor); String styleName = cursor.getAttributeText(DocxConstants.QNAME_STYLE_ATT); String styleId = cursor.getAttributeText(DocxConstants.QNAME_STYLEID_ATT); @@ -2854,6 +2854,26 @@ private void setTableIndents(XWPFTable table, XmlCursor cursor) { } + private void setTableAlign(XWPFTable table, XmlCursor cursor) throws DocxGenerationException { + String align = cursor.getAttributeText(DocxConstants.QNAME_ALIGN_ATT); + if (align == null) { + return; + } + + TableRowAlign alignObj; + if (align.equals("start")) { + alignObj = TableRowAlign.LEFT; + } else if (align.equals("center")) { + alignObj = TableRowAlign.CENTER; + } else if (align.equals("end")) { + alignObj = TableRowAlign.RIGHT; + } else { + throw new DocxGenerationException("Unknown value for table align: '" + + align + "'"); + } + + table.setTableAlignment(alignObj); + } /** * Sets the w:tblLayout to fixed or auto diff --git a/src/test/java/org/wordinator/xml2docx/TestDocxGenerator.java b/src/test/java/org/wordinator/xml2docx/TestDocxGenerator.java index 189d052..1a61ec2 100644 --- a/src/test/java/org/wordinator/xml2docx/TestDocxGenerator.java +++ b/src/test/java/org/wordinator/xml2docx/TestDocxGenerator.java @@ -17,6 +17,7 @@ import org.apache.poi.xwpf.usermodel.BodyElementType; import org.apache.poi.xwpf.usermodel.IBodyElement; import org.apache.poi.xwpf.usermodel.IRunElement; +import org.apache.poi.xwpf.usermodel.TableRowAlign; import org.apache.poi.xwpf.usermodel.XWPFAbstractNum; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFFooter; @@ -862,6 +863,29 @@ public void testMultiSectionPageProps() throws Exception { assertEquals(BigInteger.valueOf(16838), pageSz.getH()); } + public void testTableAlign() throws Exception { + XWPFDocument doc = convert("simplewp/simplewpml-table-align.swpx", "out/table-align.docx"); + List contents = doc.getBodyElements(); + assertEquals(2, contents.size()); + + // first table + Iterator it = contents.iterator(); + IBodyElement elem = it.next(); + assertEquals(BodyElementType.TABLE, elem.getElementType()); + + XWPFTable t = (XWPFTable) elem; + TableRowAlign align = t.getTableAlignment(); + assertEquals(TableRowAlign.LEFT, align); + + // second table + elem = it.next(); + assertEquals(BodyElementType.TABLE, elem.getElementType()); + + t = (XWPFTable) elem; + align = t.getTableAlignment(); + assertEquals(TableRowAlign.CENTER, align); + } + // ===== INTERNAL UTILITIES private XWPFDocument convert(String infile, String outfile) throws Exception {