Add an Endnote to Word in Java

This article shows you how to add an endnote to Word documents using Spire.Doc for Java.

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\\sample.docx");

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

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

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

        //Set endnote text
        TextRange textRange = endnote.getTextBody().addParagraph().appendText("This is an endnote created by Spire.Doc.");

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

Add an Endnote to Word in Java