I have a dynamic word document already** with different headers and footers. It can have as many pages as the user wants making it a dynamic document. I want the last page of that document alone to have a separate footer which I am not able to achieve. Please let me know the possibilities.
Thanks for your inquiry. There is no direct way to define header/footer of the last page in MS Word documents. However, there are two workarounds of this issue.
Use separate section of the last page. In this case, you can specify header/footer for the each section.
Please use the
LayoutCollector.getStartPageIndex method to get 1-based index of the
page where node begins. Please find the last node of second last page of document using getStartPageIndex method and insert section break of type new page at the position of that node.
Use IF field conditions to display different text on the last page. For example see the following code.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Build document.
// Just for testing add few pages.
for (int i = 0; i < 10; i++)
builder.insertBreak(BreakType.PAGE_BREAK);
// Move DocumentBuilder cursor to the primary footer.
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
// There is no direct way to have different headers/footers for the last page.
// However, we can achieve this using IF field.
// In our case, IF field code will look like the following:
// { IF "{PAGE}" = "{NUMPAGES}" "This is last page" "This is primary footer" }
// Insert the first part of the IF field
Field field = builder.insertField("IF \"", null);
// Move DocumentBuilder cursor to the next run after field start.
builder.moveTo(field.getStart().getNextSibling().getNextSibling());
// Insert PAGE field.
builder.insertField("PAGE", null);
// Insert operator.
builder.write("\" = \"");
// Insert NUMPAGES field.
builder.insertField("NUMPAGES", null);
// Insert true/false values.
builder.write("\" \"This is last page\" \"This is primary footer\"");
// Save output document.
doc.save(MyDir + "out.docx");
Hi,
Thank you so much for the reply. It was actually working well. And now when I restarted numbering in that particular section, the last page of the section doesn’t equal the total number of pages. So the last page footer isn’t displayed. Can you help with this?
Thanks for your inquiry.
In this case, I suggest you please use separate section of the last page. In this case, you can specify header/footer for the each section.
If you still face problem, please share following detail for investigation purposes.
Please attach your input Word document.
Please attach the output Word file that shows the undesired behavior.
Please
attach your target Word document showing the desired behavior. You can
use Microsoft Word to create your target Word document. I will
investigate as to how you are expecting your final document be generated
like.
As soon as you get these pieces of information to
us we’ll start our investigation into your issue.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.