You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Jaakov Jalink <hi...@gmail.com> on 2013/08/27 16:55:07 UTC

Re: AW: Replacing placeholder to a Table.

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: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org