这个工具类全面支持DOC, DOCX,OOXML, RTF HTML,OpenDocument,PDF, EPUB, XPS,SWF 相互转换
1. jar包下载
百度云盘 提取码:2ero
在下载的文件夹中使用maven 打包
mvn install:install-file -Dfile=aspose-words-19.2.jar -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=19.2 -Dpackaging=jar
打包好的依赖放入自己的仓库里
2在项目中进行依赖
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>19.2</version>
</dependency>
import com.aspose.words.Document;
import java.io.File;
import java.io.FileOutputStream;
import java.time.Duration;
import java.time.LocalDateTime;
public class FileUtils {
/**
* Word 转换 Pdf
* @param inPath 文件路径
* @param outPath 输出pdf的路径
*/
public static void doc2pdf(String inPath, String outPath) {
System.out.println(LocalDateTime.now()+"===============Word 转换 Pdf=================");
/*if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生(15.8版本,已更改为19.2版本,故此不需要验证了)
return;
}*/
try {
LocalDateTime startDate = LocalDateTime.now();
File file = new File(outPath); // 新建一个pdf文档
FileOutputStream os = new FileOutputStream(file);
/** 这里需要指定Linux服务器上从本地windows上传的字体库,否则生成乱码
* 【/usr/share/fonts/windowsFonts/】位于Linux服务器上的windows字体,作为word转pdf用
*/
//FontSettings.getDefaultInstance().setFontsFolder("/usr/share/fonts/windowsFonts/", true);
Document doc = new Document(inPath); // Address是将要被转化的word文档
doc.save(os, com.aspose.words.SaveFormat.PDF);// 全面支持DOC, DOCX,
// OOXML, RTF HTML,
// OpenDocument,
// PDF, EPUB, XPS,
// SWF 相互转换
LocalDateTime endDate = LocalDateTime.now();
Duration duration = Duration.between(startDate,endDate);
System.out.println(endDate+"---PDF转化现在是---------------- 用时:"+duration.toMillis()+" 毫秒");
os.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("--------PDF转换出异常---------");
}
}
}