Page 1 of 1

INCLUDEPICTURE MERGEFIELD

PostPosted: Fri May 29, 2015 2:26 am
by smgomes
Hi,

at DOCX I have this : {MERGEFIELD name \*MERGEFORMAT}
I use this code to replace the field and everything works fine :

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(FILE_PATH_IN));
List<Map<DataFieldName, String>> data = new ArrayList<Map<DataFieldName, String>>();
Map<DataFieldName, String> item = new HashMap<DataFieldName, String>();
item.put(new DataFieldName("name"), "Peter");
data.add(item);
org.docx4j.model.fields.merge.MailMerger.setMERGEFIELDInOutput(OutputField.KEEP_MERGEFIELD);
org.docx4j.model.fields.merge.MailMerger.performMerge(wordMLPackage, item, true);
wordMLPackage.save(new java.io.File(FILE_PATH_OUT));


PROBLEM
At my DOCX I have : {INCLUDEPICTURE "{MERGEFIELD picturelocation}" \d}
I use the same code to replace picturelocation but doesn´t work! I want this :
item.put(new DataFieldName("picturelocation"), "C:\Images\image.png");

Can anyone help me to solve this problem?
Thanks!

Re: INCLUDEPICTURE MERGEFIELD

PostPosted: Fri May 29, 2015 3:52 am
by smgomes
UPDATE

My TAG was WRONG, I change it to {INCLUDEPICTURE "{MERGEFIELD path}" \*MERGEFORMAT} and run the code :

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(FILE_PATH_IN));
List<Map<DataFieldName, String>> data = new ArrayList<Map<DataFieldName, String>>();
Map<DataFieldName, String> item = new HashMap<DataFieldName, String>();
item.put(new DataFieldName("path"), "C:\\Images\\image1.png");
data.add(item);
org.docx4j.model.fields.merge.MailMerger.setMERGEFIELDInOutput(OutputField.KEEP_MERGEFIELD);
org.docx4j.model.fields.merge.MailMerger.performMerge(wordMLPackage, item, true);
wordMLPackage.save(new java.io.File(FILE_PATH_OUT));

The problem is: When I run the second time for another image item.put(new DataFieldName("path"), "C:\\Images\\image2.png"); when I open the DOCX the image still the same, doesn´t change! I pressed F9 to update and nothing!

What's wrong?
Thanks

Re: INCLUDEPICTURE MERGEFIELD

PostPosted: Fri May 29, 2015 8:41 am
by jason
There are 2 things there the current code doesn't support:

1. processing INCLUDEPICTURE. You could add that easily enough, since there is code elsewhere in docx4j for processing images (eg bound content controls of type picture)

2. processing of nested fields (except in some limited cases)

It'd be great if you could contrib code to improve this.

Re: INCLUDEPICTURE MERGEFIELD

PostPosted: Fri May 29, 2015 9:07 am
by smgomes
Thanks jason for your reply!

I have a DOCX file and I want to replace image with docx4j.
The DOCX is created by user (Word2013), he uses CTRL+F9 to build the TAGS, and run an external application to do the updates.
At my application I receive a Map<String, String>, where KEY is the FIELD to UPDATE and VALUE is the PATH to IMAGE...

Can you give me an example?
I would really appreciate it if you could help me.

Re: INCLUDEPICTURE MERGEFIELD

PostPosted: Mon Jun 01, 2015 7:55 pm
by jason
Well, assuming you were able to convert the nested field, so instead of:

{INCLUDEPICTURE "{MERGEFIELD path}" \*MERGEFORMAT}

you had

{INCLUDEPICTURE "some path" \*MERGEFORMAT}

you could then add code to handle an INCLUDEPICTURE field. You could base it on the method xpathInjectImage at line 1000 in https://github.com/plutext/docx4j/blob/ ... rXSLT.java

But if it was me, I'd throw away the fields-based approach you have, and use content control data binding instead (which handles bound picture content controls).

datastorage/migration/FromMergeFields might help you migrate over:

https://github.com/plutext/docx4j/blob/ ... ields.java

but you'll need to modify your source docx manually (or that class) since it handles MERGEFIELD and not INCLUDEPICTURE (and not your nested constructs).