I am trying to concatenate difference document (Docx) with Apache POIFS, but can't success defining the appropriate header on each section.
My first page is displayed with its own header, then I have only one header for all other pages.
And just can't succeed in finding how to have different headers for each Section.
Please, if someone knows how to do this...
Here is my code, which simply concat all my additional headers to the original one (at the end, I have only one header, with all values of all my different original headers concatenate in it).
private void appendHeader(final XWPFDocument destDocx, final XWPFDocument additionalDocx) throws IOException, XmlException {
////// ==> No matter how I create the policy, the result is the same
// final XWPFHeaderFooterPolicy destPolicy = destDocx.createHeaderFooterPolicy();
final XWPFHeaderFooterPolicy destPolicy = new XWPFHeaderFooterPolicy(destDocx);
final List<XWPFHeader> destHeaders = destDocx.getHeaderList();
LOGGER.trace("Dest header size: {}", destHeaders.size());
LOGGER.trace("Additional header size: {}", additionalDocx.getHeaderList().size());
// Loop on additional headers to add them to the dest doc.
for (final XWPFHeader additionaHeader : additionalDocx.getHeaderList()) {
// Get the new header I want for this section
final String additionalXmlHeader = additionaHeader._getHdrFtr().xmlText();
// Format it properly
final CTHdrFtr newHeader = CTHdrFtr.Factory.parse(additionalXmlHeader);
// And add it to the document
////// ==> No matter how I set my header...
// destHeader.setHeaderFooter(newHeader);
final XWPFHeader destHeader = new XWPFHeader(destDocx, newHeader);
}
// This simply increases each time I'm adding a new header while I would like to have specific header for each section of the document
LOGGER.trace("New dest header size: {}", destHeaders.size());
}
Cheers.
Olivier
This will be difficult as POI does not yet support creation of multiple sections, and it also does not support adding headers and footers to any section other than the default section (which happens to be the last section in the document). You might be able to do what you want using the CT classes, but you are going to have to research how sections work as I haven't yet worked out all the various requirements.
Here's what needs to happen if you are going to use CT classes. You need to add a paragraph at each section break. Then you need to create a section properties element in that paragraph. BTW, the default section can be found at the end of the document body without a paragraph wrapper, this is the default section, and it encompasses everything before it up to the previous section paragraph. So section properties are found at the end of the section rather than at the beginning as you would expect. And all section properties except the last section are contained in a paragraph. And the last section is the default section. Weird isn't it? Now inside the section that you want to have special headings you need to add header/footer relations. Look at the last section to see what that looks like. You can replicate it. Then the hard part is you will have to manually create a new header part for those relations because If you just create a new default or first page header using the existing methods, it will return the current header or footer for the default section (creating one if it doesn't already exist). This means you will have to go through the create header code and generalize that to create the part and insert the relations into the section you want the header/footer for.
Now if all you want is to have a different header on the first page of your document, you can do that with POI because it doesn't require multiple sections. Here is how you would go about that.
XWPFDocument doc = new XWPFDocument();
XWPFParagraph p = doc.createParagraph();
XWPFRun r = p.createRun();
r.setText("Some Text");
r.setBold(true);
r = p.createRun();
r.setText("Goodbye");
// create header/footer functions insert an empty paragraph
XWPFHeader head = doc.createHeader(HeaderFooterType.FIRST);
head.createParagraph().createRun().setText("First page header");
XWPFFooter foot = doc.createFooter(HeaderFooterType.FIRST);
foot.createParagraph().createRun().setText("First page footer");
// create header/footer functions insert an empty paragraph
XWPFHeader head = doc.createHeader(HeaderFooterType.DEFAULT);
head.createParagraph().createRun().setText("header");
XWPFFooter foot = doc.createFooter(HeaderFooterType.DEFAULT);
foot.createParagraph().createRun().setText("footer");
OutputStream os = new FileOutputStream(new File("header2.docx"));
doc.write(os);
doc.close();
Even/odd page headers and footers are also not yet supported (as of 3.16 beta 1) for creation even though the enum exists.