直接上代码:

import java.io.FileOutputStream;
import java.math.BigInteger;import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFFootnotes;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdnRef;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHighlight;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHpsMeasure;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTUnderline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalAlignRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STFtnEdn;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHighlightColor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun;public class POI_创建脚注_S3_Test {public static void main(String[] args) throws Exception {POI_创建脚注_S3_Test t = new POI_创建脚注_S3_Test();t.testAddFootnotesToDocument("f:/saveFile/temp/sys_"+ System.currentTimeMillis() + ".docx");}public void testAddFootnotesToDocument(String savePath) throws Exception {XWPFDocument xdoc = new XWPFDocument();XWPFParagraph p2 = xdoc.createParagraph();setParagraphTextStyleInfo(p2, false, "测试", "微软雅黑", "000000", "22",false, false, false, false, 0, null, false, 0, null);BigInteger noteId = BigInteger.valueOf(1);addFootNote(xdoc, p2, noteId, "参见1.1.1", null, null, "微软雅黑", "FF0000","18", true, false, false, false, 0, null, false, 0,STVerticalAlignRun.SUPERSCRIPT);setParagraphTextStyleInfo(p2, true, "插", "微软雅黑", "000000", "22", false,false, false, false, 0, null, false, 0, null);noteId = noteId.add(BigInteger.ONE);addFootNote(xdoc, p2, noteId, "参见1.1.2", "[", "]", "微软雅黑", "000000","18", false, false, false, false, 0, null, false, 0,STVerticalAlignRun.SUPERSCRIPT);setParagraphTextStyleInfo(p2, true, "入脚注", "微软雅黑", "000000", "22",false, false, false, false, 0, null, false, 0, null);noteId = noteId.add(BigInteger.ONE);addFootNote(xdoc, p2, noteId, "参见1.1.3", "[", "]", "微软雅黑", "000000","18", false, false, false, false, 0, null, false, 0,STVerticalAlignRun.SUPERSCRIPT);saveDocument(xdoc, savePath);}public void addFootNote(XWPFDocument xdoc, XWPFParagraph p,BigInteger noteId, String noteContent, String notePrefix,String noteSuffix, String fontFamily, String colorVal,String fontSize, boolean isBlod, boolean isItalic,boolean isStrike, boolean isUnderLine, int underLineStyle,String underLineColor, boolean isHightLight, int hightLightValue,STVerticalAlignRun.Enum stRunEnum) {XWPFRun r1 = null;if (notePrefix != null) {r1 = p.createRun();setRunTextStyleInfo(r1, notePrefix, fontFamily, colorVal, fontSize,isBlod, isItalic, isStrike, isUnderLine, underLineStyle,underLineColor, isHightLight, hightLightValue, stRunEnum);}r1 = p.createRun();setRunTextStyleInfo(r1, null, fontFamily, colorVal, fontSize, isBlod,isItalic, isStrike, isUnderLine, underLineStyle,underLineColor, isHightLight, hightLightValue, stRunEnum);CTFtnEdnRef fr = r1.getCTR().addNewFootnoteReference();fr.setId(noteId);if (noteSuffix != null) {r1 = p.createRun();setRunTextStyleInfo(r1, noteSuffix, fontFamily, colorVal, fontSize,isBlod, isItalic, isStrike, isUnderLine, underLineStyle,underLineColor, isHightLight, hightLightValue, stRunEnum);}XWPFFootnotes footnotes = xdoc.createFootnotes();CTFtnEdn ctNote = null;CTP ctp = null;XWPFParagraph p2 = null;ctNote = CTFtnEdn.Factory.newInstance();ctNote.setId(noteId);ctNote.setType(STFtnEdn.NORMAL);ctp = ctNote.addNewP();p2 = new XWPFParagraph(ctp, xdoc);if (notePrefix != null) {r1 = p2.createRun();setRunTextStyleInfo(r1, notePrefix, fontFamily, colorVal, fontSize,isBlod, isItalic, isStrike, isUnderLine, underLineStyle,underLineColor, isHightLight, hightLightValue, stRunEnum);}r1 = p2.createRun();setRunTextStyleInfo(r1, null, fontFamily, colorVal, fontSize, isBlod,isItalic, isStrike, isUnderLine, underLineStyle,underLineColor, isHightLight, hightLightValue, stRunEnum);r1.getCTR().addNewFootnoteRef();if (noteSuffix != null) {r1 = p2.createRun();setRunTextStyleInfo(r1, noteSuffix, fontFamily, colorVal, fontSize,isBlod, isItalic, isStrike, isUnderLine, underLineStyle,underLineColor, isHightLight, hightLightValue, stRunEnum);}r1 = p2.createRun();CTText ctText = r1.getCTR().addNewT();ctText.setStringValue(" ");ctText.setSpace(SpaceAttribute.Space.PRESERVE);r1 = p2.createRun();setRunTextStyleInfo(r1, noteContent, fontFamily, colorVal, fontSize,isBlod, isItalic, isStrike, isUnderLine, underLineStyle,underLineColor, isHightLight, hightLightValue, stRunEnum);footnotes.addFootnote(ctNote);}public void setRunTextStyleInfo(XWPFRun pRun, String content,String fontFamily, String colorVal, String fontSize,boolean isBlod, boolean isItalic, boolean isStrike,boolean isUnderLine, int underLineStyle, String underLineColor,boolean isHightLight, int hightLightValue,STVerticalAlignRun.Enum stRunEnum) {if (content != null) {pRun.setText(content);}CTRPr pRpr = null;if (pRun.getCTR() != null) {pRpr = pRun.getCTR().getRPr();if (pRpr == null) {pRpr = pRun.getCTR().addNewRPr();}}if (fontFamily != null) {// 设置字体CTFonts fonts = pRpr.isSetRFonts() ? pRpr.getRFonts() : pRpr.addNewRFonts();fonts.setAscii(fontFamily);fonts.setEastAsia(fontFamily);fonts.setHAnsi(fontFamily);}if (fontSize != null) {// 设置字体大小CTHpsMeasure sz = pRpr.isSetSz() ? pRpr.getSz() : pRpr.addNewSz();sz.setVal(new BigInteger(fontSize));CTHpsMeasure szCs = pRpr.isSetSzCs() ? pRpr.getSzCs() : pRpr.addNewSzCs();szCs.setVal(new BigInteger(fontSize));}if (colorVal != null) {pRun.setColor(colorVal);}// 设置字体样式if (isBlod) {pRun.setBold(isBlod);}if (isItalic) {pRun.setItalic(isItalic);}if (isStrike) {pRun.setStrike(isStrike);}if (colorVal != null) {pRun.setColor(colorVal);}// 设置下划线样式if (isUnderLine) {CTUnderline u = pRpr.isSetU() ? pRpr.getU() : pRpr.addNewU();u.setVal(STUnderline.Enum.forInt(Math.abs(underLineStyle % 19)));if (underLineColor != null) {u.setColor(underLineColor);}}// 设置字突出显示文本if (isHightLight) {if (hightLightValue > 0 && hightLightValue < 17) {CTHighlight hightLight = pRpr.isSetHighlight() ? pRpr.getHighlight() : pRpr.addNewHighlight();hightLight.setVal(STHighlightColor.Enum.forInt(hightLightValue));}}if (stRunEnum != null) {CTVerticalAlignRun ctV = CTVerticalAlignRun.Factory.newInstance();ctV.setVal(stRunEnum);pRpr.setVertAlign(ctV);}}public void setParagraphTextStyleInfo(XWPFParagraph p, boolean isNew,String content, String fontFamily, String colorVal,String fontSize, boolean isBlod, boolean isItalic,boolean isStrike, boolean isUnderLine, int underLineStyle,String underLineColor, boolean isHightLight, int hightLightValue,STVerticalAlignRun.Enum stRunEnum) {XWPFRun pRun = null;if (isNew) {pRun = p.createRun();} else {if (p.getRuns() != null && p.getRuns().size() > 0) {pRun = p.getRuns().get(0);} else {pRun = p.createRun();}}if (content != null) {pRun.setText(content);}CTRPr pRpr = null;if (pRun.getCTR() != null) {pRpr = pRun.getCTR().getRPr();if (pRpr == null) {pRpr = pRun.getCTR().addNewRPr();}}// 设置字体CTFonts fonts = pRpr.isSetRFonts() ? pRpr.getRFonts() : pRpr.addNewRFonts();fonts.setAscii(fontFamily);fonts.setEastAsia(fontFamily);fonts.setHAnsi(fontFamily);// 设置字体大小CTHpsMeasure sz = pRpr.isSetSz() ? pRpr.getSz() : pRpr.addNewSz();sz.setVal(new BigInteger(fontSize));CTHpsMeasure szCs = pRpr.isSetSzCs() ? pRpr.getSzCs() : pRpr.addNewSzCs();szCs.setVal(new BigInteger(fontSize));if (colorVal != null) {pRun.setColor(colorVal);}// 设置字体样式if (isBlod) {pRun.setBold(isBlod);}if (isItalic) {pRun.setItalic(isItalic);}if (isStrike) {pRun.setStrike(isStrike);}if (colorVal != null) {pRun.setColor(colorVal);}// 设置下划线样式if (isUnderLine) {CTUnderline u = pRpr.isSetU() ? pRpr.getU() : pRpr.addNewU();u.setVal(STUnderline.Enum.forInt(Math.abs(underLineStyle % 19)));if (underLineColor != null) {u.setColor(underLineColor);}}// 设置突出显示文本if (isHightLight) {if (hightLightValue > 0 && hightLightValue < 17) {CTHighlight hightLight = pRpr.isSetHighlight() ? pRpr.getHighlight() : pRpr.addNewHighlight();hightLight.setVal(STHighlightColor.Enum.forInt(hightLightValue));}}if (stRunEnum != null) {CTVerticalAlignRun ctV = CTVerticalAlignRun.Factory.newInstance();ctV.setVal(stRunEnum);pRpr.setVertAlign(ctV);}}public void saveDocument(XWPFDocument document, String savePath)throws Exception {FileOutputStream fos = new FileOutputStream(savePath);document.write(fos);fos.close();}
}

结果为:


 

        转载请注明原处,原文链接:http://53873039oycg.iteye.com/blog/2158635  ,谢谢。
--------------------------------------------顺便说一句--------------------------------------------
        使用上述方法创建批注(comment)或者尾注(endnote)是不可行的,到目前为止,尚未发现有效的方法使用poi新增批注或者尾注,如果有这方便的需求,请使用docx4j,至少docx4j在git上面的samples里包含了怎么创建comment,其他的如怎么使用poi创建书签(bookmark)这种简单的功能就不写了,修改书签网上也有现成的代码。
        全文完。

[简单]poi 创建word2007脚注(FootNote)相关推荐

  1. 简单poi创建execl

    Workbook workbook = new HSSFWorkbook();// 创建一个Excel文件 Workbook workbook = new XSSFWorkbook();// 创建一个 ...

  2. JEPLUS之简单流程创建——JEPLUS软件快速开发平台

    为什么80%的码农都做不了架构师?>>>    JEPLUS之简单流程创建      在我们平时的业务场景必不可少的就是工作流,这篇我来带大家创建一个简答的请假审批的流程. 一.效果 ...

  3. poi xwpf 转换成 html,Apache POI创建的docx文档无法转化成html

    当我通过word软件创建文档的时候 FileInputStream fileInputStream = new FileInputStream(sourceFileName); //当你确定该文件可信 ...

  4. 【poi xlsx报错】使用POI创建xlsx无法打开

    如果使用的XSSFWorkbook创建的xls,打开的时候会有这样的提示: 这样 XSSFWorkbook 和HSSFWorkbook的区别. HSSF - 提供读写Microsoft Excel X ...

  5. 使用Apache下poi创建和读取excel文件

    一:使用apache下poi创建excel文档 1 @Test 2 /* 3 * 使用Apache poi创建excel文件 4 */ 5 public void testCreateExcel() ...

  6. 使用POI创建Excel无法打开

    如果使用的XSSFWorkbook创建的xls,打开的时候会有这样的提示: 这样 XSSFWorkbook 和HSSFWorkbook的区别. 使用POI创建一个新的xlsx,提示创建成功,但是打开x ...

  7. ssas脚本组织程序_SSAS(简单cube创建过程)

    SSAS(简单cube创建过程) 一. 创建AnalysisServices项目 · 单击"开始",依此指向"所有程序"和Microsoft SQL Serve ...

  8. 最简单的创建多级目录文件夹

    最简单的创建多级目录文件夹 ,莫过于使用SHCreateDirectoryEx,不用CreateDirectory一级一级创建文件夹,下面示例(在VS2010下): 新建一个Wind32的window ...

  9. 如何简单的创建一个多人在线聊天室

    学习目标: 在本教程中,我们将要使用PHP和jQuery创建一个简单的在线聊天工具. 这种实用性的模块对于你想要有实时在线客户支持系统的网站可以说是完美. 废话不多说直接开始. 步骤1:HTML的代码 ...

最新文章

  1. MaxCompute动态更新表中某个(多个)字段的数据
  2. 如何使用HTML5,CSS3和PHP创建一个联系表格
  3. sql 日期和当前日期时间差_如何通过excel获取当前的日期和时间
  4. 深入源码分析Java线程池的实现原理
  5. python控制语句字符串截取_菜鸟python---流程控制语句
  6. 《Unix网络编程(第3版)》代码编译的一些问题
  7. QT 框架搭建,用最原始的方法实现简单的塔防游戏 | 原力计划
  8. 突发!程序员!快别闷头学Python了!
  9. AS3文本框的操作,为密码框添加按钮
  10. 超简洁又强大的幻灯片JS、CSS代码,兼容性强
  11. 盘点17个能帮助企业运营完美服务的AI聊天机器人
  12. 39 个奇葩代码注释,看完笑哭了。
  13. 云计算教程:小型云计算平台怎么搭建?
  14. Linux - 麒麟17.04 删除桌面图标(计算机,主文件夹,回收站)与 Cairo-Dock 安装配置
  15. 快捷指令通知运行html,快捷指令怎样运行这段,一个书签
  16. CleanMyMac X2023Mac电脑空间内存清理工具
  17. 【原创】2012年3月24日 单骑至大峪 日志
  18. ViewPager中ViewPager的使用
  19. 论文阅读_DeepWalk
  20. Dual attention network for scene segmentation

热门文章

  1. uni-app 超详细教程(一)(从菜鸟到大佬)
  2. 计算机数字键盘无法输入数字,电脑数字键打不出数字怎么办?电脑数字键打不出数字的修复方法...
  3. java基础扫盲_Java学习:扫盲
  4. Injective Protocol官方文档翻译(九) -清盘、清算(Liquidation)
  5. x264 2pass编码说明
  6. 学习计算机英语总结,英语学习方法总结
  7. jq 修改元素css伪类样式
  8. sed修炼系列(一):花拳绣腿之入门篇
  9. 2022年世界最强与最弱的护照:日本和新加坡并列第一,德国和韩国并列第二,美国和英国排名回升 | 美通社头条...
  10. 到底什么是数据中台?