OpenOffice实现word转pdf

下载OpenOffice

https://soft.wsyhn.com/soft/OpenOffice_4.1.14.exe

启动命令

复制代码
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

pom

xml 复制代码
  <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>juh</artifactId>
            <version>4.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>unoil</artifactId>
            <version>4.1.2</version>
        </dependency>

        <dependency>
            <groupId>com.artofsolving</groupId>
            <artifactId>jodconverter</artifactId>
            <version>2.2.2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/jodconverter-2.2.2.jar</systemPath>
        </dependency>

jodconverter 需要手动下载
https://gitcode.com/open-source-toolkit/9aa3c

java

java 复制代码
  @Test
    void wordToPdf2() {
        try {
            wordToPdf("E:\\tmp\\固定取水户中南剧场每月现场勘验 (2).docx", "E:\\tmp\\固定取水户中南剧场每月现场勘验 (2).pdf");
        } catch (ConnectException e) {
            throw new RuntimeException(e);
        }
    }
     public static void wordToPdf(String docFile, String pdfFile) throws ConnectException {
        long old = System.currentTimeMillis();
// 源文件目录
        File inputFile = new File(docFile);
        System.out.println(inputFile.getName());
// 输出文件目录
        File outputFile = new File(pdfFile);
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().exists();
        }
// 连接openoffice服务
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(
                "127.0.0.1", 8100);
        //"192.168.124.201", 8100);
        connection.connect();
        // 转换word到pdf
        DocumentConverter converter = new StreamOpenOfficeDocumentConverter(
                connection);
        converter.convert(inputFile, outputFile);
// 关闭连接
        connection.disconnect();
        long now = System.currentTimeMillis();
        System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
    }

参考文章
https://blog.csdn.net/feifeifei666/article/details/136396279

相关推荐
Java面试题总结1 小时前
基于 Java 的 PDF 文本水印实现方案(iText7 示例)
java·python·pdf
傻啦嘿哟3 小时前
Python操作PDF页面详解:删除指定页的完整方案
开发语言·python·pdf
fie88895 小时前
基于C#的推箱子小游戏实现
开发语言·c#
.房东的猫6 小时前
ERP(金蝶云星空)开发【业务数据中心创建和注册】
c#
bugcome_com6 小时前
C# 进阶核心知识点汇总|多项目开发 + 委托 + Lambda + 事件一次吃透
c#
weixin_416660077 小时前
技术分析:豆包生成带公式文案导出Word乱码的底层机理
人工智能·word·豆包
骆驼爱记录8 小时前
Word样式库不显示的8种修复方法
word·wps·新人首发
苍煜8 小时前
超简单 poi-tl 学习博客:从0到1掌握Word生成(无需模板+模板填充)
学习·word
SunflowerCoder8 小时前
基于插件化 + Scriban 模板引擎的高效 HTTP 协议中心设计
http·c#
请为小H留灯10 小时前
Word论文 封面、目录、页码设置步骤!(2026详细版教程)
毕业设计·word·论文格式