2019独角兽企业重金招聘Python工程师标准>>>

Office 2007 之后使用 XML 格式来存储文档数据,这个变化其实是非常不错的。如何操作这些XML生成文件呢?或许你还在使用 POI,也有可能你在使用已经放弃更新的 iText (只是doc已经放弃更新,PDF持续更新),但是我觉得,你可以选择使用 Docx4j,这是个不错的开源软件,国内很少使用而已。

下面就简单的使用 docx4j添加页码的代码做一个简单的介绍。

/*** 创建页脚(分页信息)*/public static void createFooter(WordprocessingMLPackage wordMLPackage) throws Exception {Relationship relationship = DocxUtil.createFootPart(wordMLPackage);DocxUtil.createFootReference(wordMLPackage, relationship);}/*** 创建页脚的组件*/public static Relationship createFootPart(WordprocessingMLPackage wordprocessingMLPackage)throws Exception {FooterPart footerPart = new FooterPart();Relationship rel = wordprocessingMLPackage.getMainDocumentPart().addTargetPart(footerPart);// After addTargetPart, so image can be added properlyfooterPart.setJaxbElement(createFooterPageNr());return rel;}/*** 创建页脚的组件*/public static void createFootReference(WordprocessingMLPackage wordprocessingMLPackage,Relationship relationship)throws InvalidFormatException {ObjectFactory factory = Context.getWmlObjectFactory();List<SectionWrapper> sections = wordprocessingMLPackage.getDocumentModel().getSections();SectPr sectPr = sections.get(sections.size() - 1).getSectPr();// There is always a section wrapper, but it might not contain a sectPrif (sectPr == null) {sectPr = factory.createSectPr();wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);sections.get(sections.size() - 1).setSectPr(sectPr);}// footer referencesFooterReference footerReference = factory.createFooterReference();footerReference.setId(relationship.getId());footerReference.setType(HdrFtrRef.DEFAULT);sectPr.getEGHdrFtrReferences().add(footerReference);}/*** 添加页脚,带有页码** @return 页脚对象*/public static Ftr createFooterPageNr() {ObjectFactory factory = Context.getWmlObjectFactory();Ftr ftr = factory.createFtr();P paragraph = factory.createP();RPr fontRPr = getRPr("宋体", "000000", "20", STHint.EAST_ASIA, false);R run = factory.createR();run.setRPr(fontRPr);paragraph.getContent().add(run);paragraph.getContent().add(getTextField("第"));paragraph.getContent().add(getFieldBegin());paragraph.getContent().add(getPageNumberField());paragraph.getContent().add(getFieldEnd());paragraph.getContent().add(getTextField("页"));paragraph.getContent().add(getTextField(" 总共"));paragraph.getContent().add(getFieldBegin());paragraph.getContent().add(getTotalPageNumberField());paragraph.getContent().add(getFieldEnd());paragraph.getContent().add(getTextField("页"));setCellContentStyle(paragraph, JcEnumeration.CENTER);ftr.getContent().add(paragraph);return ftr;}

生成 docx 文档,这个代码来自Git

    public static void main(String[] args) throws Exception {WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();//        mdp.addParagraphOfText("hello world");
//      mdp.addParagraphOfText("welcome");mdp.addStyledParagraphOfText("Title","hello world");mdp.addStyledParagraphOfText("Subtitle","welcome");mdp.addParagraphOfText("today is good day . welcome all students come here.");ProtectDocument protection = new ProtectDocument(wordMLPackage);protection.restrictEditing(STDocProtect.READ_ONLY, "foobaa");DocxUtil.createFooter(wordMLPackage);String filename = System.getProperty("user.dir") + "/OUT_hello.docx";Docx4J.save(wordMLPackage, new java.io.File(filename), Docx4J.FLAG_SAVE_ZIP_FILE);System.out.println("Saved " + filename);}

转载于:https://my.oschina.net/heweipo/blog/702784

Docx4j 添加页码相关推荐

  1. pdf文件添加页码方法介绍

    现在我们在工作的时候遇到的pdf文件也是比较多的,但是很多人不知道pdf文件怎么添加页码,现在给我们的pdf文件添加页码就需要使用到软件来进行操作,下面小编就为大家介绍一下pdf添加页码的方法 软件安 ...

  2. android标题栏添加按钮_[办公小技巧]Excel 添加页码,自定义和指定单元格页码添加...

    EXCEL在指定的单元格中插入页码 1. CTRL +F3 2. 选择新建 3. 输入名称:"纵当页" 输入引用位置:=IF(ISNA(MATCH(ROW(),GET.Docume ...

  3. idea在目录中前添加目录_[LaTeX Beamer] 为目录项添加页码

    本文已加入专栏文章目录,归入「进阶使用」文章系列. 本文介绍了为 beamer 文档类的目录条目添加页码的方法,该方法可用于一般情况下的 beamer 目录条目内容(不包含样式的定制)修改需求. be ...

  4. 手机ppt怎么添加页码_全网超详细的操作教程,手把手教你使用高效PPT小技巧!...

    相信同在职场中的你,同样离不开PPT.关于PPT的一些操作,如果不懂不会,非常浪费时间精力.于是,就会经常出现为PPT加班的事儿! 今天,我就为大家分享一个职场中经常会用到的两个PPT小技巧,那就是为 ...

  5. Word实用教程——五分钟教你如何在任意页开始添加页码

    最近在写一篇论文,但是在排版上遇到一点小问题,就是要加入页码,而且页码是从目录的下一页开始计数,于是我就在网上找如何在任意页添加页码.后来辗转终于搞定,真心觉得这一个小功能让微软做的如此的麻烦,真是活 ...

  6. speedoffice(Word)怎么添加页码

    在制作文件过程中,经常需要对文件添加页码,那么Word文档怎么添加页码呢?以最常用的speedoffice(Word)为例 1.首先,打开一份Word文档,点击"插入",找到&qu ...

  7. VBA代码为ppt添加页码进度条

    VBA代码为ppt添加页码进度条 fandroid 于 2015-05-25 15:31:28 发布 4617  已收藏 5 分类专栏: office 文章标签: vba ppt 版权 office ...

  8. ppt的操作——添加页码

    仅作为记录,大佬请跳过. 文章目录 添加页码 给ppt首页不显示页码 添加页码 即可. 参考:传送门 给ppt首页不显示页码 即:将首页的页码去除. 在首页中,点击插入--页眉和页脚--去掉幻灯片编号 ...

  9. 十二、在PPT中添加页码

    在PPT中添加页码 一.PPT添加页码 1.视图-幻灯片母版-页脚 2.插入-页眉和页脚-幻灯片编号-全部应用 结果:

最新文章

  1. mysql如何下载连接到visual_Visual Studio 2015 Community连接到Mysql
  2. [翻译] ObjectAL for iPhone and Mac(持续更新)
  3. c++语言程序设计案...,C++程序设计案.ppt
  4. Javascript(JS)中的大括号{}和中括号[]详解
  5. C++中std::function和std::bind
  6. Online Learning场景下实时新闻热点机器学习训练实践
  7. 每天学一点儿shell:Linux三剑客——sed命令
  8. 四层负载均衡与七层负载均衡
  9. c语言游戏编程网盘下载,C语言游戏编程 计算器(5分下载)
  10. python测试需要学什么_从手工测试到自动化测试需要学什么?
  11. nagios监控单网卡双IP
  12. PL/SQL工具执行SQL脚本文件
  13. RHEL4As RAC手记
  14. Ubuntu 18.04安装
  15. 轻松解决电脑死机故障攻略_软件
  16. 全网最透彻的Hash分析
  17. window10安装Mac虚拟机详细教程
  18. 网址二维码:如何制作网页链接二维码?
  19. Oracle删表报错ora00054,Oracle解锁,解决“ORA-00054:资源正忙”错误
  20. https 双向认证开发实践

热门文章

  1. 为什么 Linux 需要 Swapping
  2. 实现一个全链路监控平台很难吗?Pinpoint、skywalking、zipkin,哪个实现比较好?...
  3. 一个 SpringBoot 项目该包含哪些?
  4. 有了这款可视化工具,Java 应用性能调优 so easy。。。
  5. 是什么让数据科学家从优秀变得伟大?
  6. 从0到1构建数据科学竞赛知识体系,有夕,鱼佬,茂霖等竞赛大咖将特邀分享...
  7. 当代研究生应当掌握的5种Pytorch并行训练方法(单机多卡)
  8. 作为导师,我希望学生在毕业后主动拉黑我
  9. GTA 5 + AI = ?
  10. 2021年了,该拥有自己的深度学习框架了