Add footnotes to Word documents in Java

Footnote A type of annotation that describes the content of a word or sentence. Often placed at the bottom of a page. Due to the footnotes, the user can learn some complex words more clearly and the integrity can be preserved. Therefore, this specification uses Spire.Doc to add footnotes to Word documents for Java applications. The method will be described.

  1. Add a footnote after the entire paragraph.
  2. Search for the specified text and add a footnote after the search text. Add a footnote to the first paragraph of the word document:
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.awt.*;


public class WordFootnote {
    public static void main(String[] args) throws Exception {
        //Load sample document
        Document doc = new Document();
        doc.loadFromFile("Sample.docx", FileFormat.Docx_2010);

        //Get the first stage of the first selection.
        Paragraph para = doc.getSections().get(0).getParagraphs().get(0);

        //Add a footnote after the first paragraph
        Footnote footnote = para.appendFootnote(FootnoteType.Footnote);

        //Add footnote content and format the font
        TextRange text = footnote.getTextBody().addParagraph().appendText("Demo of Spire.Doc");
        text.getCharacterFormat().setFontName("Arial Black");
        text.getCharacterFormat().setFontSize(10);
        text.getCharacterFormat().setTextColor(new Color(255, 140, 0));
        footnote.getMarkerCharacterFormat().setFontName("Calibri");
        footnote.getMarkerCharacterFormat().setFontSize(12);
        footnote.getMarkerCharacterFormat().setBold(true);
        footnote.getMarkerCharacterFormat().setTextColor(new Color(0, 0, 139));

        //Save document
        doc.saveToFile("output/Addfootnote.docx", FileFormat.Docx_2010);

        }
    }

Effect diagram: Footnote1.png

Search for the specified text Spire.Doc and add a footnote:

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


public class WordFootnotes {
    public static void main(String[] args) throws Exception {
        //Load sample document
        Document doc = new Document();
        doc.loadFromFile("Sample.docx", FileFormat.Docx_2010);
        
        //Search text Spire.Doc
        TextSelection[] selections = doc.findAllString("Spire.Doc", false, true);
        for (TextSelection selection : selections) {
            TextRange range = selection.getAsOneRange();
            Paragraph para = range.getOwnerParagraph();

            //Adds a footnote after the specified text
            Footnote footnote = para.appendFootnote(FootnoteType.Footnote);
            int index = para.getChildObjects().indexOf(range);

            para.getChildObjects().insert(index + 1, footnote);

            //Add footnote content and format the font
            TextRange text = footnote.getTextBody().addParagraph().appendText("Demo of Spire.Doc");
            text.getCharacterFormat().setFontName("Arial Black");
            text.getCharacterFormat().setFontSize(10);
            text.getCharacterFormat().setTextColor(new Color(255, 140, 0));
            footnote.getMarkerCharacterFormat().setFontName("Calibri");
            footnote.getMarkerCharacterFormat().setFontSize(12);
            footnote.getMarkerCharacterFormat().setBold(true);
            footnote.getMarkerCharacterFormat().setTextColor(new Color(0, 0, 139));

            //Save document
            doc.saveToFile("output/Addfootnote.docx", FileFormat.Docx_2010);

        }
    }
}

Effect diagram: Footnote2.png

Recommended Posts

Add footnotes to Word documents in Java
Add multi-letter watermark to Java Word
Java adds hyperlinks to Word documents
[Java] Add WordArt to Word document
Add SameSite attribute to cookie in Java
Merge Java Word documents
Java adds and removes watermarks in word documents
Change paragraph text color in Java Word documents
Java applications convert Word (DOC / DOCX) documents to PDF
Java joins and splitting table cells in Word documents
Multithreaded to fit in [Java] template
How to learn JAVA in 7 days
Add watermark to Java to PDF document
How to use classes in Java?
Java encryption, decryption of Word documents
Java draws shapes in PDF documents
Add / remove watermark to Java PowerPoint
How to concatenate strings in java
Java (add2)
Java (add)
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
Try to solve Project Euler in Java
Easy to make Slack Bot in Java
Java reference to understand in the figure
[Java] How to add data to List (add, addAll)
How to add jar file in ScalaIDE
How to do base conversion in Java
Java creates a table in a Word document
Convert SVG files to PNG files in Java
How to implement coding conventions in Java
Add Document to Azure Search Service (Java)
How to get the date in java
[Java Bronze] 5 problems to keep in mind
Sample to unzip gz file in Java
Java to C and C to Java in Android Studio
Reason to add L to the number to be put in Java long type
Add original weapons to mods in Minecraft Forge 1.15.2
Two ways to start a thread in Java + @
[Java] Add quotes only to specific CSV columns
Add --enable-preview option in Java in Visual Studio Code
I want to send an email in Java.
How to display a web page in Java
CORBA seems to be removed in Java SE 11. .. ..
Code to escape a JSON string in Java
Try to create a bulletin board in Java
Changes in Java 11
How to add sound in the app (swift)
I tried to implement deep learning in Java
Rock-paper-scissors in Java
How to get Class from Element in Java
There seems to be no else-if in java
How to add a classpath in Spring Boot
rsync4j --I want to touch rsync in Java.
How to hide null fields in response in Java
Add, read, and delete Excel comments in Java
How to conditionally add html.erb class in Rails
Library "OSHI" to get system information in Java
[Java] Introduction to Java
Add classpath: to the path specified in spring.datasource.schema