Search code examples
javatemplatesdocx4j

refill a docx template with new data in the same document using docx4j


i am trying to fill a docx template with different data but i can not make any progress. I have tried two approaches. First using only java code and second one using content control binding. For the first approach i used this tutorial http://www.smartjava.org/content/create-complex-word-docx-documents-programatically-docx4j and for the second one this one https://github.com/plutext/docx4j/blob/07c0b68512cbb3eef52ae51c3f597a7be4428232/src/samples/docx4j/org/docx4j/samples/ContentControlBindingExtensions.java . The problem is I do not know how to reuse the same template and just fill it with other datasets? Should I use xsl in order to make the logic? The picture below illustrates my goal, where first and second table are meant to be in the same document.

Refill same template


Solution

  • Each time you do the binding, you create a new instance docx.

    If you want the contents of multiple instance documents concatenated in a single docx, you'll have to do that as a separate step.

    If the contents is simple text & tables, you can just copy the contents list.

    More likely, it contains relationship references etc, in which case you need to handle those.