Home Forums Development Dynamically inserting images via merge fields

Viewing 11 reply threads
  • Author
    Posts
    • #15820
      JohnJohn
      Participant

      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 Drew PenleyAndrew Penley.
      • This topic was modified 3 years, 9 months ago by JohnJohn.
    • #16162
      norairnnorairn
      Participant

      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
    • #16172
      JohnJohn
      Participant

      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
    • #16184
      norairnnorairn
      Participant

      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
    • #16198
      JohnJohn
      Participant

      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
    • #16207
      norairnnorairn
      Participant

      John,

      Attached see a set of java junit test files from the nexj.core.template project. Let me know if it helps.

      0
      Attachments:
      You must be logged in to view attached files.
    • #16225
      JohnJohn
      Participant

      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
    • #16234
      norairnnorairn
      Participant

      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
    • #16244
      norairnnorairn
      Participant

      John,

      Have you tried INCLUDEPICTURE with a data URL (data:image/jpeg;base64:…)?

      0
    • #16252
      JohnJohn
      Participant

      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
    • #16264
      norairnnorairn
      Participant

      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
    • #16325
      JohnJohn
      Participant

      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

      1. They are NOT automatically loaded (so not /lib)
      2. 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
Viewing 11 reply threads
  • You must be logged in to reply to this topic.