Page 1 of 1

Docx4j: Insert images and text

PostPosted: Sun Sep 28, 2014 10:38 am
by Paluee1
Hi Everyone,

I am new to this forum.
Here is the use case that I am in need of.
I have images that have export from excel into a folder, I also have text that contains key-value pairs in a text file .txt.

In the past I had the keys in a template word document, making up my own markup like <key> and then using Find and Replace with key value pairs automated via the scripting language AutoIt. BUT the images I had to insert manually.

I was hoping there was somekind of basic example of inserting Text and Images in a Word document Template file using
Docx4j.

Just wondering if my use-case is fairly standard stuff, or if there are some limitations in doing this with Docx4j.

Hope someone can help.


Regards,

P

Re: Docx4j: Insert images and text

PostPosted: Tue Sep 30, 2014 9:35 am
by jason
Hi, the most elegant way to do this is to use content control databinding.

That way, your images should "just work". You store them in the XML payload base-64 encoded.

If you don't want to use content control databinding for some reason, you can go a low tech approach:

- replace the text using VariableReplace or MERGEFIELD field codes

- add or replace the images using the lower level facilities provided by docx4j

Re: Docx4j: Insert images and text

PostPosted: Tue Sep 30, 2014 3:35 pm
by Paluee1
Hi there,

I would love to tryout the Elegant way " You store them in the XML payload base-64 encoded".
I have seen images encoded via Base64 for putting in webpages(in javascript), seems to be a similar idea to that.
I tried to research this out and found one of your replies on stackoverflow here:
http://stackoverflow.com/questions/1753 ... re-binding

BUT since I am new to Docx4j, I need a bit more help, like sample of code. Analyzing the deep xml in the Word.docx and understanding
how it is constructed and how to manipulate it seems to be a large learning curve.

SO are there any samples and or tutorial on how to do this.
Any help in this is much appreciated.

P

Re: Docx4j: Insert images and text

PostPosted: Sat Oct 04, 2014 12:57 pm
by jason
https://github.com/plutext/docx4j/blob/ ... cture.docx is an example of a docx with a picture bound to XML.

If you unzip it and look at the document.xml part, you'll see:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
    <w:sdt>
      <w:sdtPr>
        <w:id w:val="1777441255"/>
        <w:showingPlcHdr/>
        <w:dataBinding w:prefixMappings="" w:xpath="/myxml[1]/element1[1]" w:storeItemID="{5448916C-134B-45E6-B8FE-88CC1FFC17C3}"/>
        <w:picture/>
      </w:sdtPr>
      <w:sdtContent>
        <w:p ..>
          :
            <w:drawing>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Notice the w:picture element, and the w:dataBinding

If you look at the XML part (/customXml/Item1.xml), you'll see the base64 encoded content:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<myxml>
  <element1>iVBORw0KG
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


If you open it in Word, Word will display the picture (having first got it from the XML part).

docx4j can do the same thing. See the sample at
https://github.com/plutext/docx4j/blob/ ... dings.java

Usually, the XML you want to inject will be in a separate file outside the docx zip. For that, see https://github.com/plutext/docx4j/blob/ ... geXML.java

How do you set up the docx template in the first place? Use the Word AddIn; from http://www.opendope.org/downloads/autho ... /setup.exe

Go to the authoring tab, add your sample XML file, then drag/drop elements from the task pane onto the document surface.

Good luck .. feel free to post again in the data binding sub forum once you've had a bit of a play around...