Page 1 of 1

Header/Footer not coming in word docx file

PostPosted: Thu May 06, 2021 5:38 am
by harish0000
Hi

I am trying to generate the word docx file and I have added headers and footer like below. but these aren't present in actual word file. I have extracted the docx file and relationships are present in _rels/document.xml but aren't in actual document.xml(present in main folder when extracted), rId12 and rId13 doesn't get added in sectpr. Please help

Code: Select all
public static void addPageNumberinHeader(WordprocessingMLPackage wordMLPackage, MainDocumentPart documentPart)
         throws InvalidFormatException {

      HeaderPart headerPart = new HeaderPart();
      Relationship rel = documentPart.addTargetPart(headerPart);
      headerPart.setJaxbElement(getHdr(wordMLPackage, documentPart, headerPart));

      List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();

      for (SectionWrapper section : sections) {
         SectPr sectPr = section.getSectPr();

         HeaderReference headerReference = objectFactory.createHeaderReference();
         headerReference.setId(rel.getId());
         headerReference.setType(HdrFtrRef.DEFAULT);
         sectPr.getEGHdrFtrReferences().add(headerReference);
      }
   }


Code: Select all
private static Ftr getFtr(WordprocessingMLPackage wordMLPackage, MainDocumentPart documentPart,
         FooterPart footerPart, Resource footerImage) throws Exception {

      Ftr ftr = objectFactory.createFtr();
      ftr.getContent().add(newImage(wordMLPackage, footerPart, BufferUtil.getBytesFromInputStream(footerImage.getInputStream()), "filename",
            "alttext", 1, 2));
      return ftr;

   }

   private static Object newImage(WordprocessingMLPackage wordMLPackage, FooterPart footerPart, byte[] bytes,
         String filenameHint, String altText, int id1, int id2) throws Exception {

      BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, footerPart, bytes);

      Inline inline = imagePart.createImageInline(filenameHint, altText, id1, id2, false);

      // Now add the inline in w:p/w:r/w:drawing
      P p = objectFactory.createP();
      R run = objectFactory.createR();
      p.getContent().add(run);
      org.docx4j.wml.Drawing drawing = objectFactory.createDrawing();
      run.getContent().add(drawing);
      drawing.getAnchorOrInline().add(inline);
      return p;
   }


Code: Select all
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
    <Relationship Target="header1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/header" Id="rId8"/>
    <Relationship Target="styles.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Id="rId3"/>
    <Relationship Target="endnotes.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes" Id="rId7"/>
    <Relationship Target="numbering.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering" Id="rId2"/>
    <Relationship Target="../customXml/item1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml" Id="rId1"/>
    <Relationship Target="footnotes.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes" Id="rId6"/>
    <Relationship Target="webSettings.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" Id="rId5"/>
    <Relationship Target="theme/theme1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Id="rId10"/>
    <Relationship Target="settings.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Id="rId4"/>
    <Relationship Target="fontTable.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Id="rId9"/>
    <Relationship Target="media/document_image_rId11.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Id="rId11"/>
    <Relationship Target="header.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/header" Id="rId12"/>
    <Relationship Target="footer.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" Id="rId13"/>
</Relationships>



Code: Select all
<w:sectPr>
            <w:headerReference w:type="default" r:id="rId8"/>
            <w:pgSz w:w="12240" w:h="15840"/>
            <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/>
            <w:cols w:space="720"/>
            <w:docGrid w:linePitch="360"/>
        </w:sectPr>

Re: Header/Footer not coming in word docx file

PostPosted: Thu May 06, 2021 7:29 am
by jason
Some troubleshooting: In your add addPageNumberinHeader method, you could output the rel Id to confirm what is happening

Is the iterator finding all the sectPr elements in the docx? Perhaps you need to refresh() the DocumentModel?

Re: Header/Footer not coming in word docx file

PostPosted: Thu May 06, 2021 2:55 pm
by harish0000
Hi Jason

thanks for the reply. after iterator rId12 gets added to sectPr, but when I do
System.out.println(wordMLPackage.getMainDocumentPart().getXML());

rId12 is not getting printed in sectPr. don't know why but I can confirm rId12 is in the document model but not in mainDoc > jaxBElement > sectPr. Attachment added.
Also I tried after for loop to refresh the document model and then I printed the xml again its not coming.

Re: Header/Footer not coming in word docx file

PostPosted: Thu May 06, 2021 9:22 pm
by harish0000
Hi Jason

getDocumentModel().refresh() works.

thanks for the help.