Page 1 of 1

"Correct" way to make first page header different

PostPosted: Tue Jul 31, 2012 5:06 am
by cjohnson
I've looked through the forums, and the way that I could see to make the first page header/footer different from the rest of the document is no longer valid as "getHeaderFooterPolicy()" is now deprecated, and I don't see what replaced it.

What is the new correct way to do this?

Re: "Correct" way to make first page header different

PostPosted: Tue Jul 31, 2012 5:12 am
by cjohnson
Nevermind, it seems to be

java.util.List<SectionWrapper> sections = docx.getDocumentModel().getSections();

HeaderFooterPolicy hfp = sections.get(sections.size() - 1).getHeaderFooterPolicy();

Is there a way to edit / delete posts? I feel a bit foolish for asking this. :D

Re: "Correct" way to make first page header different

PostPosted: Tue Jul 31, 2012 6:20 am
by cjohnson
Hi Jason,

I'm actually having a bit of a problem figuring this out: I want a combination footer that has page numbers and a custom label dependent on the page, e.g.

pagenumber ---- someClass.foo(pagenumber).

Except that I don't want the page number on the first page, just someClass.foo(pagenumber) so that the page numbering will start on page 2.

Is this even possible to do? Or am I going to have to do some hacky work-around?


On a side note, is docx4j looking for people to work on the project? I'm kind of done with my other open source project, and this looks like a great project to work on during this fall. Is there someone I should contact about this?

Re: "Correct" way to make first page header different

PostPosted: Tue Jul 31, 2012 9:26 am
by jason
You can have different headers/footers on odd/even pages, or first/other pages, on a per section basis. Sounds like you want to start with first/other pages.

So one way would be to make extra sections in your docx (a section per each value of someClass.foo), but that's probably an unnatural hacky workaround (it depends on fixed page breaks).

The other way would be to use Open XML fields to implement your someClass.foo. Macros could even help. It depends a bit on how the docx you create with docx4j is to be used... will it be opened in Word, or are you converting to PDF, or something else?

Yes, we're always happy to have people contributing to docx4j. You can get started by cloning it on GitHub, then you just make pull requests with your patches. If you'd like ideas for what to work on, either post a new topic here, or send me an email :-)

Re: "Correct" way to make first page header different

PostPosted: Wed Aug 01, 2012 12:55 am
by cjohnson
Yes, it will only be in Word. We have a separate xslfo system in place so I can't imagine this will ever put printed as a pdf.

Is there a method to get how many pages long the current document is, or is that really only figured out at run time?

Re: "Correct" way to make first page header different

PostPosted: Wed Aug 01, 2012 3:22 am
by cjohnson
For the sake of brevity, say I just want the first page blank. Should it not be as simple as this? I'm testing to make sure getFirstFooter() and getHeaderFooterPolicy aren't throwing null and the function doesn't throw anything, but I never set hfp.firstHeaderActive (which is what hfp.getFirstFooter() returns) in hfp.setHeaderReferences() via the constructor because I never call the constructor for it, I just assume it exists...what exactly am I doing wrong here? Making a header and footer otherwise works fine.
Code: Select all

   void thisFunctionShouldWorkButItDoesnt(){

        java.util.List<SectionWrapper> sections = docx.getDocumentModel().getSections();
        HeaderFooterPolicy hfp = sections.get(sections.size() - 1).getHeaderFooterPolicy();
        FooterPart footerPart = hfp.getFirstFooter();
        Relationship rel = docx.getMainDocumentPart().addTargetPart(footerPart);
        footerPart.setJaxbElement( factory.createFtr() );

        FooterReference ft = factory.createFooterReference();
        ft.setType(HdrFtrRef.DEFAULT);
        ft.setId(relationship.getId());
        getSectPr().getEGHdrFtrReferences().add(ft);
}


    SectPr getSectPr() {
        java.util.List<SectionWrapper> sections = docx.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 = factory.createSectPr();
            sections.get(sections.size() - 1).setSectPr(sectPr);
            docx.getMainDocumentPart().addObject(sectPr);
        }
        return sectPr;
    }

Re: "Correct" way to make first page header different

PostPosted: Wed Aug 01, 2012 8:54 am
by jason
If you want no header/footer on the first page, you create a first page header/footer that is empty. This is how Word does it, iirc.

docx4j doesn't know how many pages your docx is, but see http://word.tips.net/T001246_Inserting_ ... ument.html

You can probably insert that as a bound content control, if you want. There may be a building block in Word which does this for you.

Or you can do it the way you do it in xsl fo: create a bookmark on the last page, and insert a cross reference to that.

Re: "Correct" way to make first page header different

PostPosted: Fri Aug 03, 2012 2:20 am
by cjohnson
Code: Select all
java.util.List<SectionWrapper> sections = docx.getDocumentModel().getSections();
        HeaderFooterPolicy hfp = sections.get(sections.size() - 1).getHeaderFooterPolicy();


Hi Jason,

getHeaderFooterPolicy always returns null. I can't create a new one because it is in the protected constructor of SectionWrapper. This might not be necessary if there were a different way to get the first header/footer than hfp.getFirstFooter(), but I don't see anything obvious.

Re: "Correct" way to make first page header different

PostPosted: Fri Aug 03, 2012 3:07 am
by jason
Just create them directly, as per the HeaderFooterCreate sample.

If you want to see your additions in the model use wmlPkg.getDocumentModel().refresh()