The question is published on by Tutorial Guruji team.
I’m generating a docx file using Apache POI 3.13 and I stuck with headers/footers for first page.
I create XMPFParagraph[]
without any problem. Next I create headers and footers like this (I’ve tried in different oreder):
policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, defaultHeader); policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, defaultFooter); policy.createHeader(XWPFHeaderFooterPolicy.FIRST, firstHeader); policy.createFooter(XWPFHeaderFooterPolicy.FIRST, firstFooter);
Once I generate my docx file I could see my default header/footer on every page including first one. But if I select to use different header/footer for the first page – my first header and footer apperes correctly. How could I make this happens automaticaly via code? And is there any appropriate documentation with examples about POI?
Answer
If you want to set a first page header in a section, you must enter a title page tag in section properties tag (w:sectPr). The title page tag can be empty, but it is necessary. In your case, you can add only 2 code lines:
CTSectPr sect = document.getDocument().getBody().getSectPr(); sect.addNewTitlePg();
`Best regards!