Hi Aspose Team,
We Are having a requirement in one of our project like need to append the footer to word document and need to set the page numbers. if the document already contains the footer we need to update the footer in the same location without disturbing the page number section. if the document doesn’t contains any footer we need to append the footer and page numbers. for better understanding i am attaching the document how we need exactly. and finally one more thing the footer and page numbers need to be set for same location for all the pages.
Thanks
Ravikanth Vuppala
Hi Ravikanth,
Thanks for your inquiry. Please read following documentation links for your kind reference.
https://docs.aspose.com/words/java/working-with-headers-and-footers/
Use the following code snippet to achieve your requirements. Hope this helps you. Please let us know if you have any more queries.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Set font properties for header text.
builder.getFont().setName("Arial");
builder.getFont().setBold(true);
builder.getFont().setSize(14);
Section section = builder.getCurrentSection();
HeaderFooter header = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
if (header == null)
{
// There is no header of the specified type in the current section, create it.
header = new HeaderFooter(doc, HeaderFooterType.FOOTER_PRIMARY);
section.getHeadersFooters().add(header);
}
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("Footer Text ");
// Insert page numbering text here.
// It uses PAGE and NUMPAGES fields to auto calculate current page number and total number of pages.
builder.write("Page ");
builder.insertField("PAGE", "");
builder.write(" of ");
builder.insertField("NUMPAGES", "");
builder.moveToSection(0);
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
// Save the resulting document.
doc.save(MyDir + "Out.docx");