脚注在 Word 文档中被广泛使用,可以为标注的内容提供更多的信息或是详细的解释。文章中的长难词句往往会让读者难以理解文章内容,但在文章中对这些词句进行解释又会影响文章的连贯性,且使文章变得冗长乏味。而脚注可以解决这样的问题。脚注允许作者将标注内容的更多信息或详细解释放在页面底部,从而不影响文章的流畅性和简洁性。本文将介绍如何使用 Spire.Doc for Java 在 Word 文档中插入或删除脚注。
安装 Spire.Doc for Java
首先,您需要在 Java 程序中添加 Spire.Doc.jar 文件作为依赖项。您可以从这个链接下载 JAR 文件;如果您使用 Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>13.10.6</version>
    </dependency>
</dependencies>
在 Word 文档中插入脚注
插入脚注的详细操作步骤如下:
- 创建 Document 类的对象。
- 用 Document.loadFromFile() 方法从磁盘加载 Word 文档。
- 用 Document.findString() 方法找到脚注目标文本。
- 用 Paragraph.getChildObjects().insert() 方法添加一个脚注。
- 用 Footnote.getTextBody.addParagraph() 方法在脚注中添加一个段落。
- 用 Paragraph.appendText() 方法添加文本到脚注的段落中。
- 用 TextRange.getCharacterFormat() 方法返回的 CharacterFormat 对象下的方法设置脚注文本的格式。
- 用 Footnote.getMarkerCharacterFormat() 方法返回的 CharaterFormat 对象下的方法设置脚注标示文本的格式。
- 用 Doucment.svaToFile() 方法保存文档。
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.awt.*;
public class insertFootnote {
    public static void main(String[] args) {
        //创建 Document 类的对象
        Document document = new Document();
        //从磁盘加载 Word 文档
        document.loadFromFile("D:/Smaples/Sample.docx");
        //找到脚注目标文字
        TextSelection selection = document.findString("VMFS", false, true);
        //添加脚注
        TextRange textRange = selection.getAsOneRange();
        Paragraph paragraph = textRange.getOwnerParagraph();
        int index = paragraph.getChildObjects().indexOf(textRange);
        Footnote footnote = paragraph.appendFootnote(FootnoteType.Footnote);
        paragraph.getChildObjects().insert(index + 1, footnote);
        //在脚注中添加一个段落
        Paragraph paragraph1 = footnote.getTextBody().addParagraph();
        //在脚注的段落中添加文本
        textRange = paragraph1.appendText("VMware Virtual Machine File System,虚拟机文件系统。");
        //设置脚注文本的格式
        textRange.getCharacterFormat().setFontName("Arial");
        textRange.getCharacterFormat().setFontSize(11);
        textRange.getCharacterFormat().setTextColor(Color.green);
        //设置脚注标示文本的格式
        footnote.getMarkerCharacterFormat().setFontName("Calibri");
        footnote.getMarkerCharacterFormat().setFontSize(12);
        footnote.getMarkerCharacterFormat().setBold(true);
        footnote.getMarkerCharacterFormat().setTextColor(Color.red);
        //保存文档
        String output = "output/添加脚注.docx";
        document.saveToFile(output, FileFormat.Docx_2010);
    }
}
从 Word 文档中移除脚注
移除脚注的详细操作步骤如下:
- 创建 Document 类的对象。
- 用 Document.loadFromFile() 方法从磁盘加载 Word 文档。
- 循环遍历文档中的节,节中的段落,以及段落中的子对象,并判断子对象是否是脚注。然后再用 Paragraph.getChildObjects().removeAt() 方法逐一删除脚注。
- 用 Document.saveToFile() 方法保存文档。
- Java
import com.spire.doc.*;
import com.spire.doc.fields.*;
import com.spire.doc.documents.*;
import java.util.*;
public class RemoveFootnote {
    public static void main(String[] args) {
        //创建 Document 类的对象
        Document document = new Document();
        //从磁盘加载 Word 文档
        document.loadFromFile("D:/Samples/Sample.docx");
        //在所有节中循环
        for (int i = 0; i < document.getSections().getCount(); i++) {
            //获取指定节
            Section section = document.getSections().get(i);
            //在该节中的段落中循环
            for (int j = 0; j < section.getParagraphs().getCount(); j++)
            {
                //获取指定段落
                Paragraph para = section.getParagraphs().get(j);
                //创建列表
                List footnotes = new ArrayList<>();
                //在段落的所有子对象中循环
                for (int k = 0, cnt = para.getChildObjects().getCount(); k < cnt; k++)
                {
                    //获取指定子对象
                    ParagraphBase pBase = (ParagraphBase)para.getChildObjects().get(k);
                    //判断该子对象是否为脚注
                    if (pBase instanceof Footnote)
                    {
                        Footnote footnote = (Footnote)pBase;
                        //将脚注添加到列表中
                        footnotes.add(footnote);
                    }
                }
                if (footnotes != null) {
                    //在列表中的脚注中循环
                    for (int y = 0; y < footnotes.size(); y++) {
                        //删除指定脚注
                        para.getChildObjects().remove(footnotes.get(y));
                    }
                }
            }
        }
        //保存文档
        document.saveToFile("output/removeFootnote.docx", FileFormat.Docx);
    }
} 
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 
    


 
					



