博客
关于我
Java 添加Word脚注、尾注
阅读量:409 次
发布时间:2019-03-06

本文共 3122 字,大约阅读时间需要 10 分钟。

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);        }    }}

脚注添加效果:

尾注添加效果:

 

(本文完)

转载请注明出处!

 

你可能感兴趣的文章
mysql5.7性能调优my.ini
查看>>
mysql5.7的安装和Navicat的安装
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump备份时忽略某些表
查看>>
mysqlreport分析工具详解
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
mysql中cast() 和convert()的用法讲解
查看>>
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中null和空字符串的区别与问题!
查看>>