建议下载4.1.6版本
找到openoffice安装目录下OpenOffice 4\program>soffice运行cmd,运行命令soffice -headless -accept=“socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard
package com.ry.controller; import java.io.File; import java.util.Date; import com.artofsolving.jodconverter.DocumentConverter; import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; public class PDTT { public static void main(String[] args) { // 找到openoffice安装目录下OpenOffice 4\program>soffice运行cmd // 开启open office命令:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard // 获取开始时间 Date startDate = new Date(); // 目标文件(这里写需要被转换的文件地址和文件名) String sourceFile = "C:\\Users\\86199\\Desktop\\aaa.doc"; // 生成的文件(这里写转换为pdf的文件地址和文件名) String destFile = "C:\\Users\\86199\\Desktop\\测试.pdf"; try { // 运行转换方法 System.out.println(office2PDF(sourceFile, destFile)); } catch (Exception e) { e.printStackTrace(); } // 获取结束时间 Date endDate = new Date(); System.out.println("总耗时:" + (endDate.getTime() - startDate.getTime())); } /* 具体的转换方法 */ public static int office2PDF(String sourceFile, String destFile) throws Exception { try { File inputFile = new File(sourceFile); // 判断文件是否存在 if (!inputFile.exists()) { System.out.println("源文件不存在"); return -1;// 找不到源文件, 则返回-1 } // 如果目标路径不存在, 则新建该路径 File outputFile = new File(destFile); if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } // 连接到在端口8100上运行的OpenOffice.org实例 OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100); connection.connect(); // 进行转换 DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(inputFile, outputFile); // 关闭连接 connection.disconnect(); // 执行成功 System.out.println("转化成功"); return 0; } catch (Exception e) { e.printStackTrace(); } // 失败时返回1 return 1; } }
waven仓库的配置依赖信息
<!-- Apache Utils --> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.5</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <!-- openoffice--> <dependency> <groupId>com.artofsolving</groupId> <artifactId>jodconverter</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>ridl</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>jurt</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>juh</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>unoil</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.5.20</version> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.artofsolving.jodconverter/jodconverter-core --> <dependency> <groupId>org.artofsolving.jodconverter</groupId> <artifactId>jodconverter-core</artifactId> <version>3.0-beta-4</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
需要注意的问题:
由于依赖版本原因转换不了docx文件。
到此这篇关于Java 使用openoffice进行word转换为pdf的方法步骤的文章就介绍到这了,更多相关Java openoffice word转换为pdf内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
免责声明:本站发布的内容(图片、视频和文字)以原创、来自互联网转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:ts@56dr.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
Copyright © 2009-2021 56dr.com. All Rights Reserved. 特网科技 版权所有 珠海市特网科技有限公司 粤ICP备16109289号
域名注册服务机构:阿里云计算有限公司(万网) 域名服务机构:烟台帝思普网络科技有限公司(DNSPod) CDN服务:阿里云计算有限公司 中国互联网举报中心 增值电信业务经营许可证B2