1. word转pdf
1.1 引入documents4j依赖
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-local</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.0.3</version>
</dependency>
相关代码 工具类
public static void documents4jWordToPdf(String sourcePath, String targetPath) {
File inputWord = new File(sourcePath);
File outputFile = new File(targetPath);
try {
InputStream docxInputStream = new FileInputStream(inputWord);
OutputStream outputStream = new FileOutputStream(outputFile);
IConverter converter = LocalConverter.builder().build();
converter.convert(docxInputStream)
.as(DocumentType.DOCX)
.to(outputStream)
.as(DocumentType.PDF).execute();
outputStream.close();
} catch (Exception e) {
log.error("[documents4J] word转pdf失败:{}", e.toString());
}
}
1.2 aspose-words
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = DocToPDFUtils.class.getClassLoader().getResourceAsStream("\\license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void asposeWordToPdf(String sourcePath,String targetPath){
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!getLicense()) {
return;
}
try {
long old = System.currentTimeMillis();
// 新建一个空白pdf文档
File file = new File(targetPath);
FileOutputStream os = new FileOutputStream(file);
// Address是将要被转化的word文档
Document doc = new Document(sourcePath);
// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
doc.save(os, SaveFormat.PDF);
long now = System.currentTimeMillis();
log.info(sourcePath+"生成共耗时:" + ((now - old) / 1000.0) + "秒");
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
poi问题记录
poi和poi-tl版本冲突问题
利用插件重新打包命名使两者共存