Page 1 of 1

Build 3 headers but none show

PostPosted: Mon Jun 10, 2019 8:54 am
by olddave

I used the demo webapp to show the code required for my document. I am substituting in values from a data model and building the 4 different types of pages I need as I go.

But I cannot get the 3 headers (and 3 footers) from the demo webapp to display in the Word document produced.

I have this code to call my header and footer create methods:

Code: Select all
   public Body createIt(Body body) {

      // Create object for sectPr
      SectPr sectpr = wmlObjectFactory.createSectPr();
      try {
      } catch (IOException ioe) {
      } catch (Exception e) {


The header and footer create methods are much the same so I'll only show the header code here.

Code: Select all
   public void createHeader(int version) throws IOException, Exception {
      // Create object for headerReference1
      HeaderPart headerPart = null;
      Relationship rel = null;
      try {
         headerPart = new HeaderPart();
         rel = titlePage.wordMLPackage.getMainDocumentPart().addTargetPart(headerPart);
         // After addTargetPart, so image can be added properly
         Hdr hdr = null;
         if (version == 1) {
            Header1 hdr1 = new Header1();
            hdr = hdr1.createIt(wmlObjectFactory);
         } else if (version == 2) {
            Header2 hdr2 = new Header2();
            hdr = hdr2.createIt(wmlObjectFactory);
         } else if (version == 3) {
            Header3 hdr3 = new Header3();
            hdr = hdr3.createIt(wmlObjectFactory);
 is = TitlePage.class.getResourceAsStream("/headerimg.png");;
                      "filename", "alttext", 1, 2
         List<SectionWrapper> sections = titlePage.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 = wmlObjectFactory.createSectPr();
            sections.get(sections.size() - 1).setSectPr(sectPr);

         HeaderReference headerReference = wmlObjectFactory.createHeaderReference();
      } catch (InvalidFormatException e) {
         // TODO Auto-generated catch block

The 3 Header1..3 classes are as generated by the demo webapp for the headers.

I trace thru debug and see there is only one header relationship in
Code: Select all
array. I assume it is getting replaced each time I call the createHeader method? I feel I am close but cannot see the missing link.

Thank you

Re: Build 3 headers but none show

PostPosted: Tue Jun 11, 2019 6:33 am
by jason
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
        public Relationship addTargetPart(Part targetpart) throws InvalidFormatException {
                return this.addTargetPart(targetpart, AddPartBehaviour.OVERWRITE_IF_NAME_EXISTS, null);
Parsed in 0.013 seconds, using GeSHi

You want addTargetPart(headerPart, AddPartBehaviour.RENAME_IF_NAME_EXISTS);

If you continue to have problems, please post your output docx.

Re: Build 3 headers but none show

PostPosted: Tue Jun 11, 2019 8:47 am
by olddave
Here is the output, no headers or footer showing yet. The rest of the content needs work still, but one step at a time..

Re: Build 3 headers but none show

PostPosted: Wed Jun 12, 2019 10:22 pm
by jason
Unzipping your docx, there are no header or footer parts in it. and no header/footer references in the sectPr at the end of the document body.

You create a sectPr:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
// Create object for sectPr
      SectPr sectpr = wmlObjectFactory.createSectPr();
Parsed in 0.013 seconds, using GeSHi

so you should pass that sectPr to your createHeader function.

Without your complete code it is hard to see what is going on. Are you saving the docx you think you are saving?

Re: Build 3 headers but none show

PostPosted: Sun Jun 16, 2019 6:57 pm
by olddave
That solved the problem, I was creating a SectPr in isolation. But now have 3 headers on each page. What makes this so difficult is the sheer amount of code that is generated by the converter. So I have started from scratch and am substituting strings into the xml string produced by the conversion web application. I'll see how it goes.