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
main.save(template, "output.docx");
System.out.println("DONE");
} catch (Exception e) {
e.printStackTrace();
}
}
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;
text.setSpace("preserve");
if (text.getValue() != null && text.getValue().equalsIgnoreCase(placeHolder)) {
if (text.getParent() instanceof R) {
text.setValue(null);
R r = (R) text.getParent();
r.getContent().add(drawing);
}
}
}
}
}
}
}
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
<w:drawing>
<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"/>
<wp:cNvGraphicFramePr>
<a:graphicFrameLocks noChangeAspect="true"/>
</wp:cNvGraphicFramePr>
<a:graphic>
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:pic>
<pic:nvPicPr>
<pic:cNvPr id="1" name="signature-hint"/>
<pic:cNvPicPr/>
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed="rId16"/>
<a:stretch>
<a:fillRect/>
</a:stretch>
</pic:blipFill>
<pic:spPr>
<a:xfrm>
<a:off x="0" y="0"/>
<a:ext cx="4762500" cy="2381250"/>
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst/>
</a:prstGeom>
</pic:spPr>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>
The documents and results are in attachments.
Do you have any idea why or did I missed something ?
Thanks you !