Page 1 of 1

MERGEFIELD processing deletes fields

PostPosted: Fri May 02, 2014 11:27 pm
by turtle
I've just started to evaluate docx4j-3.1.0.jar and it seems to suit my need.

Unfortunately I found a problem with it. :o

My requirement is to merge fields in a document and in a contained table as well.

I wrote a helper function to to the latter and found that the former is giving me headaches. The former is done via org.docx4j.model.fields.merge.MailMerger.getConsolidatedResultCrude and this seems to delete all other merge fields. This is a problem as the second step iterates the table, find any fields in it and adds additional lines with some (merged) text.

i wrote a small test program which shows the problem and attached it.

It reads in the document inTable.docx which has 3 fields (two in a table and one global). The code replaces the outer field with the text "FooBar" which is done correctly. Sadly, the other two fields are gone also as can be seen in the output file outTable.docx

My question is on how to proceed?

Re: MERGEFIELD processing deletes fields

PostPosted: Mon May 05, 2014 1:14 pm
by jason
Somewhere before getConsolidatedResultCrude, configure:

Code: Select all
       org.docx4j.model.fields.merge.MailMerger.setMERGEFIELDInOutput(
                  org.docx4j.model.fields.merge.MailMerger.OutputField.KEEP_MERGEFIELD);    

Re: MERGEFIELD processing deletes fields

PostPosted: Mon May 05, 2014 5:32 pm
by turtle
Thanks for replying and indeed sounds good but to no avail, though :?

See my changed test code

Re: MERGEFIELD processing deletes fields

PostPosted: Mon May 05, 2014 7:45 pm
by jason
Adding it to your original code worked for me. What are you seeing?

Re: MERGEFIELD processing deletes fields

PostPosted: Mon May 05, 2014 10:59 pm
by turtle
Sorry, my fault :(

Seems, indeed, to work correctly but my test was with both functions (Merge+Adding lines in the table) instead of the one I posted.

My own function doesn't work correctly :oops:

But thanks for your help and after rewrite it should work it in my environment.