Page 1 of 1

Add Table at specific position with data populated

PostPosted: Fri Aug 04, 2017 7:28 am
by prakhar211
Hi,

I would need help with the sample code for defining new table. Would need to add header information and populate data in each cell.
Also i would like to add this table at specific position in the document, how to do that?

Re: Add Table at specific position with data populated

PostPosted: Fri Aug 04, 2017 11:13 am
by jason
See the TblFactory class which contains a method createTable(int rows, int cols, int cellWidthTwips)

But these days the better way to do things is to create a table formatted as you'd like in Word, then use the Docx4j Helper AddIn, or docx4j webapp, to generate corresponding Java code.

To add your table at a specific position in the document, well, assuming WordprocessingMLPackage wordMLPackage and that you want to do that at the top level of content, wordMLPackage.getMainDocumentPart().getContent() gives you access to List<Object> so you can use add(int index, E element) to insert the specified element at the specified position