How to Add Footnotes and Endnotes to a Word Document in Java
Aspose.Words FOSS for Java adds footnotes and endnotes to a Word document with DocumentBuilder.insertFootnote. This guide inserts one of each and reads them back, with the library from Maven Central.
Step-by-Step Guide
Step 1: Install the Package
Add the package to your build. The library needs Java 8 or later.
<dependency>
<groupId>org.aspose</groupId>
<artifactId>aspose-words-foss</artifactId>
<version>26.3.0</version>
</dependency>Step 2: Import the Classes
All classes are in the com.aspose.words package, so the example starts with import com.aspose.words.*;.
Step 3: Insert the Notes
Write the body text, then call insertFootnote with FootnoteType.FOOTNOTE or FootnoteType.ENDNOTE and the text of the note, at the position of the reference mark.
Step 4: Read the Notes Back
getChildNodes(NodeType.FOOTNOTE, true) returns every note. The run found 2 notes and printed the kind and text of each.
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.Common Issues and Fixes
The compiler reports cannot find symbol for Footnote. The com.aspose.words package is not imported. Add import com.aspose.words.*;.
A NOTEREF field shows Error! Bookmark not defined. Updating a NOTEREF field does not work in this edition; the result was always that error in a test on Java 8, 17 and 21.
Frequently Asked Questions
Can I choose the reference mark?
Yes. insertFootnote has an overload that takes a reference mark.
How do I tell a footnote from an endnote?
Footnote.getFootnoteType returns the type.