Page 1 of 1

Placing data using template

PostPosted: Thu Nov 03, 2011 5:01 am
by snehils
Hi,

I am newbie in docx4j. I have googled my problem but couldn't found proper solution.

Here is what i want:

I have a word template as mentioned follwos.
Employee Code: ${Code}
Employee Name: ${Name}
Employee Designation: ${Designation}
Attendance Table:
Date Day In Time Out Time
1/11/11 Tue 08.00 AM 06.00 PM


The fields in template need to be repeate for n number of employees in word document. For example:

Employee Code: 100
Employee Name: Smith
Employee Designation: Engg
Attendance Table:
Date Day In Time Out Time
1/11/11 Tue 08.00 AM 06.00 PM
2/11/11 Wed 08.00 AM 06.00 PM

Employee Code: 101
Employee Name: John
Employee Designation: Sr. Engg
Attendance Table:
Date Day In Time Out Time
1/11/11 Tue 08.00 AM 06.00 PM
2/11/11 Wed 08.00 AM 06.00 PM

Employee Code: 102
Employee Name: Stefan
Employee Designation: Sr. Engg
Attendance Table:
Date Day In Time Out Time
1/11/11 Tue 08.00 AM 06.00 PM
2/11/11 Wed 08.00 AM 06.00 PM


Could please any one tell me how to copy the same tamplate 'n' number of times and replace the keywords and fill data in table for diffrent employees in a word file?
Which code samples do I have to look on?

Re: Placing data using template

PostPosted: Thu Nov 03, 2011 9:14 am
by jason
Use content controls and databinding (see the sub-forum).

Specifically, docx4j implements the OpenDoPE convention for repeating data - see www.opendope.org

(In a recent post, someone reported problems with the current version of the invoice.docx sample - I'm yet to confirm this. In his post, the reporter reported success with an older version.)

Re: Placing data using template

PostPosted: Thu Nov 03, 2011 5:47 pm
by snehils
Thanks for your reply Jnson,

It would be great help if you can solve my this related problem.

Using the above approach, will the same code work if some of the fields from template are moved to other position or removed?

for example:

Template 1:
----------------
Employee Code: ${Code}
Employee Name: ${Name}
Employee Designation: ${Designation}
Attendance Table:
Date Day In Time Out Time
1/11/11 Tue 08.00 AM 06.00 PM

Template 2:
----------------
Employee Name: ${Name}
Employee Code: ${Code}
Attendance Table:
Date Day In Time Out Time
1/11/11 Tue 08.00 AM 06.00 PM
Employee Designation: ${Designation}

Template 3:
----------------
Employee Name: ${Name}
Employee Code: ${Code}
Attendance Table:
Employee Designation: ${Designation}
In Time Out Time Date Day
08.00 AM 06.00 PM 1/11/11 Tue


Here second template moved the "Employee Designation" field to last. Third template swapped the fields of table.
Can same code adapt the template changes? Or with each template type we have to make some code changes?

Re: Placing data using template

PostPosted: Fri Nov 04, 2011 9:58 pm
by jason
Using your example, you could do:

Code: Select all
    Employee Code: ${Code}
    Employee Name: ${Name}
    Employee Code: ${Code}


You'd make the 2 "employee code" entries into conditional content controls, which only insert their contents if the respective conditions evaluate to true.

The condition is an XPath expression, so provided your rules rely solely on the contents of your XML data file, this will work fine; you'll only need a single template.

Re: Placing data using template

PostPosted: Sat Nov 05, 2011 1:37 am
by lucasfgc
Jason, i'd like to clarify some doubts about this issue: replacing tags

What is the best way to read the tags from the template ?
A simple "getJAXBNodesViaXPath("//w:t", true)" sometimes don't take the tag at all (e.g. TAG is divided into diferent words because the underlined).

There is a way to read each word apart of the formatation ?


Cheers!

Re: Placing data using template

PostPosted: Sun Nov 06, 2011 2:27 pm
by jason
Hi Lucas,

Just to be clear, this thread changed focus from unmarshallFromTemplate to the quite different OpenDoPE approach.

Your post is about unmarshallFromTemplate again (or is it? since you mention using getJAXBNodesViaXPath), so in a day or so I will split this out into a separate thread (and move the rest to the OpenDoPE sub-forum).

In answer to your question though, it depends. Is the problem that you have document authors over whom you have no control? If so, one approach might be to sanitise the document before you start: marshal/serialise the MainDocumentPart to a string, then use a simple algorithm which finds ${ then strips any "<..>" until it finds the matching "}" (which is assumed to be present!). Then unmarshall the result as your new MainDocumentPart.

Note that there are cases where getJAXBNodesViaXPath does not work properly (bugs in JAXB) which I have documented elsewhere; please be aware of that!

Re: Placing data using template

PostPosted: Tue Nov 08, 2011 2:37 am
by lucasfgc
Hi Jason,

Thx for the answer.
I think that i got it :D

Just to know, the template document is mine...

In your opinion, the best way so is to read the first TAG character (like {) and wait for the last one (like }) ?

So, i have to change my JABX way to another one which will read only strings ?


Cheers!