Aspose.Words Footnote on every page

Is it possible to add a footnote on every page of a word document?

@jodeenm Sure, you can insert Footnote on each page. You can use DocumentBuilder.InsertFootnote method to insert footnote into the document. Please see the documentation for more information.
Since MS Word document is flow document it does not have a concept of Page, so to detect where page starts you should use LayoutCollector class.
Here is a simple code:

Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
LayoutCollector collector = new LayoutCollector(doc);

NodeCollection runs = doc.GetChildNodes(NodeType.Run, true);
int currentPageNumber = -1;
foreach (Run r in runs)
{
    // footnotes allowed to be inserted in the main body.
    if (r.GetAncestor(NodeType.Body) == null)
        continue;

    int pageNumber = collector.GetStartPageIndex(r);
    if (pageNumber != currentPageNumber)
    {
        // Move document builder to the run.
        builder.MoveTo(r);
        // Insert footnote
        builder.InsertFootnote(FootnoteType.Footnote, string.Format("Footnote on {0} page", pageNumber));
        currentPageNumber = pageNumber;
    }
}

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

Fantastic! thank you!

1 Like