JAVA使用OpenOffice文件转换
下载jar包
maven中央仓库包不支持docx文件 所以不建议使用。jar包是为了方便链接
下载链接:https://nchc.dl.sourceforge.net/project/jodconverter/JODConverter/2.2.2/jodconverter-2.2.2.zip
解压后找到:jodconverter-2.2.2\jodconverter-2.2.2\lib\jodconverter-2.2.2.jar
放到本地maven仓库
1 | mvn install : install - file -Dfile=jodconverter-2.2.2.jar -DgroupId=com.artofsolving -DartifactId=jodconverter -Dversion=2.2.2 -Dpackaging=jar |
<dependency> <groupId>com.artofsolving</groupId> <artifactId>jodconverter</artifactId> <version>2.2.2</version> </dependency>
链接代码
// 文件输入输出流 FileInputStream in = new FileInputStream(inputFile);//new File("D:\\file\\c.doxc") FileOutputStream out = new FileOutputStream(outputFile);//new File("D:\\file\\a.pdf") // 创建连接 OpenOfficeConnection connection = new SocketOpenOfficeConnection(openOfficeHost, openOfficePost); connection.connect(); // 创建转换器 DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection); // 待转换文件名称及扩展名称 String officeName = inputFile.getName(); String officeExt = officeName.substring(officeName.lastIndexOf(".") + 1); // 文件格式 DefaultDocumentFormatRegistry factory = new DefaultDocumentFormatRegistry(); // 待转换文件 转换文件格式对象 DocumentFormat officeFormat = factory.getFormatByFileExtension(officeExt);//如果使用的2.2.1的jar包,要转换的word后缀是docx!这里会返回null DocumentFormat pdfFormat = factory.getFormatByFileExtension("pdf"); converter.convert(in, officeFormat, out, pdfFormat);//word转换PDF //断开连接 connection.disconnect();
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构