Problem inserting footer

Hello,

We are merging several word documents on the fly and trying to insert current date as footer. The issue we’re facing is that the footer is only visible on the first page of the document. I’m using the following code…

Please suggest.

Document doc = new Document(location + "\\Print.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

builder.getFont().setName("Arial");
builder.getFont().setBold(true);
builder.getFont().setSize(14);
Section section = builder.getCurrentSection();
HeaderFooter footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
if (footer == null)
{
    footer = new HeaderFooter(doc, HeaderFooterType.FOOTER_PRIMARY);
    section.getHeadersFooters().add(footer);
}

builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.write("PRODUCTION : 04/29/2014");

Hi Luckie,

Thanks for your inquiry.

In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v14.3.0) from here and let us know how it goes on your side. If the problem still remains, please attach your input Word document here for testing. I will investigate the issue on my side and provide you more information.

Please use
HeaderFooterCollection.LinkToPrevious method to link all headers and
footers to the corresponding headers and footers in the previous section
as shown in following highlighted code snippet.

Document doc = new Document(MyDir + "Print.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setName("Arial");
builder.getFont().setBold(true);
builder.getFont().setSize(14);
Section section = builder.getCurrentSection();
HeaderFooter footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
if (footer == null)
{
    footer = new HeaderFooter(doc, HeaderFooterType.FOOTER_PRIMARY);
    section.getHeadersFooters().add(footer);
}
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.write("PRODUCTION : 04/29/2014");
for (Section sec : doc.getSections())
{
    if (sec.equals(doc.getFirstSection()))
        continue;
    sec.getHeadersFooters().linkToPrevious(true);
}
doc.save(MyDir + "Out.docx");

Hello Tahir,

Thank you for the quick response. The solution you provided worked as expected.
Also, I’m in process of upgrading to the latest version.

I’m currently using the following code snippet to first remove the existing footer (page numbers in my case) and insert the desired one. Is there way using which I can edit the existing footer instead.

if (footer != null)
{
    footer.remove();
}

Thanks,
Luckie

Hi Luckie,

Thanks for your inquiry.

Please use the HeaderFooter.removeAllChildren method to remove all the child nodes of header/footer. After removing the all child nodes of HeaderFooter, you can insert new contents according to your requirements. You may also modify the existing contents inside the HeaderFooter.

Please attach your input and expected output Word document here for our reference. We will investigate as to how you want your final Word output be generated like. We will then provide you more information on this along with code.

HeaderFooter footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
footer.removeAllChildren();

Hi Tahir,

Thank you the reply. Please find the attached input and desired output documents for your reference.

Thanks,
Luckie

Hi Luckie,
Thanks for the documents. We are investigating the issue and will share more details soon.
Best Regards,

Hi Luckie,

Thanks for sharing the documents. You can insert new contents into the header/footer of a section or modify the existing contents. Please read following documentation links for your kind reference.
https://docs.aspose.com/words/java/document-builder-overview/
https://docs.aspose.com/words/java/use-documentbuilder-to-insert-document-elements/
https://docs.aspose.com/words/java/navigation-with-cursor/

Please
use the following code example to achieve your requirements. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "TestBefore.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
Section section = doc.getSections().get(0);
HeaderFooter header = doc.getSections().get(0).getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
if (header == null)
{
    header = new HeaderFooter(doc, HeaderFooterType.HEADER_PRIMARY);
    section.getHeadersFooters().add(header);
}
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("Document Name");
HeaderFooter footer = doc.getSections().get(0).getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
if (footer == null)
{
    footer = new HeaderFooter(doc, HeaderFooterType.FOOTER_PRIMARY);
    section.getHeadersFooters().add(footer);
}
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
// builder.moveTo(footer.getFirstParagraph());
// builder.write("PRODUCTION : 05/02/2014");
// You may also remove the current contents of footer and insert new contents.
footer.getFirstParagraph().removeAllChildren();
footer.getFirstParagraph().appendChild(new Run(doc, "Document Name PRODUCTION : 05/02/2014 1"));
doc.save(MyDir + "Out.docx");