Insert footnote to word using Java
Tags: java, footnote, word, free
Published: 2021-02-26 07:44:33

Footnote is a supplementary description of the main text, it allows you to cite sources or explain a concept in detail and makes your document more professional. This article will give the below two examples of how to insert footnote to a word document in Java applications with the help of Free Spire.Doc for Java. 

● Add a footnote to a paragraph in the word document.
● Insert a footnote behinds the specified text in the word document.

**Installation**
**Method 1:** Download [Free Spire.Doc for Java](https://www.e-iceblue.com/Download/doc-for-java-free.html) and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.

**Method 2:** Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.
```xml
<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>
```

**【Example 1】
Add a footnote to the first paragraph in word**
```java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.awt.*;


public class WordFootnote {
    public static void main(String[] args) throws Exception {
        //load sample word document
        Document doc = new Document();
        doc.loadFromFile("sample.docx", FileFormat.Docx_2010);

        //get the first paragraph from the first section
        Paragraph para = doc.getSections().get(0).getParagraphs().get(0);

        //Add footnote to the first paragraph
        Footnote footnote = para.appendFootnote(FootnoteType.Footnote);

        //Add the text and format for it
        TextRange text = footnote.getTextBody().addParagraph().appendText("A study predict that polar bears may go extinct by 2100");
        text.getCharacterFormat().setFontName("Calibri");
        text.getCharacterFormat().setFontSize(11);
        text.getCharacterFormat().setTextColor(new Color(220, 130, 10));

        //set the format for footnote marker
        footnote.getMarkerCharacterFormat().setFontName("Calibri");
        footnote.getMarkerCharacterFormat().setFontSize(12);
        footnote.getMarkerCharacterFormat().setBold(true);
        footnote.getMarkerCharacterFormat().setTextColor(new Color(0, 0, 139));

        // save the document to file
        doc.saveToFile("Addfootnote.docx", FileFormat.Docx_2010);

    }
}
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gum3jfp90040x2a5subo.jpg)

**【Example 2】
Find “ecological niche” and insert a footnote behinds it in the word document**

```java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.awt.*;


public class WordFootnote {
    public static void main(String[] args) throws Exception {
        //load sample word document
        Document doc = new Document();
        doc.loadFromFile("sample.docx", FileFormat.Docx_2010);

        //find text string Spire.Doc in the whole word document
        TextSelection[] selections = doc.findAllString("ecological niche", false, true);
        for (TextSelection selection : selections) {
            TextRange range = selection.getAsOneRange();
            Paragraph para = range.getOwnerParagraph();

            //Add footnote behind the searched text strings
            Footnote footnote = para.appendFootnote(FootnoteType.Footnote);
            int index = para.getChildObjects().indexOf(range);

            para.getChildObjects().insert(index + 1, footnote);

            //Add the text and format for it
            TextRange text = footnote.getTextBody().addParagraph().appendText("In ecology, a niche is the match of a species to a specific environmental condition.");
            text.getCharacterFormat().setFontName("Calibri");
            text.getCharacterFormat().setFontSize(10);
            text.getCharacterFormat().setTextColor(new Color(41, 167, 56));
            //set the format for footnote marker
            footnote.getMarkerCharacterFormat().setFontName("Calibri");
            footnote.getMarkerCharacterFormat().setFontSize(12);
            footnote.getMarkerCharacterFormat().setBold(true);
            footnote.getMarkerCharacterFormat().setTextColor(new Color(0, 0, 139));

            // save the document to file
            doc.saveToFile("Addfootnote.docx", FileFormat.Docx_2010);

        }
    }
}
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2tjfg0z6q37g0j5z79x.jpg)