Page 1 of 1

Create multi-page document from a single page template using

PostPosted: Sat Mar 26, 2016 6:39 am
by subnot303640
I am planning to use doc4j for search and replace in a template. I'do like to create the page for each member in the list. Basically, I need to replicate the same page from the template. I have done simple search and replace. However, this little complex one for which I need some sample examples. Here is my requirement:

I have a docx template which has the content with place holders.

There is a table with 3 columns in it and I need to replace with different values for each column like first name, last name and etc. The number of rows may vary anywhere from one to 200. So technically this may go beyond one page. If it exceeds more than one page, then I need the table header to repeat in the next page too.

I want to copy the same template on every page and replace the place holder. Basically create a single document with multiple pages each page for one member.

Please provide me with the example. Appreciate the help. Thanks.

Re: Create multi-page document from a single page template u

PostPosted: Sun Mar 27, 2016 1:13 pm
by jason
docx4j supports three standard methods of content replacement:

(1) "VariableReplace", but this only does it one-off, not for each member in the list https://github.com/plutext/docx4j/blob/ ... place.java
You could extend it easily enough, but the approach in general is limited and brittle

(2) MERGEFIELD, this does support merging multiple times into the one output document,
but really, MERGEFIELDs aren't nice from an XML point of view, and this only makes sense if you are dealing with
legacy documents which already contain MERGEFIELDs

(3) "OpenDoPE" content control data binding. This is how I'd do it. Wrap a repeat content control around the table
(so it repeats for each member of the list). You feed in your data as an XML file, with a structure such as:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<list>
   <member>
       <field1>..</field1>
       or say
       <yourelement name="foo">...</yourelement>
   </member>
</list>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


So your first step is to decide which approach you want to take; you'll probably want to search/read a bit further first.

Re approach (3), you'll see there is a specific sub-forum; and if you decide to try that, I'm happy to help further.

Re: Create multi-page document from a single page template u

PostPosted: Fri Apr 08, 2016 4:56 am
by subnot303640
Thank you for your solution. I was trying to use approach 3, but quite note sure about go about it. Could you please share a sample code for this. Basically, I want to copy a template and fill the placeholders and then repeat the same for multiple list. I want the a single document with multiple pages.
Thanks and appreciate your reply.