Page 1 of 1

Create two headers and two footers problems

PostPosted: Fri Feb 28, 2014 8:03 pm
by zhj89901390
I want to create two headers and two footers in a word document, for example in the attached word file.
I tried for a long time but not found a solution.
Can someone kindly give me some help (some code better), thanks a lot!

Re: Create two headers and two footers problems

PostPosted: Fri Feb 28, 2014 11:30 pm
by jason
Headers and footers are connected to the document via the sectPr element (and more specifically the relIds in there).

You can inspect your docx to see how it works ...

You need to create your header and footer parts, and add them to the main document part using addTargetPart. That'll return a relId, which you need to use in the sectPr element.

Upload your docx to the webapp (linked in the menu above), to generate relevant code.

Re: Create two headers and two footers problems

PostPosted: Tue Mar 04, 2014 12:58 pm
by zhj89901390
now I can create two footers by using the following code:
Code: Select all
public static void addFooter(WordprocessingMLPackage wordMLPackage) throws InvalidFormatException, JAXBException {

        FooterPart footerPart = new FooterPart();
        Relationship rel = wordMLPackage.getMainDocumentPart().addTargetPart(footerPart);       
       
        String ftrXml = "<w:ftr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
                + "<w:p w:rsidR=\"00CD3E70\" w:rsidRDefault=\"00ED630A\" w:rsidP=\"00AB75CC\">"
                + "<w:pPr><w:tabs><w:tab w:val=\"right\" w:pos=\"9360\"/></w:tabs><w:rPr><w:lang w:eastAsia=\"zh-CN\"/></w:rPr></w:pPr>"
                + "<w:r><w:t>111</w:t></w:r>"
                + "<w:r w:rsidR=\"00AB75CC\"><w:tab/></w:r>"
                + "<w:r w:rsidR=\"00AB75CC\"><w:rPr><w:rFonts w:hint=\"eastAsia\"/><w:lang w:eastAsia=\"zh-CN\"/></w:rPr><w:t>222</w:t></w:r>"               
                + "</w:p>"                               
                + "</w:ftr>";

        Ftr ftr = (Ftr) XmlUtils.unmarshalString(ftrXml);
        footerPart.setJaxbElement(ftr);

        // Now Create foooter reference
        ObjectFactory objectFactory = new ObjectFactory();
        List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();

        SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
        // There is always a section wrapper, but it might not contain a sectPr
        if (sectPr == null) {
          sectPr = objectFactory.createSectPr();
          wordMLPackage.getMainDocumentPart().addObject(sectPr);
          sections.get(sections.size() - 1).setSectPr(sectPr);
        }

        FooterReference footerReference = objectFactory.createFooterReference();
        footerReference.setId(rel.getId());
        footerReference.setType(HdrFtrRef.DEFAULT);
        sectPr.getEGHdrFtrReferences().add(footerReference);
}


the responding result is :
footers.png
footers.png (878 Bytes) Viewed 1982 times


however, I want to replace the first footer with a logo, e.g:
logo_footer.png
logo_footer.png (2.1 KiB) Viewed 1982 times


how can I modify the above code to get this, thanks!

Re: Create two headers and two footers problems

PostPosted: Tue Mar 04, 2014 5:42 pm
by jason
See https://github.com/plutext/docx4j/blob/ ... geAdd.java
but at line 131 use the method
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
createImagePart(
                        OpcPackage opcPackage,
                        Part sourcePart, byte[] bytes)
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4

setting sourcePart to be your footer part.

Re: Create two headers and two footers problems

PostPosted: Tue Mar 04, 2014 6:35 pm
by zhj89901390
can you give me some detailed code? thanks a lot!

Re: Create two headers and two footers problems

PostPosted: Fri Mar 14, 2014 6:38 pm
by jason
zhj89901390 wrote:can you give me some detailed code? thanks a lot!


No, I have answered your question. Now its up to you.