0

I have 2 files - one a template document in the form of a standard MS-Word docx format file, which is marked up with Content Control placeholders using the OpenDoPE add-in for MS-Word developed by Plutext. The second file is an XML file containing the data. Merging the two works perfectly using routines from DOCX4J. All good so far. The problem is that the XML file contains just the file pathname to where the image is located. So, after the merge, the Word document contains in place of the Content Control placeholders, a string indicating where the image is. How do I go about getting an image into the document in place of the image location strings.

One approach considered would be to replace the image path inside the XML data file with a base64 encoding of the image (not sure exactly how to do that, but the documentation for OpenDoPE suggest it should work). Another option seems to be to find the path strings in the already-merged Word document and then use something like the BinaryPartAbstractImage function from the DOCX4J library to insert the image in place. The problem with this seems to be that any search and replace operation in an OpenXML document appears to be fraught with difficulties.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Pure code-writing requests are off-topic on Stack Overflow -- we expect questions here to relate to *specific* programming problems -- but we will happily help you write it yourself! Tell us [what you've tried](http://whathaveyoutried.com), and where you are stuck. This will also help us answer your question better. – Elliott Frisch Aug 01 '15 at 20:25
  • [Base64 encoding in Java](http://stackoverflow.com/questions/13109588/base64-encoding-in-java) – Elliott Frisch Aug 01 '15 at 20:28

1 Answers1

0

At authoring time, put the base64 encoded image in some element in the sample XML file you use in conjunction with "friendly" OpenDoPE add-in.

Then, when you drag that element onto the document surface, the add-in should detect that it is an image, and add the corresponding tag to the content control.

If that is done, then at run-time, whatever base64 encoded image is in your run-time XML, will be used.

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
  • So, it is not necessary to add an image control from the Developer menu and then right click on the image element and point it at the image control? Any pointers on where to find code to achieve the replacement of the image filename with the Base64 encoded string in the XML file? – Mark Schormann Aug 02 '15 at 20:52
  • No, but its just get the image (you could use commons IOUtils for that) in whatever form (inputstream, byte[]?) your base 64 encoder needs, use XPath to find the element, then set its contents. Create a new question with your code if you have problems. – JasonPlutext Aug 02 '15 at 22:08