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..a0154da 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 @@ -3378,15 +3398,13 @@ private XWPFTableRow makeTableRow( // record how many tables were in the cell previously int preTables = cell.getCTTc().getTblList().size(); - + CTTbl ctTbl = cell.getCTTc().addNewTbl(); ctTbl = cell.getCTTc().addNewTbl(); - CTTblPr tblPr = ctTbl.addNewTblPr(); - tblPr.addNewTblW(); - + XWPFTable nestedTable = new XWPFTable(ctTbl, cell); makeTable(nestedTable, cursor.getObject()); - + // for some reason this inserts two tables, where the // first one is empty. we need to remove that one. // luckily, the number of tables we used to have equals diff --git a/src/test/java/org/wordinator/xml2docx/TestDocxGenerator.java b/src/test/java/org/wordinator/xml2docx/TestDocxGenerator.java index 189d052..03b9c05 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,54 @@ 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); + } + + public void testNestedTableBorders() throws Exception { + XWPFDocument doc = convert("simplewp/simplewpml-issue-157-nested-tables.swpx", "out/nested-tables.docx"); + List contents = doc.getBodyElements(); + assertEquals(1, contents.size()); + + // outer table + Iterator it = contents.iterator(); + IBodyElement elem = it.next(); + assertEquals(BodyElementType.TABLE, elem.getElementType()); + + XWPFTable t = (XWPFTable) elem; + + // inner table + XWPFTableCell cell = t.getRows().get(0).getTableCells().get(0); + elem = cell.getBodyElements().get(0); + assertEquals(BodyElementType.TABLE, elem.getElementType()); + t = (XWPFTable) elem; + + // check the border properties + assertEquals(XWPFTable.XWPFBorderType.NONE, t.getLeftBorderType()); + assertEquals(XWPFTable.XWPFBorderType.NONE, t.getRightBorderType()); + assertEquals(XWPFTable.XWPFBorderType.NONE, t.getTopBorderType()); + assertEquals(XWPFTable.XWPFBorderType.NONE, t.getBottomBorderType()); + } + // ===== INTERNAL UTILITIES private XWPFDocument convert(String infile, String outfile) throws Exception { diff --git a/src/test/resources/simplewp/simplewpml-issue-157-nested-tables.swpx b/src/test/resources/simplewp/simplewpml-issue-157-nested-tables.swpx new file mode 100644 index 0000000..9219238 --- /dev/null +++ b/src/test/resources/simplewp/simplewpml-issue-157-nested-tables.swpx @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + Q [m + 3 + /h] + + + + + water flow + + + + + + + k + v + [m + 3 + /h] + + + + + flow coefficient of the valve      + + + + + + + Δp + v + [bar] + + + + + differential pressure across the valve + + + + + + + Δp + 1bar + [bar] + + + + + 1 bar differential pressure + + + + + + + + + + + + diff --git a/src/test/resources/simplewp/simplewpml-table-align.swpx b/src/test/resources/simplewp/simplewpml-table-align.swpx new file mode 100644 index 0000000..307f581 --- /dev/null +++ b/src/test/resources/simplewp/simplewpml-table-align.swpx @@ -0,0 +1,40 @@ + + + + + + + + + + Left-aligned + + + + + table + + + + + + + + + + + + + Centered + + + + + table + + + + + + +