Java: Insert or Remove Footnotes in Word

Footnotes are commonly used in Word documents to provide additional information or detailed explanations for the content. Long and complex words and phrases can make writings difficult for readers to travel through. But providing explanations in the main text would break the coherence of writings and make them much more tedious. Fortunately, footnotes can help writers to give information and explanations at the end of the page, without disrupting the fluency and concision of writings. This article demonstrates how to use Spire.Doc for Java to insert or remove footnotes in Word.

Install Spire.Doc for Java

First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url> https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>10.6.0</version>
    </dependency>
</dependencies>

Insert a Footnote into a Word Document

The detailed steps of inserting footnote are as follows:

  • Create an object of Document class.
  • Load a Word document form disk using Document.loadFromFile() method.
  • Find the text to be noted using Document.findString() method.
  • Add a footnote using Paragraph.getChildObjects().insert() method.
  • Add a paragraph in the footnote using Footnote.getTextBody().addParagraph() method.
  • Add text in the added paragraph using Paragraph.appendText() method.
  • Set the text format of the footnote using the methods under CharacterFormat object returned by TextRange.getCharacterFormat() method.
  • Set the text format of the marker using the methods under CharaterFormat object returned by Footnote.getMarkerCharacterFormat() method.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.awt.*;

public class insertFootnote {
    public static void main(String[] args) {
        //Create an object of Document class
        Document document = new Document();

        //Load a Word document from disk
        document.loadFromFile("D:/Samples/Sample.docx");

        //Find the word to be cited
        TextSelection selection = document.findString("ISO", false, true);

        //Add a footnote
        TextRange textRange = selection.getAsOneRange();
        Paragraph paragraph = textRange.getOwnerParagraph();
        int index = paragraph.getChildObjects().indexOf(textRange);
        Footnote footnote = paragraph.appendFootnote(FootnoteType.Footnote);
        paragraph.getChildObjects().insert(index + 1, footnote);

        //Add a paragraph in the footnote
        Paragraph paragraph1 = footnote.getTextBody().addParagraph();

        //Add text in the added paragraph
        textRange = paragraph1.appendText("International Organization for Standardization");

        //Set the text format of the footnote
        textRange.getCharacterFormat().setFontName("Arial Black");
        textRange.getCharacterFormat().setFontSize(10);
        textRange.getCharacterFormat().setTextColor(Color.yellow);

        //Set the text format of the marker
        footnote.getMarkerCharacterFormat().setFontName("Calibri");
        footnote.getMarkerCharacterFormat().setFontSize(12);
        footnote.getMarkerCharacterFormat().setBold(true);
        footnote.getMarkerCharacterFormat().setTextColor(Color.red);

        //Save the document
        String output = "output/insertFootnote.docx";
        document.saveToFile(output, FileFormat.Docx_2010);
    }
}

Java: Insert or Remove Footnotes in Word

Remove Footnotes from a Word Document

The detailed steps of removing footnotes are as follows:

  • Create an object of Document class.
  • Load a Word document from disk using Document.loadFromFile() method.
  • Loop through sections, then paragraphs and then their child objects to get a specific child object and determine if it is a footnote.
  • Remove a specific footnote using Paragraph.getChildObjects().removeAt() method.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.fields.*;
import com.spire.doc.documents.*;
import java.util.*;

public class RemoveFootnote {
    public static void main(String[] args) {

        //Create an object of Document class
        Document document = new Document();

        //Load a Word document from disk
        document.loadFromFile("D:/Samples/Sample.docx");

        //Loop through the sections
        for (int i = 0; i < document.getSections().getCount(); i++) {

            //Get a specific section
            Section section = document.getSections().get(i);

            //Loop through the paragraphs in the section
            for (int j = 0; j < section.getParagraphs().getCount(); j++)
            {
                //Get a specific paragraph
                Paragraph para = section.getParagraphs().get(j);

                //Create a list
                List footnotes = new ArrayList<>();

                //Loop through the child objects in the paragraph
                for (int k = 0, cnt = para.getChildObjects().getCount(); k < cnt; k++)
                {
                    //Get a specific child object
                    ParagraphBase pBase = (ParagraphBase)para.getChildObjects().get(k);

                    //Determine if the child object is a footnote
                    if (pBase instanceof Footnote)
                    {
                        Footnote footnote = (Footnote)pBase;
                        //Add the footnote to the list
                        footnotes.add(footnote);
                    }
                }
                if (footnotes != null) {

                    //Loop through the footnotes in the list
                    for (int y = 0; y < footnotes.size(); y++) {

                        //Remove a specific footnote
                        para.getChildObjects().remove(footnotes.get(y));
                    }
                }
            }
        }

        //Save the document
        document.saveToFile("output/removeFootnote.docx", FileFormat.Docx);
    }
}

Java: Insert or Remove Footnotes in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.