How do i add footer in word document using java

how do i add footer in word document using java

@zhangqingzhen Please see our documentation to learn how to work with headers/footers:
https://docs.aspose.com/words/java/working-with-headers-and-footers/

Give me some code examples

@zhangqingzhen There are code examples in the documentation. Here is a simple code that creates primary footer in the document:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.write("This is primary footer.");
doc.save("C:\\Temp\\out.docx");

If there is no footer in Word, I need to add a footer to each page in Word, and the content of the footer should be the current page number and the total number of pages

@zhangqingzhen You can use the following code:

Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
for (Section s : doc.getSections())
{
    builder.moveToSection(doc.getSections().indexOf(s));
    builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
    builder.insertField("PAGE");
    builder.write(" of ");
    builder.insertField("NUMPAGES");
}
doc.save("C:\\Temp\\out.docx");