目前word转pdf比较好的实现方式是Jacob与Aspose.Words。
Jacob
- windows电脑安装wps。
- 下载软件包,里面含有jacob.jar与jacob*.dll 下载地址
-
- jacob.jar 放在 C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext目录下
- jacob*.dll 放在 C:\Program Files\Java\jdk1.8.0_171\jre\bin 目录下
public class Word2Pdf {
public static void main(String[] args) {
ActiveXComponent app = null;
String wordFile = "C:\\Users\\Admin\\Desktop\\test.docx";
String pdfFile = "C:\\Users\\Admin\\Desktop\\test.pdf";
System.out.println("开始转换...");
// 开始时间
long start = System.currentTimeMillis();
try {
// 打开word
app = new ActiveXComponent("Word.Application");
// 获得word中所有打开的文档
Dispatch documents = app.getProperty("Documents").toDispatch();
System.out.println("打开文件: " + wordFile);
// 打开文档
Dispatch document = Dispatch.call(documents, "Open", wordFile, false, true).toDispatch();
// 如果文件存在的话,不会覆盖,会直接报错,所以我们需要判断文件是否存在
File target = new File(pdfFile);
if (target.exists()) {
target.delete();
}
System.out.println("另存为: " + pdfFile);
// 另存为,将文档报错为pdf,其中word保存为pdf的格式宏的值是17
Dispatch.call(document, "SaveAs", pdfFile, 17);
// 关闭文档
Dispatch.call(document, "Close", false);
// 结束时间
long end = System.currentTimeMillis();
System.out.println("转换成功,用时:" + (end - start) + "ms");
}catch(Exception e) {
e.getMessage();
System.out.println("转换失败"+e.getMessage());
}finally {
// 关闭office
app.invoke("Quit", 0);
}
}
}
注意:此方法只能在windows环境下使用。
Aspose.Words
首先你需要有一个 aspose-words-15.8.0 的 jar 包,大家可以百度一下,我这里也整理了一份,关注公众号躺平闹钟,后台回复word2pdf获取。
- 第一步:给 jar 包找一个安身之处 因为是 maven 项目,先在项目根目录中创建一个 lib 文件,和 src 文件夹同级别,这里贴出我的项目结构,让大家一目了然。
- 第二步:给了它房子,就该让它干活了 打开 pom.xml,加入如下信息,这样我们就可以在项目中任意使用它了。
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/aspose-words-15.8.0-jdk16.jar</systemPath>
</dependency>
- 第三步:配置一下 License.xml,和水印说 byebye License.xml 百度一大把,这里贴出来博主使用没有问题的。License.xml 直接放到 resources 的根目录下即可。
<License>
<Data>
<Products>
<Product>Aspose.Total for Java</Product>
<Product>Aspose.Words for Java</Product>
</Products>
<EditionType>Enterprise</EditionType>
<SubscriptionExpiry>20991231</SubscriptionExpiry>
<LicenseExpiry>20991231</LicenseExpiry>
<SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
</Data>
<Signature>
sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=
</Signature>
</License>
- 核心的一步,是时候让 word 转 pdf 了!
这里就不多说废话了,直接贴代码,解释都在注释里。
public class AsposeUtil {
/**
* 加载license 用于破解 不生成水印
*/
@SneakyThrows
private static void getLicense() {
try (InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("License.xml")) {
License license = new License();
license.setLicense(is);
}
}
/**
* word转pdf
*
* @param wordPath word文件保存的路径
* @param pdfPath 转换后pdf文件保存的路径
*/
@SneakyThrows
public static void wordToPdf(String wordPath, String pdfPath) {
getLicense();
File file = new File(pdfPath);
try (FileOutputStream os = new FileOutputStream(file)) {
Document doc = new Document(wordPath);
doc.save(os, SaveFormat.PDF);
}
}
}
- 打包会遇到的一点小坑
当使用<scope>system</scope>引入依赖时,打包的时候要多注意,要加上如下配置,不然 springboot 是不会这种方式引入的 jar 进行打包的。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 打包时会将本地jar一起打包 -->
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
至此代码全部完成,aspose-words 使用起来非常简单,就是集成的时候有点小麻烦,不过按照上面的方法一点点来绝对没问题。