Footnotes and endnotes are commonly used to add supplemental information to the body text in Word documents. In this article, I am going to elaborate how to work with footnotes and endnotes, particularly, add, format and remove footnotes and endnotes in a Word document programmatically using Free Spire.Doc for Java API.
Add Dependencies
First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that.
If you use maven, you need to add the following code to your project’s pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>Spire.Doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
For non-maven projects, download Free Spire.Doc for Java pack from this website and add Spire.Doc.jar in the lib folder into your project as a dependency.
Add Footnotes and Endnotes
The appendFootnote method in Paragraph class provided by Free Spire.Doc for Java API is used for adding footnotes and endnotes. You can either add footnotes and endnotes to newly added text or add to existing text. In the following examples, I will show you how to add and format footnotes and endnotes using this API.
a. Add footnotes
In the following code, you will see how to add footnotes to a particular text in a Word document.
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.Footnote;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class AddFootnotes {
public static void main(String[] args) {
//Create a Document instance
Document document = new Document();
//Load a Word document
document.loadFromFile("FootnoteExample.docx");
//Find a particular text
TextSelection selection = document.findString("Sample", false, true);
//Add a footnote to the text
TextRange text = selection.getAsOneRange();
Paragraph paragraph = text.getOwnerParagraph();
int index = paragraph.getChildObjects().indexOf(text);
Footnote footnote = paragraph.appendFootnote(FootnoteType.Footnote);
paragraph.getChildObjects().insert(index + 1, footnote);
//Set Character format for the footnote content
text = footnote.getTextBody().addParagraph().appendText("This is a footnote");
text.getCharacterFormat().setBold(true);
text.getCharacterFormat().setFontName("Arial");
text.getCharacterFormat().setFontSize(11);
text.getCharacterFormat().setTextColor(new Color(255, 140, 0));
//Set Character format for the footnote separator
footnote.getMarkerCharacterFormat().setFontName("Arial");
footnote.getMarkerCharacterFormat().setFontSize(14);
footnote.getMarkerCharacterFormat().setTextColor(new Color(0, 0, 139));
//Save the document
document.saveToFile("AddFootnote.docx", FileFormat.Docx_2013);
}
}
b. Add endnotes
Adding endnotes to a particular text is similar with the above code. The following code introduces how to add endnotes to newly added text.
import com.spire.doc.*;
import com.spire.doc.documents.BuiltinStyle;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Footnote;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class AddEndNotes {
public static void main(String[] args) {
//Create a Document instance
Document doc = new Document();
//Add a section
Section section = doc.addSection();
//Add a paragraph
Paragraph paragraph = section.addParagraph();
//Add text to the paragraph
paragraph.appendText("Microsoft Word is a word processor designed by Microsoft.");
paragraph.applyStyle(BuiltinStyle.Heading_1);
//Add an endnote to the paragraph
Footnote endnote = paragraph.appendFootnote(FootnoteType.Endnote);
//Add text to the endnote
TextRange text = endnote.getTextBody().addParagraph().appendText("This is an endnote");
//Set character format for the endnote content
text.getCharacterFormat().setBold(true);
text.getCharacterFormat().setFontName("Arial");
text.getCharacterFormat().setFontSize(11);
text.getCharacterFormat().setTextColor(new Color(255, 140, 0));
//Set character format for the endnote separator
endnote.getMarkerCharacterFormat().setFontName("Arial");
endnote.getMarkerCharacterFormat().setFontSize(14);
endnote.getMarkerCharacterFormat().setTextColor(new Color(0, 0, 139));
//Save the document
doc.saveToFile("AddEndnote.docx", FileFormat.Docx_2013);
}
}
Remove Footnotes and Endnotes
Below are the main steps and code to remove footnotes and endnotes from a Word document:
Step 1. Get the section in the document.
Step 2. Loop through the paragraphs in the section.
Step 3. Loop through the child objects in the paragraph and detect if the object is footnote.
Step 4. If yes, remove the object.
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Footnote;
import com.spire.doc.fields.ParagraphBase;
public class RemoveFootnotesAndEndnotes {
public static void main(String[] args) {
Document document = new Document();
document.loadFromFile("AddFootnote.docx");
Section section = document.getSections().get(0);
//Loop through the paragraphs in the section and find the footnote or endnote
for (int i = 0; i < section.getParagraphs().getCount(); i++)
{
Paragraph para = section.getParagraphs().get(i);
int index = -1;
for (int j = 0, cnt = para.getChildObjects().getCount(); j < cnt; j++)
{
ParagraphBase pBase = (ParagraphBase)para.getChildObjects().get(j);
if (pBase instanceof Footnote)
{
index = j;
break;
}
}
if (index > -1)
//Remove the footnote or endnote
para.getChildObjects().removeAt(index);
}
//Save the document
document.saveToFile("Result.docx", FileFormat.Docx_2013);
}
}
Top comments (0)