Get the App
SLTechnology News&Howtos  ›  Development  › 

How to add, read, and delete Word footnotes / endnotes by Java

Shulou Source: shulou.com Published: 2022-06-02 12:52:04 10月02日 Update

This article mainly shows you the "Java how to add, read, delete Word footnotes / endnotes", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn how to add, read, delete Word footnotes / endnotes "this article.

Using tools: Free Spire.Doc for Java (free version)

Jar file acquisition and import:

Method 1: download the jar package through the official website and decompress it. After extracting the file, import the Spire.Doc.jar file in the lib folder into the Java program.

Method 2: import through maven repository.

[example 1] add footnotes, endnotes

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 AddFootnoteEndnote {public static void main (String [] args) {/ / load test document Document doc = new Document ("test.doc") / / add footnote 1: add footnote Paragraph para1 = doc.getSections (). Get (0). GetParagraphs (). Get (2); / / get the paragraph Footnote footnote1 = para1.appendFootnote (FootnoteType.Footnote); / / add footnote TextRange text1 = footnote1.getTextBody (). AddParagraph (). AppendText ("see Annex for details"); text1.getCharacterFormat (). SetFontName ("regular script") / format footnote tags and footnote contents text1.getCharacterFormat (). SetFontSize (10); text1.getCharacterFormat (). SetTextColor (new Color (255,140,0); footnote1.getMarkerCharacterFormat (). SetFontName (regular script); footnote1.getMarkerCharacterFormat (). SetFontSize (14); footnote1.getMarkerCharacterFormat (). SetTextColor (new Color (0,0,139)) / / add footnote 2: add footnote TextSelection [] selections = doc.findAllString ("defect removal", false, true) to the specified text; for (TextSelection selection: selections) {TextRange range = selection.getAsOneRange (); Paragraph para2 = range.getOwnerParagraph (); Footnote footnote2 = para2.appendFootnote (FootnoteType.Footnote); int index = para2.getChildObjects (). IndexOf (range) Para2.getChildObjects (). Insert (index + 1, footnote2); TextRange text2 = footnote2.getTextBody (). AddParagraph (). AppendText ("Please check the operation manual"); text2.getCharacterFormat (). SetFontName ("Arial Black"); text2.getCharacterFormat (). SetFontSize (10); text2.getCharacterFormat (). SetTextColor (new Color (153,50,204)) Footnote2.getMarkerCharacterFormat (). SetFontName ("Calibri"); footnote2.getMarkerCharacterFormat (). SetFontSize (14); footnote2.getMarkerCharacterFormat (). SetTextColor (new Color (0,0,139)) / / add endnotes: add endnotes to a specified paragraph (for adding endnotes to specified text, please refer to the above code method for adding footnotes) Paragraph para3 = doc.getSections (). Get (0). GetParagraphs (). Get (15); Footnote endnote= para3.appendFootnote (FootnoteType.Endnote); TextRange text3 = endnote.getTextBody (). AddParagraph (). AppendText (quoted from Liu Ling's Operation Manual) Text3.getCharacterFormat (). SetFontName ("Arial Black"); text3.getCharacterFormat (). SetFontSize (10); text3.getCharacterFormat (). SetTextColor (new Color (135,206,204); endnote.getMarkerCharacterFormat (). SetFontName ("Calibri"); endnote.getMarkerCharacterFormat (). SetFontSize (14); endnote.getMarkerCharacterFormat (). SetTextColor (new Color (0,0,139)) / / Save the document doc.saveToFile ("result.docx", FileFormat.Docx_2010);}

Footnote add effect:

Endnote add effect:

[example 2] read Word footnotes, endnotes

The footnotes and endnotes generated in the above text are test documents.

1. Read Word footer

Import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.Footnote; import com.spire.doc.fields.TextRange; import java.util.List; public class ExtractFootnoteAndEndnote {public static void main (String [] args) {/ / create Document instance Document doc = new Document (); doc.loadFromFile ("result.docx") / / get all footnotes in the document List footNotes = doc.getFootnotes (); / / instantiate the String type variable String str = ""; / / traverse the footer for (Footnote footNote: footNotes) {/ / traverse the paragraph for in the footer (int j = 0; j)

< footNote.getTextBody().getParagraphs().getCount(); j++) { Paragraph paragraph = footNote.getTextBody().getParagraphs().get(j); //遍历段落中的对象 for(Object object : paragraph.getChildObjects()){ //读取文本 if (object instanceof TextRange) { TextRange textRange = (TextRange) object; str = str + textRange.getText(); } } } } //输出脚注文本 System.out.println(str); } } 脚注读取结果:

two。 Read Word endnote

Import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.Footnote; import com.spire.doc.fields.TextRange; import java.util.List; public class ExtractFootnoteAndEndnote {public static void main (String [] args) {/ / create Document instance Document doc = new Document (); doc.loadFromFile ("result.docx") / / get all endnotes List endNotes = doc.getEndnotes (); / / instantiate the String type variable String str = ""; / / traverse endnote for (Footnote endnote: endNotes) {/ / traverse paragraph for (endnote j = 0; j)

< endnote.getTextBody().getParagraphs().getCount(); j++) { Paragraph paragraph = endnote.getTextBody().getParagraphs().get(j); //遍历段落中的对象 for(Object object : paragraph.getChildObjects()){ //读取文本 if (object instanceof TextRange) { TextRange textRange = (TextRange) object; str = str + textRange.getText(); } } } } //输出尾注文本 System.out.println(str); } } 尾注读取结果:

[example 3] Delete Word footnotes and endnotes

Import com.spire.doc.*;import com.spire.doc.documents.Paragraph;import com.spire.doc.fields.Footnote;import java.util.List;public class DeleteFootnoteAndEndnote {public static void main (String [] args) {/ / load the test document Document doc = new Document (); doc.loadFromFile ("result.docx"); / / get the first section Section section = doc.getSections () .get (0) / / traverse the sub-object for (int I = 0; I) in all paragraphs

Tags: Footnotes paragraphs text documents content files instances objects methods examples articles tests variables manuals operation manuals effects regular script types results jposts + Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple Shulou Technology Linux MySQL