Footer text is being inserted only in first two pages of dot file using Aspose Words for Java API

@alexey.noskov
Hi ,
For one of the dot files, footer text is getting inserted in only first two pages.

Code snippet used

com.aspose.words.DocumentBuilder builder = new com.aspose.words.DocumentBuilder(doc);
builder.moveToHeaderFooter(com.aspose.words.HeaderFooterType.FOOTER_PRIMARY);
builder.write(footerTxt);
doc.save(outfile);

Including the dot file for your reference:

dotfile.zip (134.4 KB)

Thanks
Rama

@Rama_Menon Headers/footers in MS Word document are defined per section. In your document there are 5 section, but code inserts footer only into the first section. To insert the same footer into all sections you should either insert it into each section:

Document doc = new Document("C:\\Temp\\in.dot");
DocumentBuilder builder = new DocumentBuilder(doc);

for (Section s : doc.getSections())
{
    // Move to the section.
    builder.moveToSection(doc.getSections().indexOf(s));
    // Move to footer and put some text
    builder.moveToHeaderFooter(com.aspose.words.HeaderFooterType.FOOTER_PRIMARY);
    builder.write("I am a cool footer");
}

doc.save("C:\\Temp\\out.docx");

or inherit footer from the previous section.

ok.So should we use the same logic in general for dotx,doc,docx file types also,since we wont be knowing whether sections are available or not when processing the document?

@Rama_Menon Yes, this applies for all file formats. Please see our documentation to learn more about Aspose.Words Document Object Model:
https://docs.aspose.com/words/java/aspose-words-document-object-model/

ok thanks.

Was trying to the use the section loop(as you had suggested) for the dot file which I had uploaded earlier.

On 3rd and 7th page of the dot file ,the footer text is not getting inserted.

Any suggestions?

Thanks

@Rama_Menon In MS Word each section can have different headers and footers, also there are three types of headers/footers: First Page, Primary and Even pages. In your code you process only FOOTER_PRIMARY so if the section have different first header/footer, the changes made by the code will not affect the first page footer.

The same applies to even/odd headers/footers.

ok,as I understand, should we use all the three

HeaderFooterType.FOOTER_FIRST
HeaderFooterType.FOOTER_PRIMARY
HeaderFooterType.FOOTER_EVEN

so that footer is printed in all the pages?

@Rama_Menon Yes, to make sure the content is inserted on all pages, you should use all types of the footer.
Alternatively, you can still use only primary footer, but explicitly disable PageSetup.DifferentFirstPageHeaderFooter and PageSetup.OddAndEvenPagesHeaderFooter properties for each section.

Tried the alternative approach as you had suggested.

for (Section s : doc.getSections())
{
    builder.getPageSetup().setDifferentFirstPageHeaderFooter(false);
    builder.getPageSetup().setOddAndEvenPagesHeaderFooter(false);
    // Move to the section.
    builder.moveToSection(doc.getSections().indexOf(s));
    // Move to footer and put some text
    builder.moveToHeaderFooter(com.aspose.words.HeaderFooterType.FOOTER_PRIMARY);

    builder.getFont().setName("Arial");
    builder.getFont().setBold(false);
    builder.getFont().setSize(10);
    builder.write(footerText);
    //builder.write("Date ");
    builder.insertField(" DATE", "");
    builder.write(footerText1);
}

In this case for seventh page it is not inserting the footer for some reason.

@Rama_Menon You should disable the properties for each section. Please modify the code like this:

for (Section s : doc.getSections())
{
    s.getPageSetup().setDifferentFirstPageHeaderFooter(false);
    s.getPageSetup().setOddAndEvenPagesHeaderFooter(false);
    // Move to the section.
    builder.moveToSection(doc.getSections().indexOf(s));
    // Move to footer and put some text
    builder.moveToHeaderFooter(com.aspose.words.HeaderFooterType.FOOTER_PRIMARY);

    builder.getFont().setName("Arial");
    builder.getFont().setBold(false);
    builder.getFont().setSize(10);
    builder.write(footerText);
    //builder.write("Date ");
    builder.insertField(" DATE", "");
    builder.write(footerText1);
}

Thanks,this worked.

1 Like