We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Need to get last page footer of docx file

Hi Team -

In DOCX file (which is modified by ASPOSE word api) i have a page number in footer and other fixed content as well, when i try to get the last page footer of that file with below code I always get the first-page footer.

After that I have opened the same file in MS word 2010 and added a new line after the last paragraph and tried to get the last page footer with the same code, in that case, I am getting last page footer.

Attached herewith is the test document. (30.6 KB)

Can you please check and help me. Thanks!

public static String getLastPageFooter(String docPath) throws Exception
{
String content = “”;
Document doc = new Document(docPath);
SectionCollection sections = doc.getSections();
for (Section section : sections)
{
HeaderFooterCollection headerFooterCollection = section.getHeadersFooters();
HeaderFooter headerFooter= headerFooterCollection.getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);

  	ParagraphCollection paragraphs = headerFooter.getParagraphs();
  	for (Paragraph paragraph : paragraphs)
  	{
  		RunCollection runs = paragraph.getRuns();
  		for (Run run : runs)
  		{
  			content += run.getText();
  		}
  		content += "\n";
  	}
  }
  doc.removeExternalSchemaReferences();
  return content;

}

@kevalp,

Thanks for your inquiry. Your document contains only one section that has primary footer. Please call Document.updatefields to get the desired output. You can get the last section of document using Document.LastSection property.

Please check the following modified code. Hope this helps you.

public static String getLastPageFooter(String docPath) throws Exception
{
    String content = "";
    Document doc = new Document(docPath);
    doc.updateFields();
    HeaderFooterCollection headerFooterCollection = doc.getLastSection().getHeadersFooters();
    HeaderFooter headerFooter= headerFooterCollection.getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);

    ParagraphCollection paragraphs = headerFooter.getParagraphs();
    for (Paragraph paragraph : paragraphs)
    {
        RunCollection runs = paragraph.getRuns();
        for (Run run : runs)
        {
            content += run.getText();
        }
        content += "\n";
    }

    doc.removeExternalSchemaReferences();
    return content;
}

Thanks! Tahir

It works as expected.

@kevalp,

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.