I want to insert both a table and some text at the same level in a XWPFDocument using Apache POI, meaning that the table will be on the left side and the text on it'r right side, just like in this template:
Using a solution providing a cursor found around, does not seems to make it work. This is the approach commonly used:
XWPFTable table = document.createTable();
XWPFTableRow tableRowOne = table.getRow(0);
tableRowOne.getCell(0).setText("col one");
tableRowOne.addNewTableCell().setText("col two");
tableRowOne.addNewTableCell().setText("col three");
XmlCursor cursor = table.getCTTbl().newCursor();
cursor.toEndToken();
while(cursor.toNextToken() != org.apache.xmlbeans.XmlCursor.TokenType.START);
XWPFParagraph paragraph2 = document.insertNewParagraph(cursor);
XWPFRun run = paragraph2.createRun();
run.setText("Abstract: ");
The result is this:
Do you have any idea there is a way to make it work? Thanks.


The
XmlCursoris the wrong way here. AnXmlCursorgets used to get or insert XML into a special position of an XML document. But what you need is either a table property (text wrapping) or a page setting (multiple columns on page).To get a page spiltted into two columns, see How to add continuous section break to XWPFDocument?. There left side also may contain a table.
To set table properties, which not are available using XWPFTable one can do the following:
Create a
*.docxdocument having the setting. See Set or change table properties for how to set table text wrapping.Unzip the
*.docxfile (each Office Open XML file is simply a ZIP archive) and have a look into/word/document.xml. You will find something likefor table XML.
That are TableProperties having TablePositionProperties and TableOverlap.
Now recreate this using
org.openxmlformats.schemas.wordprocessingml.x2006.main.*classes.Complete example: