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仓库

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();
posted @ 2023-04-13 11:50  大萝卜萌萌哒  阅读(55)  评论(0编辑  收藏  举报