Skip to main content

Working with Footnotes and Endnotes in Word in Java


Footnotes and endnotes are commonly used to add supplemental information to the body text in Word documents. In this article, I am going to elaborate how to work with footnotes and endnotes, particularly, add, format and remove footnotes and endnotes in a Word document programmatically using Free Spire.Doc for Java API.

Add Dependencies

First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that.

If you use maven, you need to add the following code to your project’s pom.xml file.

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

For non-maven projects, download Free Spire.Doc for Java pack from this website and add Spire.Doc.jar in the lib folder into your project as a dependency.

Add footnotes and endnotes

The appendFootnote method in Paragraph class provided by Free Spire.Doc for Java API is used for adding footnotes and endnotes. You can either add footnote and endnote to newly added text or add to existing text. In the following examples, I will show you how to add and format footnote and endnote using this API.

  • Add footnotes

In the following code, you will see how to add footnotes to a particular text in a Word document.

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

import java.awt.*;

public class AddFootnotes {
public static void main(String[] args) {
//Create a Document instance
Document document = new Document();
//Load a Word document
document.loadFromFile("FootnoteExample.docx");

//Find a particular text
TextSelection selection = document.findString("Sample", false, true);

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

//Set Character format for the footnote content
text = footnote.getTextBody().addParagraph().appendText("This is a footnote");
text.getCharacterFormat().setBold(true);
text.getCharacterFormat().setFontName("Arial");
text.getCharacterFormat().setFontSize(11);
text.getCharacterFormat().setTextColor(new Color(255, 140, 0));

//Set Character format for the footnote separator
footnote.getMarkerCharacterFormat().setFontName("Arial");
footnote.getMarkerCharacterFormat().setFontSize(14);
footnote.getMarkerCharacterFormat().setTextColor(new Color(0, 0, 139));

//Save the document
document.saveToFile("AddFootnote.docx", FileFormat.Docx_2013);
}
}

Result:

  • Add endnotes

Adding endnotes to a particular text is similar with the above code. The following code introduces how to add endnotes to newly added text.

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

import java.awt.*;

public class AddEndNotes {
public static void main(String[] args) {
//Create a Document instance
Document doc = new Document();
//Add a section
Section section = doc.addSection();
//Add a paragraph
Paragraph paragraph = section.addParagraph();
//Add text to the paragraph
paragraph.appendText("Microsoft Word is a word processor designed by Microsoft.");
paragraph.applyStyle(BuiltinStyle.Heading_1);

//Add an endnote to the paragraph
Footnote endnote = paragraph.appendFootnote(FootnoteType.Endnote);
//Add text to the endnote
TextRange text = endnote.getTextBody().addParagraph().appendText("This is an endnote");

//Set character format for the endnote content
text.getCharacterFormat().setBold(true);
text.getCharacterFormat().setFontName("Arial");
text.getCharacterFormat().setFontSize(11);
text.getCharacterFormat().setTextColor(new Color(255, 140, 0));

//Set character format for the endnote separator
endnote.getMarkerCharacterFormat().setFontName("Arial");
endnote.getMarkerCharacterFormat().setFontSize(14);
endnote.getMarkerCharacterFormat().setTextColor(new Color(0, 0, 139));

//Save the document
doc.saveToFile("AddEndnote.docx", FileFormat.Docx_2013);
}
}

Result:

Remove footnotes and endnotes

The steps to remove footnotes and endnotes are listed as below:

Step 1. Get the section in the document.

Step 2. Loop through the paragraphs in the section.

Step 3. Loop through the child objects in the paragraph and detect if the object is footnote.

Step 4. If yes, remove the object.

The full code is given below.

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

public class RemoveFootnotesAndEndnotes {
public static void main(String[] args) {
Document document = new Document();
document.loadFromFile("AddFootnote.docx");
Section section = document.getSections().get(0);

//Loop through the paragraphs in the section and find the footnote or endnote
for (int i = 0; i < section.getParagraphs().getCount(); i++)
{
Paragraph para = section.getParagraphs().get(i);
int index = -1;
for (int j = 0, cnt = para.getChildObjects().getCount(); j < cnt; j++)
{
ParagraphBase pBase = (ParagraphBase)para.getChildObjects().get(j);
if (pBase instanceof Footnote)
{
index = j;
break;
}
}

if (index > -1)
//Remove the footnote or endnote
para.getChildObjects().removeAt(index);
}

//Save the document
document.saveToFile("Result.docx", FileFormat.Docx_2013);
}
}

Result:

Remove footnotes and endnotes


 

Comments

Popular posts from this blog

3 Ways to Generate Word Documents from Templates in Java

A template is a document with pre-applied formatting like styles, tabs, line spacing and so on. You can quickly generate a batch of documents with the same structure based on the template. In this article, I am going to show you the different ways to generate Word documents from templates programmatically in Java using Free Spire.Doc for Java library. Prerequisite First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file. <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name> ...

Insert and Extract OLE objects in Word in Java

You can use OLE (Object Linking and Embedding) to include content from other programs, such as another Word document, an Excel or PowerPoint document to an existing Word document. This article demonstrates how to insert and extract embedded OLE objects in a Word document in Java by using Free Spire.Doc for Java API.   Add dependencies First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file.     <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>     ...

Remove Duplicate Rows in Excel in C# and VB.NET

When an Excel file contains a huge amount of records, there might be duplicate records as well. In this blog, I am going to show you how to remove the duplicate rows in an Excel file programmatically in C# and VB.NET. The library I used: Free Spire.XLS for .NET Free Spire.XLS for .NET is a feature-rich Excel API offered by E-iceblue. It can be easily integrated in your .NET (C#, VB.NET, ASP.NET, .NET Core) applications to create, read, edit, convert and print Excel files without using Microsoft Office. Before coding, you need to get Free Spire.XLS for .NET by installing it via NuGet or downloading it via the official website . C# Code using  Spire.Xls;   using  System.Linq;      namespace  RemoveDuplicateRows   {        class  Program       {            static   void  Main( string [] args) ...