Add an Endnote to Word Using Java

Andrew Wilson
1 min readSep 7, 2021

Like footnote, the endnote is a supplementary explanation to the main text. It is usually located at the end of the document to list the source of the citation. This article will show you how to add an endnote to Word documents using Free Spire.Doc for Java.

Installation
Method 1: Download the free API 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>

Sample Code

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Footnote;
import com.spire.doc.fields.TextRange;

import java.awt.*;

public class AddEndnote {

public static void main(String[] args) {

//Create a Document object
Document doc = new Document();

//Load the sample Word file
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Moon.docx");

//Get the first section
Section section = doc.getSections().get(0);

//Get the specific paragraph to add endnote
Paragraph paragraph = section.getParagraphs().get(3);

//Add an endnote
Footnote endnote = paragraph.appendFootnote(FootnoteType.Endnote);

//Set endnote text
TextRange textRange = endnote.getTextBody().addParagraph().appendText("You can add the necessary endnote here.");

//Set text format of endnote
textRange.getCharacterFormat().setFontName("Arial");
textRange.getCharacterFormat().setFontSize(13f);
textRange.getCharacterFormat().setTextColor(Color.RED);

//Save to file
doc.saveToFile("AddEndnote.docx", FileFormat.Docx_2013);
}
}

--

--

Andrew Wilson

Explore C#, Java and Python solutions for processing Word/Excel/PowerPoint/PDF files.