Add, Read, and Delete Word Footnotes and Endnotes from Java
Java Operations on Word Footnotes and Endnotes: Add, Read, Delete
When writing research reports, technical documents, or theses, some content needs supplementary explanation, but placing it directly in the body text can disrupt reading. Examples include explanations of specialized terms, data sources, or references.
Word provides footnotes and endnotes to handle such information. Footnotes typically appear at the bottom of the current page, while endnotes are grouped at the end of the document or a section.
If you need to generate documents via Java, or process annotations in existing Word files, you can directly manipulate footnote and endnote objects.
This article describes how to use Java to add footnotes and endnotes in Word, read existing annotations, and delete footnotes that are no longer needed.
1. Add Maven Dependency
First, add the following Maven repository and dependency to the pom.xml file of your Java project:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>14.10.6</version>
</dependency>
</dependencies>
The following examples use a Word document named report.docx to demonstrate different operations. Each example is an independent Java program; the required input files are explained in the corresponding sections.
2. Add a Footnote After Specified Text in Word
Assume the report contains this sentence:
The carbon emission accounting in this report references relevant international standards.
We want to add a footnote after the phrase "carbon emission accounting" that explains the calculation method and reference basis.
You can first use Document.findString() to locate the specified text, then create a footnote via Paragraph.appendFootnote(), and insert it after the matched text.
Complete 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 Word document
document.loadFromFile("report.docx");
// Find the text where the footnote needs to be added
TextSelection selection =
document.findString(
"碳排放核算", false, false);
if (selection == null) {
throw new IllegalArgumentException(
"未找到指定文字:碳排放核算");
}
// Get the paragraph and position of the matched text
TextRange textRange = selection.getAsOneRange();
Paragraph paragraph = textRange.getOwnerParagraph();
int index = paragraph.getChildObjects()
.indexOf(textRange);
// Create a footnote
Footnote footnote =
paragraph.appendFootnote(
FootnoteType.Footnote);
// Insert the footnote marker after the specified text
paragraph.getChildObjects()
.insert(index + 1, footnote);
// Set footnote content
TextRange noteText = footnote.getTextBody()
.addParagraph()
.appendText(
"碳排放核算依据相关排放因子和活动数据计算,"
+ "具体方法以报告采用的核算标准为准。");
// Set footnote text format
noteText.getCharacterFormat()
.setFontName("宋体");
noteText.getCharacterFormat()
.setFontSize(10);
// Set the font of the footnote reference marker
footnote.getMarkerCharacterFormat()
.setFontName("Calibri");
// Save the document
document.saveToFile(
"report-with-footnote.docx",
FileFormat.Docx_2013);
} finally {
document.dispose();
}
}
}
After running, the program generates report-with-footnote.docx. A footnote reference marker appears after "carbon emission accounting" in the original text, and the corresponding explanation appears at the bottom of the page.
Here are a few key operations.
findString() is used to locate where the footnote should be added. The example uses the first match, so if "carbon emission accounting" appears multiple times in the document, only the first occurrence is processed.
getOwnerParagraph() gets the paragraph containing the matched text, and indexOf() determines the position of the matched text in the paragraph's child object collection.
When creating a footnote, use:
Footnote footnote =
paragraph.appendFootnote(FootnoteType.Footnote);
Then, use getChildObjects().insert() to move the footnote to after the specified text rather than at the end of the paragraph.
The footnote content is added via getTextBody().addParagraph().appendText(). It belongs to the footnote body and is a different document object from the text in the original paragraph.
Note that if the target text does not exist, or if you need to add a footnote for each occurrence of the same keyword, you must adjust the search and insertion logic.
3. Add an Endnote in Word
Footnotes are suitable for explaining content on the current page. If you need to place references or supplementary explanations together at the end of the document, use an endnote.
The method for adding an endnote is similar to adding a footnote; the main difference is the parameter of appendFootnote().
Using FootnoteType.Endnote creates an endnote object.
The following example uses report.docx and adds an endnote at the end of the first paragraph in the first section.
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 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 at the end of the paragraph
Footnote endnote =
paragraph.appendFootnote(
FootnoteType.Endnote);
// Set endnote content
TextRange noteText = endnote.getTextBody()
.addParagraph()
.appendText(
"参考资料:2026 年项目环境影响评估报告。");
// Set endnote text format
noteText.getCharacterFormat()
.setFontName("宋体");
noteText.getCharacterFormat()
.setFontSize(10);
// Save the document
document.saveToFile(
"report-with-endnote.docx",
FileFormat.Docx_2013);
} finally {
document.dispose();
}
}
}
After running, report-with-endnote.docx is generated.
The core code here is:
Footnote endnote =
paragraph.appendFootnote(FootnoteType.Endnote);
Although the method name is still appendFootnote(), passing FootnoteType.Endnote creates an endnote.
Note that this example adds the endnote reference marker at the end of the specified paragraph, not after a specific keyword in the paragraph.
If you need to insert an endnote after a certain word, refer to the earlier footnote example: first locate the TextRange, then adjust the endnote object's position within the paragraph.
Also, whether an endnote displays at the end of the document or the end of a section depends on the Word document's endnote position setting. Inserting an endnote does not guarantee it appears on the last page of the entire file.
4. Read Footnotes and Endnotes in Word
For Word files that already contain footnotes and endnotes, you may sometimes need to extract annotation content, such as checking references or collecting reference information separately.
In Java, you can obtain the footnote and endnote collections via Document.getFootnotes() and Document.getEndnotes() respectively.
The following assumes a report-with-notes.docx file exists that contains both footnotes and endnotes. The program reads both types and outputs them 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 containing footnotes and endnotes
document.loadFromFile(
"report-with-notes.docx");
// Get all footnotes
List<Footnote> footnotes =
document.getFootnotes();
System.out.println("=== 脚注 ===");
for (int i = 0; i < footnotes.size(); i++) {
Footnote note = footnotes.get(i);
System.out.println("脚注 " + (i + 1));
// Read each paragraph 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("=== 尾注 ===");
for (int i = 0; i < endnotes.size(); i++) {
Footnote note = endnotes.get(i);
System.out.println("尾注 " + (i + 1));
// Read each paragraph 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();
}
}
}
This code processes footnotes and endnotes separately.
getFootnotes() returns the document's footnote collection, and getEndnotes() returns the endnote collection.
Each note object contains a TextBody, which you can access via:
note.getTextBody().getParagraphs()
to retrieve its paragraphs, then use Paragraph.getText() to read plain paragraph text.
Note that footnotes and endnotes can contain not only text, but also multiple paragraphs, hyperlinks, or other Word objects.
The example above mainly extracts plain paragraph text. If you need to preserve complex formatting, images, or other structures inside the notes, you must further traverse the corresponding document objects.
Additionally, the (i + 1) used in the example is just the sequential number in the program's output, not necessarily equal to the actual footnote or endnote number displayed in Word.
5. Delete Footnotes in Word
If some footnotes in a document are no longer needed, you can remove the corresponding footnote objects from their paragraphs.
For example, a report draft may contain many term explanations that should be removed before final publication, while keeping the body text intact.
The following code reads all footnotes from report-with-notes.docx and deletes them one by one.
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 a Word document containing footnotes
document.loadFromFile(
"report-with-notes.docx");
// Get all footnotes
List<Footnote> footnotes =
document.getFootnotes();
// Delete footnotes from the end backwards
for (int i = footnotes.size() - 1;
i >= 0;
i--) {
Footnote footnote = footnotes.get(i);
// Remove the footnote object from its owner paragraph
footnote.getOwnerParagraph()
.getChildObjects()
.remove(footnote);
}
// Save the processed document
document.saveToFile(
"report-without-footnotes.docx",
FileFormat.Docx_2013);
System.out.println("脚注删除完成");
} finally {
document.dispose();
}
}
}
The code uses getFootnotes() to obtain the footnote collection, then getOwnerParagraph() to find the paragraph each footnote belongs to.
When deleting, use:
footnote.getOwnerParagraph()
.getChildObjects()
.remove(footnote);
This deletes the footnote object in the Word document, including the corresponding reference marker in the body and the annotation content, not just the text shown at the bottom of the page.
The backward traversal avoids index issues that arise when objects are removed from a collection while it is being iterated.
Note that this example only deletes footnotes; it does not remove endnotes. If you need to delete only a single footnote, you can first locate the target footnote object and then perform the same removal operation.
For files with many footnotes, it is advisable to save a copy of the original document first so you can verify the body text and reference markers after deletion.
6. Considerations When Using Footnotes and Endnotes
1. Different Purposes
Footnotes usually sit at the bottom of the current page, suitable for supplementary explanations of terms, data, or specific content in the body.
Endnotes typically appear at the end of the document or section, suitable for placing references or longer supplementary explanations.
Both are represented by the Footnote object in Java, distinguished primarily by FootnoteType.Footnote and FootnoteType.Endnote.
2. Reference Marker Position Affects Reading Experience
In Word, footnotes generally appear right after the word or sentence they explain.
If you simply call appendFootnote(), the reference marker is added at the end of the paragraph. For precise positioning, first locate the target text, then insert the footnote object at the corresponding position.
For documents with multiple occurrences of the same keyword, you should clearly decide whether to handle only the first occurrence or to add a note at every match.
3. Don't Confuse Footnotes with Footers
Footnotes and page footers are different Word document elements.
Footers typically display page numbers, document names, dates, etc., while footnotes explain specific content in the body and usually carry a reference marker.
Therefore, deleting a footer does not delete footnotes, and operating on footnotes does not involve the header/footer collection.
4. Beware of Complex Content in Notes
If a footnote contains multiple paragraphs, special formatting, or embedded objects, Paragraph.getText() may not give you the complete structured information.
Moreover, footnote numbering, positioning, and formatting may be affected by document or section settings.
So for theses, technical specifications, or other Word files with strict layout requirements, reopen the document after modifications and check the positions of reference markers, numbering, and notes.
7. Summary
Java can handle common Word footnote and endnote operations, including adding annotations, reading existing content, and deleting footnotes that are no longer needed.
When adding a footnote or endnote, you primarily use Paragraph.appendFootnote() to create the object and FootnoteType to specify the type. To place the reference marker after specific text, you need to combine text search and paragraph object positioning.
For existing documents, you can use getFootnotes() and getEndnotes() to read annotations, then extract text or delete the corresponding objects as needed.
In practice, the most important points are the difference between footnotes and endnotes, and the precise position of the reference marker in the body text.