博客
关于我
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中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
MySQL中interactive_timeout和wait_timeout的区别
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中kill掉所有锁表的进程
查看>>
mysql中like % %模糊查询
查看>>
MySql中mvcc学习记录
查看>>
mysql中null和空字符串的区别与问题!
查看>>
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
查看>>
MYSQL中TINYINT的取值范围
查看>>
MySQL中UPDATE语句的神奇技巧,让你操作数据库如虎添翼!
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>
MySQL中一条SQL语句到底是如何执行的呢?
查看>>
MySQL中你必须知道的10件事,1.5万字!
查看>>
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>