Page 1 of 1

Working with Tables

PostPosted: Mon Aug 31, 2009 9:01 pm
by eford
I need help adding table objects to the main document. Looking at the document.xml from a Word generated file it looks like tables are, typically, inserted at the same level as P objects. So, I tried calling the MainDocumentPart.addObject method passing in an instance of Tbl but that merely generates an "UNEXPECTED: org.docx4j.wml.Tbl" in the log. It turns out, the traverseMainDocumentRecursive method allows only P and R objects. As near as I can tell, the only way to add a table would be to resort to directly calling the getEGBlockLevelElts().add() method on the Document Body. Since tables contain P elements it would be beneficial to have the style scanning features of traverseMainDocumentRecursive available for tables, as well. Have I overlooked something?

Re: Working with Tables

PostPosted: Tue Sep 01, 2009 9:16 am
by holgerschlegel
On reading a Word generated docx, Tbl objects are wrapped inside JAXBElement instances. Based on that I suspect that you need to add them wrapped like that (I did not test it).

Re: Working with Tables

PostPosted: Tue Sep 01, 2009 5:00 pm
by eford
holgerschlegel wrote:On reading a Word generated docx, Tbl objects are wrapped inside JAXBElement instances. Based on that I suspect that you need to add them wrapped like that (I did not test it).


Are you referring to "reading a Word generated docx" using docx4j? When I created a document using Word and examined the contents of the document.xml file I did not notice any wrapping of the table nodes. By the way, I'm not having a problem adding tables - I'm able to do that, as described, by adding the Tbl node directly as a blockelement. My comment was more related to a preferred solution of using addObject. That method performs some internal style checking via call to traverseMainDocumentRecursive which currently only supports adding P and R nodes.

Re: Working with Tables

PostPosted: Wed Sep 02, 2009 7:27 am
by jason
Hi Eric

eford wrote:
holgerschlegel wrote:On reading a Word generated docx, Tbl objects are wrapped inside JAXBElement instances. Based on that I suspect that you need to add them wrapped like that (I did not test it).


Are you referring to "reading a Word generated docx" using docx4j? When I created a document using Word and examined the contents of the document.xml file I did not notice any wrapping of the table nodes. By the way, I'm not having a problem adding tables - I'm able to do that, as described, by adding the Tbl node directly as a blockelement.


That's right. Definitely no need to wrap a table node in a JAXBElement instance in order to add it. The only vaguely related problem you might run in to is with elements which don't have a @XmlRootElement annotation; but org.docx4j.wml.Tbl has this.

eford wrote: My comment was more related to a preferred solution of using addObject. That method performs some internal style checking via call to traverseMainDocumentRecursive which currently only supports adding P and R nodes.


You should still be able to add a table via addObject (could you please check again?); the "UNEXPECTED: org.docx4j.wml.Tbl" is merely a warning that any fonts and styles contained within it won't be noted as being in use. This should be fixed - I've added http://dev.plutext.org/trac/docx4j/ticket/9