You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by sars84 <sa...@googlemail.com> on 2013/02/06 17:45:55 UTC

Re: AW: Replacing placeholder to a Table.

Hi;

I have now the same Problem. i tried this way, but no success.
Did you got another solution for the table or what had you done?
----
XWPFDocument newDoc = new XWPFDocument();
for(IBodyElement element : list){
if(element.getElementType().equals("TABLE"){
XWPFTable tb = newDoc.createTable();
newDoc.setTable(newDoc.getPosOfTable(tb), tableToAdd);
}
} 
---

I need some support, please...
Thanks in advance.
sars



--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Replacing-placeholder-to-a-Table-tp3392424p5712072.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


Re: AW: Replacing placeholder to a Table.

Posted by Jaakov Jalink <hi...@gmail.com>.
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


Re: AW: Replacing placeholder to a Table.

Posted by Mark Beardsley <ma...@tiscali.co.uk>.
Sorry to say this but you will need to download the source for the project
and create a patch for XWPF yourself so that it handles the insertion of
tables correctly. Remember that POI is not a commercial product and that
everyone that works on it does so as a volunteer. Typically, people develop
areas of the api as they need that specific functionality; it seems as
though you have a pressing need to work with tables and so are the ideal
person to enhance this part.

The problem may not be as simple as it seems. Yesterday, I played around
with some test code and found that the createTable() methods do create a new
table that is added to the bottom of the list of body elements. If a call to
the setTable() method is made, this will move the position of the table in
the body element list but seems to create a second entry; it appears to
leave the original entry at the bottom of the list and then create a second
entry in the correct position. Confusingly however, saving this document
creates xml markup with just a single copy of the table, the one created at
the bottom of the list of body elements and it seems to me that the write
process it doing something a little strange. To confirm this, I created the
new table directly myself and then set it into the document. This did result
in a list of body elements that showed a single entry for the table and in
the correct position. It still, however, resulted in the markup being
created with the table at the end of the document. This is why I suspect
that something is awry with the writing of the markup when the document is
saved, no proof just a suspicion.

I cannot investigate further myself - not clever enough to dig around in the
source code of the api - but this is what I feel you will have to do. Find
out how the list of body elements is processed when the document is saved
and why the position of the table within it is being ignored when a new
table - seemingly, existing tables read in from documents are handled
properly but I have yet to fully confirm this - is created and I think you
will have the answer.



--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Replacing-placeholder-to-a-Table-tp3392424p5712081.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