Encountered this issue a few hours ago, here is my solution:
XmlCursor cursor = doc.getDocument().getBody().newCursor();
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject o = cursor.getObject();
if (o instanceof CTP) {
XWPFParagraph paragraph = new XWPFParagraph((CTP) o,
doc);
for (CTR run : paragraph.getCTP().getRList()) {
for (CTText text : run.getTList()) {
if(newText.indexOf("POSITION")>-1){
newText =
newText.replaceAll("POSITION", "");//remove place holder
text
text.setStringValue(newText);
XWPFTable tableOne =
paragraph.getBody().insertNewTbl(c);
}
}
}
} else if (o instanceof CTTbl) {
XWPFTable t = new XWPFTable((CTTbl) o, doc);//i don't
care about these
right now
}
}
cursor.dispose();
Where POSITION is place holder in for where the table should be
Table is placed right before the paragraph with the placeholder
The trick with insertNewTbl is that the cursor has to come from the document
body, otherwise it returns null (see XWPFDocument source)
Note - ooxml-schemas-1.1.jar required for this to work ( download link
<http://repo1.maven.org/maven2/org/apache/poi/ooxml-schemas/1.1/> )
--
View this message in context:
http://apache-poi.1045710.n5.nabble.com/Replacing-placeholder-to-a-Table-tp3392424p5713654.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]