Page 1 of 1

Step by step examples for Content Control Data Binding

PostPosted: Tue Apr 30, 2019 6:34 am
by javaDev23
(I want to preface this by saying that I'm a beginner at using docx4j)

I have a Word template, complete with fonts, colors, etc. I am querying a database and retrieving information into a POJO. I want to extract the relevant info from said POJO and create a Word document as per my template's directives.

The doc will have tables and graphs so I need to use Content Control Data Binding. As I understand it, I'll have to do the following to achieve this

    Modify the Word template to add content controls
    Transform the POJO into an XML object (template?)
    Use ContentControlMergeXML to bind the XML data to the Word template
Unfortunately, I can't find a good step-by-step example of this anywhere. Nearly all of the links in this forum lead to broken GitHub pages

My questions

    How can I use OpenDoPE to add tags to my Word template? I'll need to preserve style, so I want the correct OpenDoPE version
    Should the POJO be converted into an XML object or document?
Is there an end to end example of this entire process so I can follow along? (preferably with source code)

Re: Step by step examples for Content Control Data Binding

PostPosted: Tue Apr 30, 2019 11:31 am
by javaDev23
Update:

I found the valid github repo. I think I'm getting a little more acquainted with the process, but have a few more holes that need to be filled in.

For instance, I'm referencing ContentControlsApplyBindings.java in the sample docx4j folder. From what I understand, we get the input file (binding-simple.docx) that contains the content controls. We then apply the bindings as described in the binding-simple.docx's XML pane to another output file. In this scenario, both of the files look identical as the binding-simple file was already populated with valid XML values.

Now suppose the input file does not contain the XML but only the content controls -- how can I inject the XML (that will be a result of transforming my POJO into an XML format) to bind to the content controls described in the input file?

Re: Step by step examples for Content Control Data Binding

PostPosted: Tue Apr 30, 2019 7:46 pm
by jason
Just answered your cross-post at https://stackoverflow.com/questions/559 ... 8#55917038

(Please don't cross-post!)

If you have a read of that answer and look at the sample referenced (ContentControlBindingExtensions) there, I think that should answer your question about how to get the XML injected.

Re: Step by step examples for Content Control Data Binding

PostPosted: Wed May 01, 2019 3:58 am
by javaDev23
Thank you. I'm able to successfully bind simple strings and variables.

Now the challenge that I'm facing is successfully repeating rows in a table. I've followed your instructions in ContentControlBindingExtensions.java, and your example works perfectly. However, when I try to implement the same concept in my Word template, the repeat doesn't occur (the first row is being populated correctly, but the subsequent rows are not).

I have a few differences in my content controls that may account for this

  • I see that your XPath has a [1] next to certain fields -- in this case, next to 'invoice' and 'item'. What is this for? Thus far, I've been setting my XPath by clicking on the XML tag in the OpenDoPE editor (i.e. usually something like /example/items/item/description). Yet yours looks like /invoice[1]/items/item[1]/name
  • I haven't been able to edit/add tags (I can see the tags but can't seem to populate them w/a value). Are tags for purely cosmetic purposes or do they serve another functionality? How can I add a tag?
  • The first row in your 'invoice.docx' file is already populated with "apples" and "$20". No rows have been populated in my template at design time, as all of the data is to come from the XML at run-time. Unlikely, but could this be the source of my issue?

Thanks again for your help!

Re: Step by step examples for Content Control Data Binding

PostPosted: Wed May 01, 2019 5:03 am
by javaDev23
As a follow up,

I'm using the code in ContentControlBindingExtensions.java as is

Re: Step by step examples for Content Control Data Binding

PostPosted: Wed May 01, 2019 9:31 am
by jason
To repeat a table row, you need to wrap it in a "repeat" content control.

Such a content control contains a tag like:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<w:tag w:val="od:repeat=x2"/>
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


In this example, od:repeat refers to an XPath x2, for example:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  <od:xpath id="x2">

    <od:dataBinding storeItemID="{8b049945-9dfe-4726-9de9-cf5691e53858}" xpath="/invoice[1]/items/item"/>

  </od:xpath>
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


Please see further https://opendope.org/opendope_conventions_v2.3.html

I see that your XPath has a [1] next to certain fields -- in this case, next to 'invoice' and 'item'. What is this for? Thus far, I've been setting my XPath by clicking on the XML tag in the OpenDoPE editor (i.e. usually something like /example/items/item/description). Yet yours looks like /invoice[1]/items/item[1]/name


Shouldn't matter, [1] just means the first; please refer to the XPath spec. Some authoring tools might insert it.

I haven't been able to edit/add tags (I can see the tags but can't seem to populate them w/a value). Are tags for purely cosmetic purposes or do they serve another functionality? How can I add a tag?


