Page 1 of 1

Inserting new tables and modify existing ones

PostPosted: Tue Sep 06, 2011 5:15 pm
by Vaish
Hi,
I have a requirement, in which I have a docx file (almost 150 pages) containing images, text and tables, which will be used by my program as a template. My program should read it and insert new tables and modify some of the existing tables with data coming from DB.

I am able to modify existing table using docx4j, but insertnig new tables (number of tables is also dynamic, and no. of rows in each table is also dynamic). These tables need to be inserted at placeholders marked in document at various places... Is it possible using docx4j? Could you give me some pointers on how to perform this?

Could I use an approach, like I sequentially read source document and copy the contents to destination document, if a placeholder appears, take control to insert a table in the destination document, and then come back to source document to complete the rest of document?

Thanks in advance,

Vaish

Re: Inserting new tables and modify existing ones

PostPosted: Wed Sep 07, 2011 1:22 am
by jason
I'd recommend you consider custom xml databinding to do this. Basically, you set up a docx using the OpenDoPE Word Add-In, with repeating table rows, etc, then at run time, provide an XML data file to docx4j, which it will use to populate your document.

If you don't want to take that approach, of course you can manually search for your placeholders, and insert tables there as you wish. TblFactory shows you how to create tables.

Your sequentially-read-source-document-and-copy--contents-to-destination-document approach will work provided you take care of all rels (eg images, comments, footnotes, etc). If you put them in your destination docx using the same rel id's that should just work, and there are no collisions, that may just work...

Re: Inserting new tables and modify existing ones

PostPosted: Fri Sep 09, 2011 4:56 pm
by Vaish
Thanks Jason.
I could insert one table for one Text object found in the document. (Second appraoch, using TblFactory) So this is done, Thanks.

But I was wondering, can I insert more than one table (number of tables is dynamic depends upon database value), which I guess is not possible with this second appraoch, as the code sees the index of the Text and adds the table at same index.
Now if I need more than one table to appear there, I thought I need those many placeholders in my document... (very crude appraoch!!! but... ) I thought I will try to find out this Text, and write Number of Text objects in new temperory file... (but then, if you can not insert more tables, you can not even insert Texts with different index, isn't it?) so, I am back to square one!!!

Any thoughts will surely help...

Re: Inserting new tables and modify existing ones

PostPosted: Tue Sep 13, 2011 10:43 pm
by jason
Well, the OpenDoPE approach can repeat tables for you.

Re: Inserting new tables and modify existing ones

PostPosted: Wed Oct 25, 2017 5:57 pm
by monika_thakran
Hi,

I am new to docx4j and looking for a solution where in I can fill data in a table existing in a docx template.

Every where I am able to find code to create a new table and add it to the documentparts but my requirement is to get the table from documentparts and then fill data in it

Re: Inserting new tables and modify existing ones

PostPosted: Wed Oct 25, 2017 8:46 pm
by jason
Do you need to create extra rows, or just fill existing cells?

Re: Inserting new tables and modify existing ones

PostPosted: Wed Oct 25, 2017 9:06 pm
by monika_thakran
Need to add extra rows, planning to generate an account statement which has headers already populate.

Re: Inserting new tables and modify existing ones

PostPosted: Fri Oct 27, 2017 3:08 pm
by monika_thakran
Any idea how we can do this?

Re: Inserting new tables and modify existing ones

PostPosted: Fri Oct 27, 2017 5:43 pm
by jason
To repeat table rows, I'd use OpenDoPE repeats.

Or you might try https://github.com/thombergs/docx-stamper which has repeatTableRow

Or you can of course write your own code to add table rows.