Footer in the LAST PAGE of a Dynamic Document alone

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 in advance
Jacob

Hi Jacob,

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.

  1. Use separate section of the last page. In this case, you can specify header/footer for the each section.

Aspose.Words uses our own Rendering Engine to layout documents into pages. The Aspose.Words.Layout namespace provides
classes that allow to access information such as on what page and where
on a page particular document elements are positioned, when the document
is formatted into pages. Please read about LayoutCollector and
LayoutEnumerator from here:
https://reference.aspose.com/words/java/com.aspose.words/LayoutCollector
https://reference.aspose.com/words/java/com.aspose.words/layoutenumerator

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.

  1. 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 in advance,
Jacob

Hi Jacob,

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.