Page 1 of 1

Create 2 footers

PostPosted: Thu Sep 01, 2011 6:28 am
by billbruyn
I'm sure it should be obvious to me when I look at docs or samples, but I don't see how I can use the API to add a footer to all pages except the first.

I'd appreciate it if someone could point me in the right direction.

Re: Create 2 footers

PostPosted: Thu Sep 01, 2011 10:32 am
by jason
You'll need a first page footer (HdrFtrRef.FIRST) that is empty, and another footer for the rest (HdrFtrRef.DEFAULT).

You can adapt the HeaderFooterCreate sample.

Re: Create 2 footers

PostPosted: Thu Sep 01, 2011 1:11 pm
by billbruyn
Thanks Jason. I actually was trying to adapt the HeaderFooterCreate example, but the thing that isn't clear is how I create both footers.

It looks like you only get one HeaderPart, and one Hdr per part. I don't see how to add a Hdr without another HeaderPart.

So here's some code borrowed from the sample (which of course works) to add the first header

Code: Select all
HeaderPart header1 = new HeaderPart();
header1.setJaxbElement(getHdr());
      
Relationship rel1 = wordprocessingMLPackage.getMainDocumentPart().addTargetPart(header1);
      
HeaderReference headerReference1 = objectFactory.createHeaderReference();
headerReference1.setId(rel1.getId());
headerReference1.setType(HdrFtrRef.DEFAULT);
      
//add header or footer references
sectPr.getEGHdrFtrReferences().add(headerReference1);


I cannot follow that with this

Code: Select all
HeaderPart header2= new HeaderPart();
header2.setJaxbElement(objectFactory.createHdr());
      
Relationship rel2 = wordprocessingMLPackage.getMainDocumentPart().addTargetPart(header2);

HeaderReference headerReference2 = objectFactory.createHeaderReference();
headerReference2.setId(rel2.getId());
headerReference2.setType(HdrFtrRef.FIRST);
      
//add header or footer references
sectPr.getEGHdrFtrReferences().add(headerReference2);

Re: Create 2 footers

PostPosted: Thu Sep 01, 2011 11:31 pm
by jason
You'll need to provide a part name for each of the 2 parts:

Code: Select all
       HeaderPart header1 = new HeaderPart(new PartName("/word/header1.xml"));
       HeaderPart header2 = new HeaderPart(new PartName("/word/header2.xml"));


The no-arg constructor uses part name /word/header.xml. Admittedly it would be easier if docx4j generated new part names automatically as needed.

Re: Create 2 footers

PostPosted: Fri Sep 02, 2011 5:12 am
by billbruyn
Ah, yes. That got it, thanks. Well, that and

Code: Select all
sectPr.setTitlePg(new BooleanDefaultTrue());


Seeing it now, I guess I should have been able to figure that out. Appreciate the help.


Bill

Re: Create 2 footers

PostPosted: Fri Sep 02, 2011 6:24 pm
by jason
OK, post http://www.docx4java.org/trac/docx4j/changeset/1649
it will automatically allocate a new part name if you call addTargetPart with parameter AddPartBehaviour.RENAME_IF_NAME_EXISTS.
The default is OVERWRITE_IF_NAME_EXISTS:- overwrite any existing part of the same name.