Page 1 of 1

Picture bindings and repeats

PostPosted: Fri Feb 10, 2012 9:50 am
by olabrosse
Hi, I'm having a hard time figuring out what's wrong with my code and/or Word template. I searched both this forum and the whole Web and couldn't find any working solution. Here is my situation:

My problem is two-fold. For one, my Word template contains many repeat controls, including nested ones, and apparently when applyBindings() is called only the first element's data is used in each iteration. Secondly, the template includes picture content controls, and applyBindings() leads to the Base64 encoding to appear as text rather than a picture in the output.

One thing that seems peculiar in my case is that the custom XML part isn't populated in the template. All the XPaths I define don't actually exist until my application dynamically generates the XML and saves it in the custom XML part. Here is the code that is executed at this point:

Code: Select all
         OpenDoPEHandler odh = new OpenDoPEHandler(wmlPackage);
         odh.preprocess();
         OpenDoPEIntegrity odi = new OpenDoPEIntegrity();
         odi.process(wmlPackage);

         // Apply the bindings
         BindingHandler.applyBindings(wmlPackage.getMainDocumentPart());

         // Remove controls and leave the contents
         RemovalHandler rh = new RemovalHandler();
         rh.removeSDTs(wmlPackage, Quantifier.ALL);

         wmlPackage.save(new File(docxFilePath));

As I said above, the XPaths in the template don't point to any existing data, which means I don't get any CTDataBinding elements in document.xml; instead this is what a picture content control looks like:

Code: Select all
<w:sdt>
   <w:sdtPr>
      <w:tag w:val="od:xpath=orgChartDiagram"/>
      <w:id w:val="1900247006"/>
      <w:showingPlcHdr/>
      <w:picture/>
   </w:sdtPr>
   <w:sdtContent>
      <w:p w:rsidR="00837201" w:rsidRDefault="00D873A9">
         <w:r>
            <w:rPr>
               <w:noProof/>
            </w:rPr>
            <w:drawing>
               <wp:inline distT="0" distB="0" distL="0" distR="0">
                  <wp:extent cx="1905000" cy="1905000"/>
                  <wp:effectExtent l="0" t="0" r="0" b="0"/>
                  <wp:docPr id="3" name="Picture 3"/>
                  <wp:cNvGraphicFramePr>
                     <a:graphicFrameLocks noChangeAspect="1"/>
                  </wp:cNvGraphicFramePr>
                  <a:graphic>
                     <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
                        <pic:pic>
                           <pic:nvPicPr>
                              <pic:cNvPr id="0" name="Picture 3"/>
                              <pic:cNvPicPr>
                                 <a:picLocks noChangeAspect="1" noChangeArrowheads="1"/>
                              </pic:cNvPicPr>
                           </pic:nvPicPr>
                           <pic:blipFill>
                              <a:blip r:embed="rId9">
                                 <a:extLst>
                                    <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
                                       <a14:useLocalDpi val="0"/>
                                    </a:ext>
                                 </a:extLst>
                              </a:blip>
                              <a:srcRect/>
                              <a:stretch>
                                 <a:fillRect/>
                              </a:stretch>
                           </pic:blipFill>
                           <pic:spPr bwMode="auto">
                              <a:xfrm>
                                 <a:off x="0" y="0"/>
                                 <a:ext cx="1905000" cy="1905000"/>
                              </a:xfrm>
                              <a:prstGeom prst="rect">
                                 <a:avLst/>
                              </a:prstGeom>
                              <a:noFill/>
                              <a:ln>
                                 <a:noFill/>
                              </a:ln>
                           </pic:spPr>
                        </pic:pic>
                     </a:graphicData>
                  </a:graphic>
               </wp:inline>
            </w:drawing>
         </w:r>
      </w:p>
   </w:sdtContent>
</w:sdt>

Another important detail is the fact that I put the Base64 image content directly in the XML before it is saved in the package, which means I'm not using any calls to setNodeValueAtXPath(). I don't see how this would cause the binding to show up as text instead of the actual image, but maybe it is the cause here? I must admit I have tried taking the output document (which still contained the custom XML part) and binding a brand new picture content control to an existing XPath containing the Base64 image, but the picture doesn't show up, only a web-like missing image message saying "This image cannot currently be displayed."

Note that I'm using Docx4Java 2.7.1 and I created the template using the Word Add-In.

Many thanks for any support you may provide.

-Olivier

Re: Picture bindings and repeats

PostPosted: Fri Feb 10, 2012 1:00 pm
by olabrosse
Ok, so I have come around to pre-binding the repeated controls (by pre-loading the custom XML and re-binding the controls). This fixes the repeat issue, although I don't see why that would be a requirement.

As for the picture issue, that one changed from converting the picture control into text, to not displaying anything. No text, but no image either.

I'm thinking maybe the Base64 encoding is wrong, or I'm doing something wrong somewhere.

Please advise as to a possible solution; I'm really running out of ideas now.

Thanks for the awesome API by the way! :)

Re: Picture bindings and repeats

PostPosted: Fri Feb 10, 2012 7:14 pm
by jason
You've probably got the XPath to the picture right, but that's the first thing to check.

Have you got log4j logging at DEBUG level on package org.docx4j.model.datastorage?

Attached is a sample docx with a picture binding. I guess you could try replacing the image with your base64 data and see what Word thinks of it; alternatively, open it in Word and paste your image into it, save it, and look at the base64 encoded result.

Re: Picture bindings and repeats

PostPosted: Sat Feb 11, 2012 4:29 am
by olabrosse
Thank you Jason for the quick reply.

I kind of feel stupid now, as I just realized my XML marshaller already does the Base64 encoding... and I was doing it myself beforehand. :oops:

So now all is well.

Hey, what are your thoughts on the requirement for <w:dataBinding> tags to be present in order for repeats to iterate properly? As I said before, if I bind to an XPath that doesn't currently exist and then add the data and call applyBindings(), the first element gets repeated for all.

Again, thanks a lot for the awesome API.

-Olivier