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
| Requirement | Detail |
|---|---|
| Java | 8 or later |
| Dependency | The package from Installation |
| Import | import com.aspose.words.*; |
| Code | The 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
insertFootnotealso has an overload that takes a custom reference mark as a third argument.- Pass
trueas the second argument ofgetChildNodesso that notes in nested content are found too. Footnote.getFootnoteTypetells a footnote from an endnote.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
| 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 / Method | Description |
|---|---|
Footnote | Represents a container for text of a footnote or endnote. |
DocumentBuilder.insertFootnote(type, text) | Inserts a footnote or endnote into the document. |
FootnoteType | Specifies whether this is a footnote or an endnote. |
Footnote.getFootnoteType() | Returns a value that specifies whether this is a footnote or endnote. |
Related Guides
- Bookmarks
- Fields
- Working with Text
- How to Add Footnotes and Endnotes to a Word Document in Java (knowledge base)