word目前应该是现在最主流的编辑软件了吧,基本每个人都会用到,功能也十分强大,应用人群广泛,但是他也存在一些问题,比如,不同软件或者不同操作系统之间传输时,格式会发生变化,这种变化很让人恼火。所以现在越来越多的人把word转换成pdf格式文件,以保证文件格式不发生变化。
如果只是1个Word文件转换成Pdf文件,简直so easy;10个Word文件转换成pdf文件,虽烦躁,但能忍;如果是将1000个word文件转换成pdf文件呢?这会估计一股无名之火直冲天灵盖,立马想摔电脑的冲动都有了。
所以今天突发奇想,想试试是否可以通过程序将docx批量转换成pdf文档,通过参考Apache poi java库以及docx4j组件,于是选择以docx4j组件来进行文档操作。
话不多说,开始干:
一、下载依赖 docx4j所有的依赖jar包使用maven去处理还是蛮简洁的:
代码语言:javascript复制<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-Internal</artifactId>
<version>8.2.4</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>8.2.4</version>
</dependency>
二、代码实现
代码语言:javascript复制
package com.convert.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import org.docx4j.Docx4J;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
public class ConvertTest {
public static void main(String[] args) {
word2pdf("D:\tran\2.doc", "D:\tran\2.pdf");
}
public static void word2pdf(String source, String target) {
try {
WordprocessingMLPackage pkg = Docx4J.load(new File(source));
Mapper fontMapper = new IdentityPlusMapper();
fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei"));
fontMapper.put("黑体", PhysicalFonts.get("SimHei"));
fontMapper.put("楷体", PhysicalFonts.get("KaiTi"));
fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));
fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));
fontMapper.put("等线", PhysicalFonts.get("SimSun"));
fontMapper.put("等线 Light", PhysicalFonts.get("SimSun"));
fontMapper.put("华文琥珀", PhysicalFonts.get("STHupo"));
fontMapper.put("华文隶书", PhysicalFonts.get("STLiti"));
fontMapper.put("华文新魏", PhysicalFonts.get("STXinwei"));
fontMapper.put("华文彩云", PhysicalFonts.get("STCaiyun"));
fontMapper.put("方正姚体", PhysicalFonts.get("FZYaoti"));
fontMapper.put("方正舒体", PhysicalFonts.get("FZShuTi"));
fontMapper.put("华文细黑", PhysicalFonts.get("STXihei"));
fontMapper.put("宋体扩展", PhysicalFonts.get("simsun-extB"));
fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
fontMapper.put("新細明體", PhysicalFonts.get("SimSun"));
pkg.setFontMapper(fontMapper);
Docx4J.toPDF(pkg, new FileOutputStream(target));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Docx4JException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
三、转换结果
代码语言:javascript复制SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Using pdbs 420=7mm
Using pdbs 420=7mm
有一点报错,不过并不影响pdf的生成,打开生成的pdf,内容也是完整的。算是完成了吧,只要再写一个for循环,去遍历所有的文档就可以了。但是后来发现转换下来的pdf数量少了10个,所有的文档并没有全都转换成功。
四、后续研究
排查一番,发现这些文档中有10个doc文档,就该就是这10个没有成功了,单独拎出来转换一下,结果就报错了:
代码语言:javascript复制SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
org.docx4j.openpackaging.exceptions.Docx4JException: This file seems to be a binary doc/ppt/xls, not an encrypted OLE2 file containing a doc/pptx/xlsx
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:612)
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:414)
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:287)
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:265)
at org.docx4j.openpackaging.packages.WordprocessingMLPackage.load(WordprocessingMLPackage.java:168)
at org.docx4j.Docx4J.load(Docx4J.java:232)
at com.convert.test.ConvertTest.word2pdf(ConvertTest.java:26)
at com.convert.test.ConvertTest.main(ConvertTest.java:19)
This file seems to be a binary doc/ppt/xls, not an encrypted OLE2 file containing a doc/pptx/xlsx
“此文件似乎是一个二进制文件doc/ppt/xls,而不是包含doc/pptx/xlsx的加密OLE2文件,经过验证docx4j并不能完美的支持所有的word文档,至少doc文档并不能支持。不知道你们有没有遇到过这个问题,又是怎么解决的