Home › Forums › Development › Dynamically inserting images via merge fields
- This topic has 11 replies, 2 voices, and was last updated 3 years, 8 months ago by John.
-
AuthorPosts
-
-
February 22, 2021 at 10:05 am #15820JohnParticipant
Has had success dynamically inserting images via merge fields? Our use case is doing mail merge to insert 2 profile images and 2 email signatures into printed correspondence.
This approach would work if all possible images were embedded in the template file:https://superuser.com/questions/301688/conditional-images-in-mail-merge
But does anyone know a way to dynamically get the files rather than embedding them in the template?
0- This topic was modified 3 years, 9 months ago by Andrew Penley.
- This topic was modified 3 years, 9 months ago by John.
-
March 5, 2021 at 2:54 pm #16162norairnParticipant
Hi John,
Have you tried using the approach described here: https://www.texvet.org/blogs/texvetwebmaster/tech-skill-including-pictures-mail-merge?
Use ctrl+F9 to insert a IncludePicture field with a dummy picture url. Then shift+F9 to edit the source of the field and replace the url with a MERGEFIELD providing an image URL or image OID.
It will look something like this: { INCLUDEPICTURE “MERGEFIELD ${imageURL}” \* MERGEFORMAT}
0 -
March 6, 2021 at 12:41 am #16172JohnParticipant
Thanks, Norair.
> or image OID
Do you mean that if the merge field expands to an OID, the framework will look up the binary image data and embed it into the document, or that the OID can be used as part of a URL? I tried the former – a merge field that expands to an Obj’s OID and it didn’t work.
I’m also having trouble expanding to a URL – I can expand it manually but if it’s expanded via merge fields it doesn’t render the image, at least not until I open and close the “Edit Field..” dialog.
0 -
March 6, 2021 at 12:56 pm #16184norairnParticipant
Hi, John:
It would probably be easier to have a calculated attribute returing entire image URL (which would include an OID) and embedding that in a MERGEFIELD.
I started experimenting with image URLs (not dynamically generated yet). They don’t refresh automatically, so I used a trick from here: https://www.msofficeforums.com/mail-merge/17385-includepicture-does-not-refresh-pictures.html. Have you used something like this?
0 -
March 7, 2021 at 3:59 pm #16198JohnParticipant
Hi Norair,
Thanks for the suggestion.
As an alternative to the URL approach, I am wondering if it may be possible to use POI to insert images as a second pass? With the URL approach, I’m worried about the complexity and/or security implications of opening up unauthenticated attachment access.
Using POI to insert images as a second pass would be simple with a recent version of POI but I’m having trouble instantiating an XWPFDocument using scheme. I’m wondering if there is some sample code, even in Java, showing how SysTemplate.java opens a docx file? Java doc for the patched POI would also work.
0 -
March 7, 2021 at 4:13 pm #16207
-
March 8, 2021 at 10:39 pm #16225JohnParticipant
Thanks that helps. I’ve figured out most parts, I think, except that I can’t find any methods for inserting an image. Is there Java code showing how to insert an image? It might be related to XWPFRun.
0 -
March 8, 2021 at 11:13 pm #16234norairnParticipant
The POI API docs mention https://poi.apache.org/apidocs/4.0/index.html XWPFRun.addPicture() method, but I don’t see it in our bundled poi jar. Ours is probably out of date. See if you can lift the addPicture method from the newer poi source and bundle it as custom java code.
0 -
March 9, 2021 at 9:54 am #16244norairnParticipant
John,
Have you tried INCLUDEPICTURE with a data URL (data:image/jpeg;base64:…)?
0 -
March 9, 2021 at 11:24 pm #16252JohnParticipant
I tried it when you suggested it but I couldn’t get it to work. Have you seen it to work?
I’m currently leaning towards using a private class loader to load POI5 and use it for this step. So far this is working (I have to use a parent last class loader).
0 -
March 10, 2021 at 4:21 pm #16264norairnParticipant
John,
Personally, I’ve never tried this. But from looking around on the web I see that embedded pictures are stored as base64 encoded values in docx. Also, there is an Office add-in API https://docs.microsoft.com/en-us/javascript/api/word/word.inlinepicture which has methods to insert, read base64 inline images.
0 -
March 14, 2021 at 2:16 pm #16325JohnParticipant
Norair,
I have a solution with POI 5 that replaces field {INCLUDEPICTURE (oid ..some oid..)} with an image that is the same size as the INCLUDEPICTURE placeholder image. This runs as a 2nd pass and adds generic image merge field support so long as the image’s Obj oid is exposed on some object.
To do this I need to load POI 5.0 in a different classloader. This is fine in local development but I am wondering where I should put these POI5 jars such that
- They are NOT automatically loaded (so not /lib)
- They end up extracted on disk in a reliable location (would /mod be a good choice? In a deployment is this extracted onto disk and if so, do you know how I can get the path in scheme?)
Thanks!
0
-
-
AuthorPosts
- You must be logged in to reply to this topic.