Сноска – это дополнительное описание основного текста, она позволяет вам ссылаться на источники или подробно объяснять концепцию и делает ваш документ более профессиональным. В этой статье будут приведены два примера того, как вставить сноску в документ Word в приложениях Java с помощью бесплатного Spire. Документ для Java.
● Добавьте сноску к абзацу в документе word. ° Вставьте сноску за указанным текстом в документе word.
Установка Способ 1: Скачать Бесплатный Шпиль. Doc для Java и распакуйте его. Затем добавьте Spire.Doc.jar файл в ваше Java-приложение в качестве зависимости.
Метод 2: Напрямую добавьте зависимость jar в проект maven, добавив следующие конфигурации в pom.xml .
com.e-iceblue e-iceblue http://repo.e-iceblue.com/nexus/content/groups/public/ e-iceblue spire.doc.free 3.9.0
【Пример 1】 Добавить сноску к первому абзацу в word
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 word document Document doc = new Document(); doc.loadFromFile("sample.docx", FileFormat.Docx_2010); //get the first paragraph from the first section Paragraph para = doc.getSections().get(0).getParagraphs().get(0); //Add footnote to the first paragraph Footnote footnote = para.appendFootnote(FootnoteType.Footnote); //Add the text and format for it TextRange text = footnote.getTextBody().addParagraph().appendText("A study predict that polar bears may go extinct by 2100"); text.getCharacterFormat().setFontName("Calibri"); text.getCharacterFormat().setFontSize(11); text.getCharacterFormat().setTextColor(new Color(220, 130, 10)); //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 document to file doc.saveToFile("Addfootnote.docx", FileFormat.Docx_2010); } }
【Пример 2】 Найдите “экологическую нишу” и вставьте сноску за ней в документ word
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 word document Document doc = new Document(); doc.loadFromFile("sample.docx", FileFormat.Docx_2010); //find text string Spire.Doc in the whole word document TextSelection[] selections = doc.findAllString("ecological niche", 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); //Add the text and format for it TextRange text = footnote.getTextBody().addParagraph().appendText("In ecology, a niche is the match of a species to a specific environmental condition."); text.getCharacterFormat().setFontName("Calibri"); text.getCharacterFormat().setFontSize(10); text.getCharacterFormat().setTextColor(new Color(41, 167, 56)); //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 document to file doc.saveToFile("Addfootnote.docx", FileFormat.Docx_2010); } } }
Оригинал: “https://dev.to/jazzzzz/insert-footnote-to-word-using-java-55k7”