1.手动安装jar到到maven仓库
mvn install:install-file -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=18.8 -Dpackaging=jar -Dfile=C:/Users/29265/Desktop/aspose-words-19.5jdk.jar
2.写一个方法类
public class AsposeWordsUtils {
/**
* Word转PDF操作
*@param sourcerFile 源文件
*@param targetFile 目标文件
*/
public static void doc2pdf(String sourcerFile,String targetFile) {
try {
long old = System.currentTimeMillis();
File file = new File(targetFile); //新建一个空白pdf文档
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(sourcerFile); //sourcerFile是将要被转化的word文档
doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
os.close();
long now = System.currentTimeMillis();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
} catch (Exception e) {
e.printStackTrace();
}
}
//测试Word转PDF
public static void main(String[] args) {
doc2pdf("C:\\Users\\29265\\Desktop\\1.docx","C:\\Users\\29265\\Desktop\\1.pdf");
}
}
参考:https://blog.csdn.net/weixin_39643007/article/details/93341886