Page 1 of 1

Replace footer placeholder by image

PostPosted: Thu Jun 11, 2020 9:03 pm
by Kekwel
Hi !

I would like to replace a variable in the footer by a picture, and then convert the document to PDF.
First I thought it will be like adding an image in the document so find the word in the footer, and add the image, but it looks like it doesn't work like that.

So I tried to do like this : (I don't put all of the functions, so if you need them, just tell me)
Code: Select all
public static void main(String[] args) {
      try {
         TestFooter main = new TestFooter();
         String modeleDocx = "src/Modele_Certificat.docx";
         // create template
         WordprocessingMLPackage template = main.template(modeleDocx, 0);
         String filename = "src/signature.png";
         Path signatureFile = Paths.get(filename);
         byte[] bytes = Files.readAllBytes(signatureFile);
         Drawing signature = main.newDrawing(template, bytes, "signature-hint", "signature-alt", 0, 1);
         main.searchImagePlaceHolderFooter(template, "${signature}", signature);
         // -- save locally, "output.docx");
      } catch (Exception e) {

   public void searchImagePlaceHolderFooter(WordprocessingMLPackage template, String placeHolder, Drawing drawing)
         throws InvalidFormatException {
      List<SectionWrapper> sectionWrappers = template.getDocumentModel().getSections();
      for (SectionWrapper sw : sectionWrappers) {
         HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
         if (hfp.getDefaultFooter() != null) {
            List<Object> elements = getAllElementFromObject(hfp.getDefaultFooter(), P.class);

            for (Object obj : elements) {
               // On r├ęcupere la variable a remplacer pour l'enelver
               List<Object> texts = getAllElementFromObject(obj, Text.class);
               for (Object textObj : texts) {
                  Text text = (Text) textObj;

                  if (text.getValue() != null && text.getValue().equalsIgnoreCase(placeHolder)) {
                     if (text.getParent() instanceof R) {
                        R r = (R) text.getParent();


In the output, the image cannot be found (red cross). But it is in the word/media folder with the name "document_image_rId16.png".
And in the footer2.xml, the image has the right relId I think ?
Code: Select all
                <wp:inline distT="0" distB="0" distL="0" distR="0">
                    <wp:extent cx="4762500" cy="2381250"/>
                    <wp:effectExtent l="0" t="0" r="0" b="0"/>
                    <wp:docPr id="0" name="signature-hint" descr="signature-alt"/>
                        <a:graphicFrameLocks noChangeAspect="true"/>
                        <a:graphicData uri="">
    <pic:cNvPr id="1" name="signature-hint"/>
    <a:blip r:embed="rId16"/>
        <a:off x="0" y="0"/>
        <a:ext cx="4762500" cy="2381250"/>
    <a:prstGeom prst="rect">

The documents and results are in attachments.

Do you have any idea why or did I missed something ?

Thanks you ! :)

Re: Replace footer placeholder by image

PostPosted: Fri Jun 12, 2020 2:35 am
by Kekwel
Well, I tried to create the Drawing (the image part) with the wrong method..

So instead of
Code: Select all
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(template, bytes);

I used
Code: Select all
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(template, part, bytes);

where part is the FooterPart

And it works !