腳註,通常附在文章頁面的底部,用於對文檔某段或某處內容加以說明,經常使用在一些說明書、標書、論文等正式文書中。以此來讓原文保持完整、流暢。本文將經過使用Java程序來演示如何在Word文檔中添加和刪除腳註。html
方法1:經過官方網站下載獲取jar包。解壓後將lib文件夾下的Spire.Doc.jar文件導入Java程序。(以下圖)java
方法2:經過maven倉庫安裝導入。具體安裝詳情參見此網頁。app
狀況1:在整個段落後面添加腳註。maven
import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.*; import java.awt.*; public class AddFootnote { public static void main(String[] args) { //加載示例文檔 Document doc = new Document(); doc.loadFromFile("D:\\Desktop\\Sample.docx", FileFormat.Docx_2010); //獲取第一個section的第二段 Paragraph para = doc.getSections().get(0).getParagraphs().get(18); //在第一段後面添加腳註 Footnote footnote = para.appendFootnote(FootnoteType.Footnote); //添加腳註內容並設置字體格式 TextRange text = footnote.getTextBody().addParagraph().appendText("注:確屬本公司產品質量問題,自購置之日起保修期爲3個月。"); 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)); //保存文檔 doc.saveToFile("output/Addfootnote1.docx", FileFormat.Docx_2010); } }
腳註添加效果:工具
狀況2:查找指定文本,並在查找的文本後面添加腳註。字體
import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.*; import java.awt.*; public class AddFootnote2 { public static void main(String[] args) { //加載示例文檔 Document doc = new Document(); doc.loadFromFile("D:\\Desktop\\Sample.docx", FileFormat.Docx_2010); //查找文本AC110V/220V TextSelection[] selections = doc.findAllString("AC110V/220V", false, true); for (TextSelection selection : selections) { TextRange range = selection.getAsOneRange(); Paragraph para = range.getOwnerParagraph(); //在指定文本後添加腳註 Footnote footnote = para.appendFootnote(FootnoteType.Footnote); int index = para.getChildObjects().indexOf(range); para.getChildObjects().insert(index + 1, footnote); //添加腳註內容並設置字體格式 TextRange text = footnote.getTextBody().addParagraph().appendText("直流電110/220伏"); 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)); //保存文本 doc.saveToFile("output/Addfootnote2.docx", FileFormat.Docx_2010); } } }
腳註添加效果:網站
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.*; public class DeleteFootnote { public static void main(String[] args) { //加載示例文檔 Document document = new Document(); document.loadFromFile("D:\\Desktop\\Addfootnote1.docx"); Section section = document.getSections().get(0); //遍歷section中的段落並獲取全部腳註 for (int j = 0; j < section.getParagraphs().getCount(); j++) { Paragraph para = section.getParagraphs().get(j); int index = -1; for (int i = 0, cnt = para.getChildObjects().getCount(); i < cnt; i++) { ParagraphBase pBase = (ParagraphBase) para.getChildObjects().get(i); if (pBase instanceof Footnote) { index = i; break; } } if (index > -1) //移除腳註 para.getChildObjects().removeAt(index); } document.saveToFile("output/Removefootnote.docx", FileFormat.Docx); } }
腳註刪除效果:spa
(本文完)code