Page 1 of 1

Footer is not always visible if added to docx template

PostPosted: Mon Feb 25, 2019 11:23 pm
by alister
I am adding first page footer to existing docx document in known way.
On some docx footer was visible in document, on others not visible, but still is present in xml. Sample of docx with not visible footer is attached as "sample.docx".
Comparison of xml of docx with visible and not visible footer does not show any difference.
Below is code for generating footer.

Code: Select all
private static void addFooterToDocument(WordprocessingMLPackage wordMLPackage, String footerText)throws InvalidFormatException {
   ObjectFactory factory = Context.getWmlObjectFactory();
   Relationship relationship = createFooterPart(wordMLPackage, factory, footerText);
   createFooterReference(relationship, wordMLPackage, factory);

private static Relationship createFooterPart(WordprocessingMLPackage wordMLPackage, ObjectFactory factory,
         String vFooterText) throws InvalidFormatException {
  FooterPart footerPart = new FooterPart();
  footerPart.setJaxbElement(createFooter(vFooterText, factory, wordMLPackage, footerPart));
  return wordMLPackage.getMainDocumentPart().addTargetPart(footerPart);

private static Ftr createFooter(String contents, ObjectFactory factory,WordprocessingMLPackage wordMLPackage, Part sourcePart) {
   Ftr footer = factory.createFtr();
    byte[] imageBytes = null;
    try {
         imageBytes = GenerateQRCode.createQRImage(contents, 110) ;
    catch(Exception e) {
    org.docx4j.wml.R rImage = null;
    try {
         if (imageBytes != null) {            
            rImage = runImagePart(wordMLPackage,sourcePart,imageBytes,"QRCode","QRCode",0,1);
    catch (Exception e) {
   R run = factory.createR();
   P paragraph = factory.createP();
   RPr rpr = new RPr();
   HpsMeasure size = new HpsMeasure();
   if(contents!= null) {
     log.debug("contents = "+contents);
     int i=0;
     try(Scanner scan = new Scanner(contents);){
     while (scan.hasNextLine() ){
        String content = scan.nextLine();
            log.debug("content = "+content);
        if(i>0) {
          Br br = factory.createBr();
           Text text = new Text();
   if(rImage != null)
   return footer;

private static void createFooterReference(Relationship relationship,WordprocessingMLPackage wordMLPackage, ObjectFactory factory) {
   List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();
   SectPr sectionProperties = sections.get(sections.size() - 1)
   if (sectionProperties == null) {
        sectionProperties = factory.createSectPr();
        sections.get(sections.size() - 1).setSectPr(sectionProperties);
    List<CTRel> relations = sectionProperties.getEGHdrFtrReferences();
    Iterator<CTRel> relationsItr = relations.iterator();
    while (relationsItr.hasNext()) {
           CTRel relation =;
            if (relation instanceof FooterReference) {
    FooterReference firstPagefooterRef = factory.createFooterReference();

Re: Footer is not always visible if added to docx template

PostPosted: Tue Feb 26, 2019 12:08 am
by alister
Seems like first page footer can not be present alone, only with default footer.
If I change type of footer, it is visible.
Besides TitlePg should be set according to below link. In this case first pager footer is visible alone without default footer.