DEV Community

CodeSharing
CodeSharing

Posted on

Insert footnote to word using Java

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 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.

<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>
Enter fullscreen mode Exit fullscreen mode

【Example 1】
Add a footnote to the first paragraph in word

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);

    }
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

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

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);

        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

Top comments (0)