The w:tag is critical; please see above.

The first row in your 'invoice.docx' file is already populated with "apples" and "$20". No rows have been populated in my template at design time, as all of the data is to come from the XML at run-time. Unlikely, but could this be the source of my issue?


That's Word automatically evaluating the plain binds against the Custom XML part present in the docx. There has to be a sample Custom XML part in the docx at design time; docx4j replaces this at run time

Re: Step by step examples for Content Control Data Binding

PostPosted: Wed May 01, 2019 11:24 am
by javaDev23
Thanks for the response!

I was actually able to figure this out -- one thing to note though -- I think I'm using an older version of OpenDoPE because my XML is all built from scratch and looks nothing like the examples you posted. Regardless, I'm able to dynamically populate tables -- onto the next challenge!

Now I need to insert a chart (line graph) in a Word template. Is this possible, especially with the older version of OpenDoPE that I'm using?
So far, I've accomplished the following

  • Query the DB for some data, which is returned as a POJO
  • Convert the POJO to an XML file
  • Use the XML file to populate the Word template
  • Save new output Word doc

To create the line graph, my requirements are

  • Perform some business ops on the data found in the POJO (i.e. aggregate 'Count' for each 'Day')
  • Convert the POJO - specifically this newly processed data - to an XML file (*this won't be a distinct step from the one listed above but wrapped into the same step -- just wanted to call it out)
  • Use the XML file to create a line graph in the place specified by the Word template
  • Save new output Word doc

I don't mind if the line graph is an image or an actual chart -- whichever is easier. Is there some documentation out there to accomplish this task?

Re: Step by step examples for Content Control Data Binding

PostPosted: Thu May 02, 2019 9:25 pm
by jason
I'm afraid there's nothing in OpenDoPE which does charts for you, although I know of a user who iirc did extend it to do this. They didn't contrib the code back though.

That said, you can manipulate charts using docx4j. Its a little complex though since they are typically backed by data in an embedded spreadsheet. If you can rely on Word to refresh the chart (ie the docx will be opened in Word), then things are simpler than if you have to draw the chart correctly yourself.

If as you say an image is enough, then your options are wider. Any third party tool that can give you a chart image could do the trick.

Speaking of third party tools, you might google something like "R Office Chart".

Re: Step by step examples for Content Control Data Binding

PostPosted: Sat May 04, 2019 8:54 am
by javaDev23
Thanks for the suggestions - I'm able to generate a graph using XChart.
Now for the final step!

I'm looking at your sample code (ImageAdd.java) and I see that you convert the image into bytes and then add it as an object (after it's converted to org.docx4j.wml.P).

Is it possible to convert the image into bytes, store it in the same XML file in which I store my other data, then bind it in a similar fashion? This method would be preferable since I can just embed the byte-encoded image into the XML file that I'm already using to hold my other data, and bind it to the content controls in my Word template. I can't find a code sample that does this however ...

(Edit: To clarify, I'm using the old version of OpenDoPE that allows custom XML)

Re: Step by step examples for Content Control Data Binding

PostPosted: Sat May 04, 2019 7:34 pm
by jason
Have a look at https://github.com/plutext/docx4j/blob/ ... mages.docx

IIRC with http://www.opendope.org/downloads/autho ... /setup.exe it will detect you are binding an image and set things up properly. (You might need to right click?)