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");
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");
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.
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.