博客
关于我
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);        }    }}

脚注添加效果:

尾注添加效果:

 

(本文完)

转载请注明出处!

 

你可能感兴趣的文章
Mysql Row_Format 参数讲解
查看>>
mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
查看>>
MySQL Server 5.5安装记录
查看>>
mysql server has gone away
查看>>
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
MYSQL sql语句针对数据记录时间范围查询的效率对比
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>
mysql union orderby 无效
查看>>
mysql v$session_Oracle 进程查看v$session
查看>>
mysql where中如何判断不为空
查看>>
MySQL Workbench 使用手册:从入门到精通
查看>>
mysql workbench6.3.5_MySQL Workbench
查看>>
MySQL Workbench安装教程以及菜单汉化
查看>>
MySQL Xtrabackup 安装、备份、恢复
查看>>
mysql [Err] 1436 - Thread stack overrun: 129464 bytes used of a 286720 byte stack, and 160000 bytes
查看>>
MySQL _ MySQL常用操作
查看>>
MySQL – 导出数据成csv
查看>>