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.
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:
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:
Recommended Posts