Insert footnote to word in Java

This article demonstrates how to insert footnote to a word document in Java applications with the help of Spire.Doc. We will show you how to add footnote by the following two parts.

  • Add a footnote to a paragraph in the word document.
  • Find a specified text string and insert footnote behinds it in the word document.

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("Demo of Spire.Doc");
        text.getCharacterFormat().setFontName("Arial Black");
        text.getCharacterFormat().setFontSize(10);
        text.getCharacterFormat().setTextColor(new Color(255, 140, 0));

        //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("output/Addfootnote.docx", FileFormat.Docx_2010);

        }
    }

Effective screenshot after insert a footnote in the paragraph:

Insert footnote to word in Java

Find “Spire.Doc” and insert 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 WordFootnotes {
    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("Spire.Doc", 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("Demo of Spire.Doc");
            text.getCharacterFormat().setFontName("Arial Black");
            text.getCharacterFormat().setFontSize(10);
            text.getCharacterFormat().setTextColor(new Color(255, 140, 0));
            //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("output/Addfootnote.docx", FileFormat.Docx_2010);

        }
    }
}

Effective screenshot after insert footnotes to the searched text strings:

Insert footnote to word in Java