Java使用JodConverter和OpenOffice实现Word转PDF
2019-08-14 15:09:34 来源:华启智能
要实现Word格式文档转成PDF输出,发现可以使用JodConverter和OpenOffice实现。
1.JodConverter介绍
LibreOffice / Apache OpenOffice
JodConverter GitHub项目地址:https://github.com/sbraconnier/jodconverter/
号称使用JodConverter和OpenOffice可以实现Word转换成PDF。
2.使用JodConverter实现的Mave项目依赖:
4.总结
理论上讲这样应该可以实现Word至PDF转换,Windows XP系统实际测试程序会卡死在构建OfficeManager,这个版本我一直没成功。在Win7系统是成功的。
LibreOffice / Apache OpenOffice
JodConverter GitHub项目地址:https://github.com/sbraconnier/jodconverter/
号称使用JodConverter和OpenOffice可以实现Word转换成PDF。
2.使用JodConverter实现的Mave项目依赖:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.openoffice/juh -->
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>juh</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openoffice/jurt -->
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>jurt</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openoffice/ridl -->
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>ridl</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openoffice/unoil -->
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>unoil</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-core -->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>4.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-local -->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>4.2.2</version>
</dependency>
</dependencies>
3.实现代码:
3.实现代码:
File inputFile = new File("D:\\test.doc");
File outputFile = new File("D:\\test20190811.pdf");
System.out.println("开始构建officeManager。。。。");
System.out.println("开始构建officeManager。。。。");
OfficeManager officeManager = null;
try {
officeManager = LocalOfficeManager.builder()
.install()
.portNumbers(2002)
.officeHome("C:\\Program Files\\OpenOffice 4")
.build();
System.out.println("启动服务。。。。");
officeManager.start();
officeManager.start();
System.out.println("开始转换。。。。");
// Convert
JodConverter
.convert(inputFile)
.as(DefaultDocumentFormatRegistry.DOCX)
.to(outputFile)
.as(DefaultDocumentFormatRegistry.PDF)
.execute();
System.out.println("转换完成。。。。");
} catch (OfficeException ex) {
System.out.println("异常:"+ex.getMessage());
Logger.getLogger(WordToPDF5.class.getName()).log(Level.SEVERE, null, ex);
} finally {
// Stop the office process
//officeManager.stop();
OfficeUtils.stopQuietly(officeManager);
System.out.println("停止服务。。。。");
}
System.out.println("程序结束");
4.总结
理论上讲这样应该可以实现Word至PDF转换,Windows XP系统实际测试程序会卡死在构建OfficeManager,这个版本我一直没成功。在Win7系统是成功的。