Friday, 11 June 2021

Add Footnotes and Endnotes to Word documents in Java

In a Word document, footnotes appear at the bottom of the page while endnotes come at the end of the document. A number on the footnote or endnote matches up with a reference mark in the document. This article will introduce how to add a footnote for a specified paragraph or text string in a Word document, and also show how to insert a endnote for a specified paragraph. It’s worth mentioning that I’ll finish the opration above using Java codes without Microsoft Office installed.

Development Environment

First of all, we need to download and install JDK and Intellij IDEA so that the development environment can be created. And then we add a Jar file called Spire.Doc.jar which is located in a free third-party library called Free Spire.Doc for Java to Intellij IDEA. You can get it from the link or refer to it by using the following Maven configuration.

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

Using the code

Add a footnote for a specified paragraph in Word

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.awt.*;

public class AddFootNoteForParagraph {
public static void main(String[] args) {
//Load a Word document
Document doc = new Document();
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.docx", FileFormat.Docx_2010);

//get the fifth paragraph of the first section
Paragraph para = doc.getSections().get(0).getParagraphs().get(4);

//Add footnote for the fifth paragraph
Footnote footnote = para.appendFootnote(FootnoteType.Footnote);

//Set font size, font color and font name for the content of the footnote
TextRange text = footnote.getTextBody().addParagraph().appendText("The Definition of Cashless Society");
text.getCharacterFormat().setFontName("Arial Black");
text.getCharacterFormat().setFontSize(10);
text.getCharacterFormat().setTextColor(new Color(180, 11, 255));

//set the format for footnote marker
footnote.getMarkerCharacterFormat().setFontName("Calibri");
footnote.getMarkerCharacterFormat().setFontSize(12);
footnote.getMarkerCharacterFormat().setBold(true);
footnote.getMarkerCharacterFormat().setTextColor(new Color(10, 6, 139));

// save the resulting document
doc.saveToFile("output/AddFootnoteToParagraph.docx", FileFormat.Docx_2010);
}
}

Output


Add a footnote for a specified text string in Word

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.awt.*;

public class AddFootNoteForText {
public static void main(String[] args) {
//load a Word document
Document doc = new Document();
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.docx", FileFormat.Docx_2010);

//find the text string "Cashless Society" in the whole document
TextSelection[] selections = doc.findAllString("Cashless Society", 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);

//Set font color, font name and font size for the content of the footnote
TextRange text = footnote.getTextBody().addParagraph().appendText("The Tech in Cashless Society");
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 resulting document
doc.saveToFile("output/AddFootnoteForText.docx", FileFormat.Docx_2010);

}
}
}

Output

Add a endnote for a specified paragraph in Word

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) {
//Load a Word document
Document doc = new Document();
doc.loadFromFile("C:\\Users\\Test1\\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 the content of the Endnote
TextRange textRange = endnote.getTextBody().addParagraph().appendText("This is an Endnote sample");

//Set font name, font size and font color for the content
textRange.getCharacterFormat().setFontName("Arial");
textRange.getCharacterFormat().setFontSize(14f);
textRange.getCharacterFormat().setTextColor(Color.RED);

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

Output





No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...