I was able to make section-level headers and footers work but it required 
copying WXPFHeaderFooterPolicy and changing two lines.

The issue is that these methods:

    private void setFooterReference(Enum type, XWPFHeaderFooter wrapper) {
        CTHdrFtrRef ref = 
doc.getDocument().getBody().getSectPr().addNewFooterReference();
        ref.setType(type);
        ref.setId(doc.getRelationId(wrapper));
    }


    private void setHeaderReference(Enum type, XWPFHeaderFooter wrapper) {
        CTHdrFtrRef ref = 
doc.getDocument().getBody().getSectPr().addNewHeaderReference();
        ref.setType(type);
        ref.setId(doc.getRelationId(wrapper));
    }

Get the sectPr from the document even when a sectPr is provided on the 
constructor, so even if you pass in a section's sectPr, the headers you create 
end up getting associated with the document, not the section.

The fix is simple:

1. Store the sectPr as a global variable in the constructor:

        // WEK: Store the sectPr
        this.sectPr = sectPr;
        this.doc = doc;

2. Use the stored sectPr when setting the references:

    private void setFooterReference(Enum type, XWPFHeaderFooter wrapper) {
      // WEK: Use stored sectPr:
        CTHdrFtrRef ref = sectPr.addNewFooterReference();
        ref.setType(type);
        ref.setId(doc.getRelationId(wrapper));
    }

    private void setHeaderReference(Enum type, XWPFHeaderFooter wrapper) {
      // WEK: Use stored sectPr:
        CTHdrFtrRef ref = sectPr.addNewHeaderReference();
        ref.setType(type);
        ref.setId(doc.getRelationId(wrapper));
    }

With this change in my copy of the class section headers and footers work. I 
added logic in my Wordinator code to add document-level headers to sections 
when the section doesn't explicitly define the headers. This matches what I 
think Word does when you create a new section manually and matches the 
expectation of users of Wordinator.

I can make a pull request for this change if its wanted.

Cheers,

E.
--
Eliot Kimber
http://contrext.com
 

On 6/25/19, 9:22 AM, "Eliot Kimber" <[email protected]> wrote:

    Yeah, I'm still not sure how a section object would work because of the way 
that they get translated to Word objects under the covers.
    
    What I have working at the moment is correct generation of section breaks 
and page number controls but I haven't worked out how to set up the headers and 
footers--I think it should be a matter of adding the header or footer 
definition to the document and then adding references to them on the CTSectPr 
for the section, but I haven't tried it yet.
    
    I think the correct behavior is to have sections use the default header and 
footer if the section doesn't specify it's own (that's certainly what I would 
expect), but I don't know if that's how Word actually works.
    
    Cheers,
    
    E.
    
    --
    Eliot Kimber
    http://contrext.com
     
    
    On 6/24/19, 3:16 PM, "Mark Murphy" <[email protected]> wrote:
    
        I don't think there is anyone working on sections. But, I would want a
        section class that is used by the document class much like headers and
        footers. The document object would need to be able to create, retrieve,
        move, and remove sections. In addition, it might be nice to be able to 
mark
        a particular section as the default section. That's really not the way 
that
        Word does it, so that functionality would have to be thought about as 
far
        as how it would work. In Word, the last section is the default section. 
And
        other than the default section, section information is kept in the last
        paragraph in the section. That paragraph can be otherwise empty.
        
        On Sat, Jun 22, 2019 at 2:31 PM Eliot Kimber <[email protected]> 
wrote:
        
        > I need to implement generation of sections in DOCX. Looking at the 
code in
        > trunk I see some code that reads or makes CTSectPr objects (e.g.,
        > XWPFDocument#createFooter) but it doesn't look like full section 
support is
        > there.
        >
        > I would expect an XWPFSection or XWPFSectPR class for working with
        > sections, and maybe methods on XWPFDocument for creating or setting 
them,
        > although I haven't worked out my exact API needs yet.
        >
        > Is anyone actively working on more complete section support or is 
there
        > any design work I could use to take this forward?
        >
        > I'll implement some ad-hoc section creation stuff in my Wordinator 
code
        > but I'd like to fold anything I come up with back into the XWPF API.
        >
        > Cheers,
        >
        > E.
        > --
        > Eliot Kimber
        > http://contrext.com
        >
        >
        >
        >
        > ---------------------------------------------------------------------
        > To unsubscribe, e-mail: [email protected]
        > For additional commands, e-mail: [email protected]
        >
        >
        
    
    
    
    ---------------------------------------------------------------------
    To unsubscribe, e-mail: [email protected]
    For additional commands, e-mail: [email protected]
    
    
    



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to