JAVA合并多个word文档根据文章标题生成目录
下载jar包,或者引入相关maven
maven引入相关地址:https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html
jar包下载地址:点击下载
如果不知道怎么引入jar包到项目中,请面向百度。
如果word文档中已经设置了大纲就直接使用一段代码即可
public static void main(String[]args){
//加载测试文档
Document doc = new Document("测试文件.docx");
//在文档最前面插入一个段落,写入文本并格式化
Paragraph parainserted = new Paragraph(doc);
TextRange tr= parainserted.appendText("目 录");
tr.getCharacterFormat().setBold(true);
tr.getCharacterFormat().setTextColor(Color.gray);
doc.getSections().get(0).getParagraphs().insert(0,parainserted);
parainserted.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//手动设置文档中指定段落的大纲级别
doc.getSections().get(0).getParagraphs().get(1).applyStyle(BuiltinStyle.Heading_1);
doc.getSections().get(0).getParagraphs().get(2).applyStyle(BuiltinStyle.Heading_2);
doc.getSections().get(0).getParagraphs().get(4).applyStyle(BuiltinStyle.Heading_2);
doc.getSections().get(0).getParagraphs().get(6).applyStyle(BuiltinStyle.Heading_2);
doc.getSections().get(0).getParagraphs().get(12).applyStyle(BuiltinStyle.Heading_2);
doc.getSections().get(0).getParagraphs().get(13).applyStyle(BuiltinStyle.Heading_3);
doc.getSections().get(0).getParagraphs().get(14).applyStyle(BuiltinStyle.Heading_3);
doc.getSections().get(0).getParagraphs().get(15).applyStyle(BuiltinStyle.Heading_3);
doc.getSections().get(0).getParagraphs().get(17).applyStyle(BuiltinStyle.Heading_1);
doc.getSections().get(0).getParagraphs().get(18).applyStyle(BuiltinStyle.Heading_2);
//添加目录
doc.getSections().get(0).getParagraphs().get(0).appendTOC(1,3);
//更新目录表
doc.updateTableOfContents();
//保存文档
doc.saveToFile("目录.docx",FileFormat.Docx_2010);
}
demo来源地址:https://www.e-iceblue.cn/spiredocforjavaformfield/add-word-toc-in-java.html
下面这个是没有设置大纲的文档生成目录,我的方案是把每页第一段设置为大纲,然后生成目录。
下面的是合并文档然后生成目录和页码的代码逻辑。
/**
* 先临时生成一个合并完成后的docx格式文档,doc会出现乱码。
* @param pathList 所有需要合并的文档的绝对路径
* @param savePath 一个路径,但是没有文件的后缀,之后进行拼接。
* @return 状态,是否保存成功
*/
public static boolean mergeWordToPdf(List<String> pathList, String savePath){
//判断是否为pdf文件后缀的路径
// String[] split = savePath.split("\\.");
// if (!"pdf".equals(split[split.length-1])) {
// System.out.println("请给一个以pdf保存路径结尾的路径");
// return false;
// }
//保存合并完成后临时存放的文件
String file = savePath + ".docx";
File newfile = new File(file);
try {
//判断是否存在,存在则删除
if (newfile.exists()) {
newfile.delete();
}
newfile.createNewFile();
//创建一个新的doc文件
Document doc = new Document(file);
int count = 0;
// 进行合并
for (String filePath : pathList) {
// 获取文档的路径,然后合并
count++;
Document doc2 = new Document();
doc2.loadFromFile(filePath);
for (int j = 0; j < doc2.getSections().getCount(); j++) {
doc.getSections().add(doc2.getSections().get(j).deepClone());
}
} // 在开头创建一个目录页
ParagraphStyle title1style = new ParagraphStyle(doc);
title1style.setName("TL1");
title1style.getParagraphFormat().setOutlineLevel(OutlineLevel.Level_1);
doc.getStyles().add(title1style);
Section sec = doc.getSections().get(0);
//设置边距
sec.getPageSetup().getMargins().setTop(71.882f);
sec.getPageSetup().getMargins().setBottom(71.882f);
sec.getPageSetup().getMargins().setLeft(90f);
sec.getPageSetup().getMargins().setRight(90f);
sec.getParagraphs().get(0).applyStyle(title1style.getName()); //循环遍历每一页的标题,并添加到目录页中
for (int i = 1; i <= count; i++) {
sec = doc.getSections().get(i);
sec.getParagraphs().get(0).applyStyle(title1style.getName());
}
sec = doc.getSections().get(0);
Paragraph para = new Paragraph(doc);
sec.getParagraphs().insert(0, para);
TableOfContent toc = para.appendTOC(1, 3);
toc.setUseHeadingStyles(false);
toc.setUseHyperlinks(true);
toc.setUseTableEntryFields(false);
toc.setRightAlignPageNumbers(true);
toc.setTOCLevelStyle(1, title1style.getName());
doc.isUpdateFields();
doc.updateTableOfContents(); //设置目录的字体
TextRange range = para.appendText("目录");
range.getCharacterFormat().setFontName("宋体");
range.getCharacterFormat().setFontSize(16);
para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
sec.getParagraphs().insert(0, para);
for (int i = 0; i < sec.getParagraphs().getCount(); i++) {
Paragraph p = sec.getParagraphs().get(i);
if (p.getStyleName().equals("TOC1")) {
for (int j = 0; j < p.getChildObjects().getCount(); j++) {
if (p.getChildObjects().get(j).getDocumentObjectType().equals(DocumentObjectType.Text_Range)) {
TextRange range0 = (TextRange) p.getChildObjects().get(j);
range0.getCharacterFormat().setFontName("宋体");
range0.getCharacterFormat().setBold(false);
}
}
}
}
//删除页眉
for (int i = 1; i <= count; i++) {
ParagraphCollection paragraphsHeader = doc.getSections().get(i).getHeadersFooters().getHeader().getParagraphs();
if (paragraphsHeader.getCount() > 0) {
paragraphsHeader.removeAt(0);
}
doc.getSections().get(i).getHeadersFooters().getFirstPageFooter().getChildObjects().clear();
doc.getSections().get(i).getHeadersFooters().getOddFooter().getChildObjects().clear();
} //添加文字、页码域和总页数域到段落
Paragraph paragraph = doc.getSections().get(0).getHeadersFooters().getFirstPageFooter().addParagraph();
paragraph.appendField("page number", FieldType.Field_Page);
paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right); Paragraph paragraph1 = doc.getSections().get(0).getHeadersFooters().getOddFooter().addParagraph();
paragraph1.appendField("page number", FieldType.Field_Page);
paragraph1.getFormat().setHorizontalAlignment(HorizontalAlignment.Right); //在转换为pdf时出现字体便乱的情况,格式化字体后解决。如果不需要转换为pdf,此操作可以删除。
for (int a = 1; a <= count; a++) {
Section s = doc.getSections().get(a);
//更新全文的字体(不包括tbale里的)
for (int i = 1; i < s.getParagraphs().getCount(); i++) {
Paragraph p = s.getParagraphs().get(i);
for (int j = 0; j < p.getChildObjects().getCount(); j++) {
if (p.getChildObjects().get(j).getDocumentObjectType().equals(DocumentObjectType.Text_Range)) {
TextRange range0 = (TextRange) p.getChildObjects().get(j);
range0.getCharacterFormat().setFontName("宋体");
range0.getCharacterFormat().setBold(false);
}
}
}
TableCollection tables = s.getTables();
//更新table里字体
if (tables.getCount() > 0) {
updateTable(tables);
}
} //保存word文件
doc.saveToFile(file, FileFormat.Docx);
//转换为pdf,转换的代码在下一篇文章里,使用的不是同一个jar包,因为这个jar对生成pdf没有限制,准确的说是破*了。
//WordToPdfUtil.wordToPdf(file, savePath + ".pdf");return true;
}catch (Exception e){
e.printStackTrace();
}
return false;
}
可能每个人的需求都不一样,不过你可以去查看相关Api或者官方demo进行修改。
此产品版本是免费版的,我也是在用免费,除了只能单次识别25张一下的word和生成pdf有限制,其他的功能都和正式版差不多。
如果你几十个文档,每个文档几页,输出出来超过25页,那没关系,依然可以使用。别单个文档超过25页即可。
如果公司使用,请支持购买收费版。
转换pdf的文章路径:
https://www.cnblogs.com/hunmeng/p/11983882.html
JAVA合并多个word文档根据文章标题生成目录的更多相关文章
- C# 合并及拆分Word文档
本文简要分析一下如何如何使用C#简单实现合并和拆分word文档.平时我们在处理多个word文档时,可能会想要将两个文档合并为一个,或者是将某个文档的一部分添加到另一个文档中,有的时候也会想要将文档拆分 ...
- 用java将简单的word文档换成pdf文档
用java将简单的word文档换成pdf文档的方式很多,因为很多都没有实际测试过,所以这里就先泛泛的说一下 整体上来看分两种: 1.纯java代码实现,有很多优秀的开源软件可以用,比如poi,itex ...
- Java使用freemarker导出word文档
通过freemarker,以及JAVA,导出word文档. 共分为三步: 第一步:创建模板文件 第二步:通过JAVA创建返回值. 第三步:执行 分别介绍如下: 第一步: 首先创建word文档,按照想要 ...
- 合并两个word文档,保持样式不变
一.需求说明 例如将封面插入到word正文上方 二.导入依赖 <dependency> <groupId>org.apache.poi</groupId> < ...
- java 导出数据为word文档(保持模板格式)
导出数据到具体的word文档里面,word有一定的格式,需要保持不变 这里使用freemarker来实现: ①:设计好word文档格式,需要用数据填充的地方用便于识别的长字符串替换 如 aaaaa ...
- Java利用aspose-words将word文档转换成pdf(破解 无水印)
首先下载aspose-words-15.8.0-jdk16.jar包 http://pan.baidu.com/s/1nvbJwnv 引入jar包,编写Java代码 package doc; impo ...
- Java将数据写入word文档(.doc)
Java可用org.apache.poi包来操作word文档.org.apache.poi包可于官网上下载,解压后各jar作用如下图所示: 可根据需求导入对应的jar. 一.HWPFDocument类 ...
- Java 合并、拆分PDF文档
处理PDF文档时,我们可以通过合并的方式,来任意组几个不同的PDF文件或者通过拆分将一个文件分解成多个子文件,这样的好处是对文档的存储.管理很方便.下面将通过Java程序代码介绍具体的PDF合并.拆分 ...
- 用java语言通过POI实现word文档的按标题提取
最近有一个项目需要将一个word文档中的数据提取到数据库中.就去网上查了好多资料,最靠谱的就是用poi实现word文档的提取. 喝水不忘挖井人,我查了好多资料就这个最靠谱,我的这篇博客主要是借鉴htt ...
随机推荐
- HTML5+CSS:02用户注册表单
新的学期已开始接近两个月了,还记得是在国庆节那几天申请的博客账号,可过了一个月都还没开始写博客,(>_<)有点小偷懒了,不过,学习还是不能落下的,今写一个有点实践运用的关于 ...
- Java 高并发之魂
前置知识 了解Java基本语法 了解多线程基本知识 知识介绍 Synchronized简介:作用.地位.不控制并发的后果 两种用法:对象锁和类锁 多线程访问同步方法的7种情况:是否是static.Sy ...
- Python编程 Notepad++和Pycharm设置
Notepad++ 一.缩进 初学Python的同学需要注意python的缩进,官方鼓励用4个空格缩进, 这时需要注意Tab键与空格的区分.使用Notepad++的同学可以在设置-首选项-制表符宽度中 ...
- nginx篇中级用法之反向代理(七层调度)
环境: 两台后端web,一台代理服务器 web1:eth0:192.168.2.100/24 httpd做一个web web2:eth0:192.168.2.200/24 httpd做一个we ...
- [考试反思]0929csp-s模拟测试55:消逝
菜得过分. 面对T1的大板子不知所措,然后T2的贪心不小心把排序语句删了... T1这种大模板啊...其实我是觉得我能打出来的,然后先用一个小时码了一个2k. 然后做T2想贪心就出来了.十分钟码完T3 ...
- 基于 H5 Canvas 实现楼宇自控系统
前言 楼宇自控是指楼宇中电力设备,如电梯.水泵.风机.空调等,其主要工作性质是强电驱动.通常这些设备是开放性的工作状态,也就是说没有形成一个闭环回路.只要接通电源,设备就在工作,至于工作状态.进程.能 ...
- [ PyQt入门教程 ] PyQt5中数据表格控件QTableWidget使用方法
如果你想让你开发的PyQt5工具展示的数据显得整齐.美观.好看,显得符合你的气质,可以考虑使用QTableWidget控件.之前一直使用的是textBrowser文本框控件,数据展示还是不太美观.其中 ...
- Linux下mysql的主从复制配置
1.准备两台数据库环境,或者单台多实例环境,能正常启动和登录. 数据库的安装和多实例配置请参考https://www.cnblogs.com/qiuhom-1874/p/9757061.html. 2 ...
- spring+struts2引起的错误被记忆问题
标题表述的比较模糊,详细情况是这样的: 目前开发的一个管理系统,当使用出现异常时会自动跳转到错误页.其处理流程是“发生异常——跳转到错误处理action——错误页”. 但是出现了一个bug,即某个操作 ...
- T-SQL, Part I: LIKE Pattern
The basic usage of LIKE pattern: %: it would be placed at the end and/or the beginning of a string. ...