word搞了一天,找了无数的资料,以下这种方法是最好的。。。什么只能转3页了。。。什么字体库了。。。。各种坑
就三步:
第一步:下载jar包,然后放在这里
第二步:引入jar包,放在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>
第三步:写工具类
package com.xxx.util;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import lombok.SneakyThrows;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
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);
}
}
}
最后就能直接用了,可以参考我的
String inPath = "D:\\/java" + "\\/123456.docx";
String outPath = "D:\\/java" + "\\/123456.pdf";
AsposeUtil asposeUtil = new AsposeUtil();
asposeUtil.wordToPdf(wordPath,pdfPath);
以下可能就是你需要的jar包咯~
链接: https://pan.baidu.com/s/1A4OZKApDA-CgcpmN-XaCag 提取码: 2f66 复制这段内容后打开百度网盘手机App,操作更方便哦
觉得有帮助的话,帮点个赞哦~多谢