Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/main/doctypes/simplewpml/simplewpml-base.rng
Original file line number Diff line number Diff line change
Expand Up @@ -1330,11 +1330,23 @@
</choice>
</attribute>
</optional>

<ref name="style-atts"/>
<optional>
<attribute name="align">
<a:documentation>
<p>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.</p>
</a:documentation>
<choice>
<value type="string">start</value>
<value type="string">center</value>
<value type="string">end</value>
</choice>
</attribute>
</optional>

<ref name="style-atts"/>
</element>
</define>

<define name="cols">
<element name="cols" ns="urn:ns:wordinator:simplewpml">
<a:documentation>
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/org/wordinator/xml2docx/generator/DocxGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/wordinator/xml2docx/TestDocxGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<IBodyElement> contents = doc.getBodyElements();
assertEquals(2, contents.size());

// first table
Iterator<IBodyElement> 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 {
Expand Down