Footnotes and Endnotes

Overview

This guide shows how to insert footnotes and endnotes into a Word document and read them back with Aspose.Words FOSS for Java. A footnote or an endnote is a container of text that is tied to a reference mark in the body of a Word document. DocumentBuilder.insertFootnote inserts one at the current position; FootnoteType.FOOTNOTE makes a footnote and FootnoteType.ENDNOTE an endnote. Footnote.getFootnoteType returns which of the two a Footnote node is, and the notes of a document are found with getChildNodes(NodeType.FOOTNOTE, true). The example was compiled and run against the published package on Java 8, 17 and 21.


Prerequisites

RequirementDetail
Java8 or later
DependencyThe package from Installation
Importimport com.aspose.words.*;
CodeThe examples are statements. Put them in a method that declares throws Exception.

Insert a Footnote and an Endnote and Read Them Back

Write some text, call insertFootnote with the note type and its text, and continue writing. The example then asks the document for all nodes of type NodeType.FOOTNOTE and prints the kind and the text of each note. It found 2 notes, one footnote and one endnote.

import com.aspose.words.*;

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.write("Aspose.Words FOSS for Java reads and writes DOCX.");
builder.insertFootnote(FootnoteType.FOOTNOTE, "The package is published on Maven Central.");
builder.write(" It also converts Markdown.");
builder.insertFootnote(FootnoteType.ENDNOTE, "Markdown files are recognised by the .md extension.");

NodeCollection notes = doc.getChildNodes(NodeType.FOOTNOTE, true);
System.out.println("Notes in the document: " + notes.getCount());
for (int i = 0; i < notes.getCount(); i++) {
    Footnote note = (Footnote) notes.get(i);
    String kind = note.getFootnoteType() == FootnoteType.FOOTNOTE ? "footnote" : "endnote";
    System.out.println(kind + ": " + note.getText().trim());
}

The program printed:

Notes in the document: 2
footnote: The package is published on Maven Central.
endnote: Markdown files are recognised by the .md extension.

Tips and Best Practices

  • insertFootnote also has an overload that takes a custom reference mark as a third argument.
  • Pass true as the second argument of getChildNodes so that notes in nested content are found too.
  • Footnote.getFootnoteType tells a footnote from an endnote.

Common Issues

IssueCauseFix
The compiler reports cannot find symbol for Document.The com.aspose.words package is not imported.Add import com.aspose.words.*;.
The compiler reports an unreported exception.The Document constructors, Document.save and some other methods declare throws Exception.Declare it on the calling method or catch it.
A NOTEREF field to a footnote shows Error! Bookmark not defined.Updating a NOTEREF field does not work in this edition. In a test on Java 8, 17 and 21 the result was always that error, even for a bookmark that wraps a footnote reference.Do not rely on NOTEREF; write the reference text yourself.

FAQ

What is the difference between a footnote and an endnote?

Footnote.getFootnoteType returns a value that specifies whether the node is a footnote or an endnote; both are created with insertFootnote.

Can I set my own reference mark?

Yes, through the insertFootnote overload that takes a reference mark, and Footnote.setReferenceMark.


API Reference Summary

Class / MethodDescription
FootnoteRepresents a container for text of a footnote or endnote.
DocumentBuilder.insertFootnote(type, text)Inserts a footnote or endnote into the document.
FootnoteTypeSpecifies whether this is a footnote or an endnote.
Footnote.getFootnoteType()Returns a value that specifies whether this is a footnote or endnote.

Related Guides

See Also