Search code examples
javaxmldocxdocx4j

Docx4J get header/footer elements from docx file and modify them


I am using Docx4J to modify docx templates and put values in place of placeholders in the template that are predefined.

so far, i had been successful in finding and replacing paragraphs and texts, tables, images etc. But i am not yet successful in finding Header and/or Footer elements of the document.

I am using

WordprocessingMLPackage wordMLPackage =
WordprocessingMLPackage.load(new java.io.File(inputfilepath));
wordMLPackage.getMainDocumentPart(); 

to search for elements in the template.


Solution

  • For your application, you can mimic the code in https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/model/datastorage/BindingHandler.java at line 145

    A similar approach is taken in https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/model/fields/merge/MailMerger.java at line 124