Java 添加Word脚注、尾注

如果你不相信努力和时光,那么成果就会是第一个选择辜负你的。不要去否定你自己的过去,也不要用你的过去牵扯你现在的努力和对未来的展望。不是因为拥有希望你才去努力,而是去努力了,你才有可能看到希望的光芒。Java 添加Word脚注、尾注,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

Word中的脚注和尾注都是对文本的补充说明。脚注一般是附在书页最左下端的注文,用以解释、说明特定内容;而尾注则是位于文档末尾,用于列出引文的出处。脚注和尾注都可以是针对某些文字或者段落来添加。本文中,就将演示如何通过Java程序来添加脚注及尾注到Word文档。

注:添加脚注时,本文分给指定文本添加脚注、给指定段落添加脚注;同理,添加尾注时,也可以指定文本或段落(可参照文中对脚注的添加方法)。

 

使用工具:Free Spire.Doc for Java (免费版)

Jar文件获取及导入:

方法1通过官网下载jar文件包,并解压。解压文件后,将lib文件夹中的Spire.Doc.jar文件导入Java程序。

方法2通过maven仓库导入

 

Java代码示例(供参考)

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){
        //加载测试文档
        Document doc = new Document("test.doc");

        //添加脚注1:给指定段落添加脚注
        Paragraph para1 = doc.getSections().get(0).getParagraphs().get(2);//获取段落
        Footnote footnote1 = para1.appendFootnote(FootnoteType.Footnote);//添加脚注
        TextRange text1 = footnote1.getTextBody().addParagraph().appendText("详见附件内容");
        text1.getCharacterFormat().setFontName("楷书");//格式化脚注标签及脚注内容
        text1.getCharacterFormat().setFontSize(10);
        text1.getCharacterFormat().setTextColor(new Color(255, 140, 0));
        footnote1.getMarkerCharacterFormat().setFontName("楷书");
        footnote1.getMarkerCharacterFormat().setFontSize(14);
        footnote1.getMarkerCharacterFormat().setTextColor(new Color(0, 0, 139));

        //添加脚注2:给指定文本添加脚注
        TextSelection[] selections = doc.findAllString("消除缺陷", false, true);
        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("请查看操作手册");
            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));

         //添加尾注:给指定段落添加尾注(给指定文本添加尾注可参考以上添加脚注的代码方法)
         Paragraph para3 = doc.getSections().get(0).getParagraphs().get(15);
         Footnote endnote= para3.appendFootnote(FootnoteType.Endnote);
         TextRange text3 = endnote.getTextBody().addParagraph().appendText("引用自刘玲《操作手册》");
         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));

         //保存文档
         doc.saveToFile("result.docx",FileFormat.Docx_2010);
        }
    }
}

脚注添加效果:

Java 添加Word脚注、尾注

尾注添加效果:

Java 添加Word脚注、尾注

 

(本文完)

转载请注明出处!

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/209418.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!