You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by primeq <pa...@yahoo.com> on 2012/03/28 13:36:23 UTC

Re: AW: Replacing placeholder to a Table.

Nick,

I'm unable to find any more information on what seems to still be inability
to place a new table in a specified location (i.e. not at the bottom of the
document).

Do you know if there have been any changes, or any new advice on how to make
the table go where I want (happens I also want to put it within a
table-cell)
Nick Burch-11 wrote
> 
> On Mon, 27 Jun 2011, Brian Rosenberger wrote:
>> Each element is exposed through a separate list, thus ordering is lost.
> 
> I think we should be maintaining a list with both paragraphs and tables in 
> it though, with relative ordering preserved. On XWPFDocument there's
>  	List<IBodyElement> bodyElements
> for example, which is built by doing a child walk and storing the entries 
> in the order they occur
> 
> I'd look at extending that to suit your needs
> 
> Nick
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@.apache
> For additional commands, e-mail: user-help@.apache
> 


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


Re: AW: Replacing placeholder to a Table.

Posted by sars84 <sa...@googlemail.com>.
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 IsadorCJ <is...@gmail.com>.
I have a way of dealing with the "replacement" issue. I have tried serveral
days, but cannot finish the task. (a similar task as the OP had). 
So my approach is create an empty List<IBodyElement> 
so now you read in your templates, according to the templates, construct
paragraphs and tables, and then stuff them into the list with order. 
and then you can use a for loop:
XWPFDocument newDoc = new XWPFDocument();
for(IBodyElement element : list){
if(element.getElementType().equals("PARAGRAPH"){
XWPFParagraph pr = newDoc.createParagraph();
newDoc.setParagraph((XWPFParagraph) element, newDoc.getPosOfParagrah(pr));
}
}
and add your if condition to table.......by doing the same.....

this will do the trick~ i've proven it work. 

but this has a serious problem: if there is a picture in the original
document, it will show as a "broken linked picture" like what you see on
website, and tells you the picture currently cannot be showed. 


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