Footnotes and endnotes are useful when a Word document needs references or additional explanations without interrupting the main text. You might use them to cite a source, explain a technical term, or provide background information.
Footnotes typically appear at the bottom of a page, while endnotes are collected at the end of a document or section.
With Java, you can add these notes to specific locations in a Word document, read their contents, and remove notes that are no longer needed.
1. Set Up the Java Project
First, add a Word document processing library to your Java project.
For Maven projects, include the following repository and dependency in pom.xml:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>14.10.6</version>
</dependency>
</dependencies>
We'll use an existing Word document named report.docx for the insertion examples. Each program saves its result as a separate DOCX file, leaving the original unchanged.
2. Add a Footnote After Specific Text in Word
Suppose a report contains this sentence:
The emissions accounting process follows internationally recognized reporting standards.
You want to add a footnote immediately after "emissions accounting" to explain the calculation method.
To do this, first locate the phrase using Document.findString(). Then retrieve its paragraph, create a footnote, and position the footnote reference mark after the matching text.
Here's the complete Java code:
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
public class AddWordFootnote {
public static void main(String[] args) {
Document document = new Document();
try {
// Load the Word document
document.loadFromFile("report.docx");
// Find the target text
TextSelection selection =
document.findString(
"emissions accounting", false, false);
if (selection == null) {
throw new IllegalArgumentException(
"Target text not found.");
}
// Get the matching text and its paragraph
TextRange textRange =
selection.getAsOneRange();
Paragraph paragraph =
textRange.getOwnerParagraph();
// Find the position of the text range
int index = paragraph.getChildObjects()
.indexOf(textRange);
// Create a footnote
Footnote footnote =
paragraph.appendFootnote(
FootnoteType.Footnote);
// Position the reference mark after the text
paragraph.getChildObjects()
.insert(index + 1, footnote);
// Add the footnote content
TextRange noteText = footnote.getTextBody()
.addParagraph()
.appendText(
"Emissions are calculated using "
+ "activity data and applicable "
+ "emission factors.");
// Format the footnote text
noteText.getCharacterFormat()
.setFontName("Arial");
noteText.getCharacterFormat()
.setFontSize(10);
// Format the reference marker
footnote.getMarkerCharacterFormat()
.setFontName("Calibri");
// Save the modified document
document.saveToFile(
"report-with-footnote.docx",
FileFormat.Docx_2013);
} finally {
document.dispose();
}
}
}
The program creates report-with-footnote.docx, with a footnote reference mark after the first occurrence of "emissions accounting".
There are two important parts of this example.
First, findString() locates the target phrase, and getAsOneRange() retrieves its text range. From there, getOwnerParagraph() identifies the paragraph containing the text.
Second, appendFootnote(FootnoteType.Footnote) creates the footnote, but by default adds its reference mark at the end of the paragraph. To place it immediately after the matched phrase, we use:
int index = paragraph.getChildObjects()
.indexOf(textRange);
paragraph.getChildObjects()
.insert(index + 1, footnote);
The note itself is added through footnote.getTextBody(). Its content is separate from the paragraph text.
Because findString() returns the first match, the code only adds one footnote. If the same phrase appears several times, you would need to decide whether to annotate the first occurrence or each occurrence separately.
3. Add an Endnote to a Word Document
Endnotes work similarly to footnotes, but their content appears at the end of a document or section rather than at the bottom of the page.
They are often used for references or supplementary information that doesn't need to appear next to the main discussion.
To insert an endnote, use Paragraph.appendFootnote() with FootnoteType.Endnote.
The following example adds an endnote to the end of the first paragraph in report.docx.
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Footnote;
import com.spire.doc.fields.TextRange;
public class AddWordEndnote {
public static void main(String[] args) {
Document document = new Document();
try {
// Load the Word document
document.loadFromFile("report.docx");
// Get the first section
Section section =
document.getSections().get(0);
// Get the first paragraph
Paragraph paragraph =
section.getParagraphs().get(0);
// Add an endnote
Footnote endnote =
paragraph.appendFootnote(
FootnoteType.Endnote);
// Set the endnote text
TextRange noteText = endnote.getTextBody()
.addParagraph()
.appendText(
"Reference: Project Environmental "
+ "Assessment Report, 2026.");
// Format the endnote text
noteText.getCharacterFormat()
.setFontName("Arial");
noteText.getCharacterFormat()
.setFontSize(10);
// Save the document
document.saveToFile(
"report-with-endnote.docx",
FileFormat.Docx_2013);
} finally {
document.dispose();
}
}
}
The resulting file is saved as report-with-endnote.docx.
The key difference from the previous example is:
Footnote endnote =
paragraph.appendFootnote(FootnoteType.Endnote);
Although the method is named appendFootnote(), passing FootnoteType.Endnote creates an endnote instead.
This example places the reference mark at the end of the first paragraph. If you need the mark after a particular word or phrase, you can use the same text-search and insertion approach shown in Section 2.
The endnote's final position also depends on the document's endnote settings. Word can place endnotes at the end of a section or at the end of the entire document.
4. Read Footnotes and Endnotes from Word
When reviewing an existing document, you may need to extract its footnotes and endnotes—for example, to check references or collect explanatory notes.
The Document class provides two methods for accessing them:
-
getFootnotes()retrieves the document's footnotes. -
getEndnotes()retrieves its endnotes.
Both return lists of Footnote objects. Each object contains a text body that can have one or more paragraphs.
For this example, prepare a file named report-with-notes.docx containing both footnotes and endnotes.
The following program reads their text and prints it to the console.
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Footnote;
import java.util.List;
public class ReadWordNotes {
public static void main(String[] args) {
Document document = new Document();
try {
// Load a document with footnotes and endnotes
document.loadFromFile(
"report-with-notes.docx");
// Get all footnotes
List<Footnote> footnotes =
document.getFootnotes();
System.out.println("=== Footnotes ===");
for (int i = 0; i < footnotes.size(); i++) {
Footnote note = footnotes.get(i);
System.out.println(
"Footnote " + (i + 1));
// Read paragraphs in the footnote
for (int j = 0;
j < note.getTextBody()
.getParagraphs().getCount();
j++) {
Paragraph paragraph =
note.getTextBody()
.getParagraphs().get(j);
System.out.println(
paragraph.getText());
}
}
// Get all endnotes
List<Footnote> endnotes =
document.getEndnotes();
System.out.println("=== Endnotes ===");
for (int i = 0; i < endnotes.size(); i++) {
Footnote note = endnotes.get(i);
System.out.println(
"Endnote " + (i + 1));
// Read paragraphs in the endnote
for (int j = 0;
j < note.getTextBody()
.getParagraphs().getCount();
j++) {
Paragraph paragraph =
note.getTextBody()
.getParagraphs().get(j);
System.out.println(
paragraph.getText());
}
}
} finally {
document.dispose();
}
}
}
The code handles footnotes and endnotes separately, then loops through the paragraphs inside each note.
The main operation for reading the content is:
note.getTextBody().getParagraphs()
Each paragraph's text can then be retrieved using Paragraph.getText().
This approach works for ordinary text notes. However, footnotes and endnotes can also contain multiple paragraphs, hyperlinks, and other document elements. If you need to preserve those structures, you'll have to inspect the objects inside the note body rather than extracting only paragraph text.
Also, the numbers printed by the program are simply list positions. They aren't necessarily the same as the reference numbers displayed in Word, particularly when a document uses custom numbering or restarts numbering between sections.
5. Remove Footnotes from a Word Document
Sometimes explanatory notes become unnecessary after a document has been revised. You may want to remove them while keeping the main text.
In Java, a footnote can be removed from its owning paragraph's child object collection.
The following example removes all footnotes from report-with-notes.docx and saves the result as a new file.
import com.spire.doc.*;
import com.spire.doc.fields.Footnote;
import java.util.List;
public class RemoveWordFootnotes {
public static void main(String[] args) {
Document document = new Document();
try {
// Load the Word document
document.loadFromFile(
"report-with-notes.docx");
// Get all footnotes
List<Footnote> footnotes =
document.getFootnotes();
// Remove footnotes in reverse order
for (int i = footnotes.size() - 1;
i >= 0;
i--) {
Footnote footnote = footnotes.get(i);
// Remove the footnote from its paragraph
footnote.getOwnerParagraph()
.getChildObjects()
.remove(footnote);
}
// Save the modified document
document.saveToFile(
"report-without-footnotes.docx",
FileFormat.Docx_2013);
System.out.println(
"Footnotes removed successfully.");
} finally {
document.dispose();
}
}
}
The important part is:
footnote.getOwnerParagraph()
.getChildObjects()
.remove(footnote);
getOwnerParagraph() identifies the paragraph containing the footnote reference. Removing the Footnote object removes that reference and its associated note content without deleting the surrounding paragraph text.
The code iterates backward because removing objects can change the collection being processed.
This example removes footnotes only. Endnotes are left unchanged.
If you only need to remove a particular footnote, locate the corresponding object first and remove it using the same approach.
6. A Few Things to Keep in Mind
Footnotes Are Not the Same as Page Footers
Despite their similar names, footnotes and footers are different Word elements.
A footer generally contains information repeated across pages, such as page numbers or document titles. A footnote is connected to a particular reference in the document's main text.
Removing a page footer doesn't remove footnotes, and footnotes don't need to be accessed through the document's header and footer collection.
The Reference Mark's Position Matters
When you call appendFootnote(), the reference mark is added at the end of the paragraph.
If the note explains a particular term, it's usually better to place the mark immediately after that term. This requires locating the relevant text range and inserting the footnote object at the correct position.
For documents with complex formatting, it's worth checking the result in Word to make sure the marker appears exactly where intended.
Notes Can Contain More Than Plain Text
Footnotes and endnotes aren't limited to single paragraphs.
They may contain multiple paragraphs, formatted text, hyperlinks, or other embedded elements. Reading only Paragraph.getText() is sufficient for basic text extraction, but not for reconstructing the complete content or formatting of a note.
Check Numbering and Placement After Editing
Word controls footnote and endnote numbering through document or section settings.
Adding or removing notes may affect the displayed reference numbers. If you're processing a document with multiple sections or custom numbering, check the output to ensure the numbering and placement still follow the intended format.
7. Conclusion
Working with footnotes and endnotes in Java involves three main tasks: creating a note, accessing its content, and removing it when needed.
Use Paragraph.appendFootnote() with the appropriate FootnoteType to add either a footnote or an endnote. To read existing notes, use getFootnotes() or getEndnotes() and access their text bodies.
For removal, locate the footnote object and remove it from its owning paragraph.
The main detail to watch is where the reference mark appears in the document. When it needs to follow a specific word or phrase, positioning the note correctly is just as important as setting its content.
Top comments (0)