这个工具类全面支持DOC, DOCX,OOXML, RTF HTML,OpenDocument,PDF, EPUB, XPS,SWF 相互转换
转化时需要的jar包和pom:
jar包地址如下:链接:https://pan.baidu.com/s/1w1aMvTYqMKRhrRtum88HbQ 提取码:y1qg
<!-- https://mvnrepository.com/artifact/com.aspose/aspose-words -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>19.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/aspose-words-19.2-jdk16.jar</systemPath>
</dependency>
1.word转PDF的工具类代码如下:
public class FileUtils {
/**
* Word 转换 Pdf
* @param inPath
* @param outPath
*/
public static void doc2pdf(String inPath, String outPath) {
System.out.println("===============Word 转换 Pdf=================");
/*if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生(15.8版本,已更改为19.2版本,故此不需要验证了)
return;
}*/
try {
long old = System.currentTimeMillis();
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文档
System.out.println("doc"+doc);
doc.save(os, com.aspose.words.SaveFormat.PDF);// 全面支持DOC, DOCX,
// OOXML, RTF HTML,
// OpenDocument,
// PDF, EPUB, XPS,
// SWF 相互转换
long now = System.currentTimeMillis();
// 将时间对象格式化为字符串
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
String strDate = format.format(date);
System.out.println("---PDF转化现在是--------"+strDate+"------------");
os.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("--------PDF转换出异常---------");
}
}
}
调用的样例:
public static void main( String[] args ) {
String addr1 = "E:\\NYQX_BQD.docx";
String addr2 = "E:\\NYQX_BQD.pdf";
FileUtils.doc2pdf(addr1,addr2);
}