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

相关推荐
故事不长丨30 分钟前
C#集合:解锁高效数据管理的秘密武器
开发语言·windows·c#·wpf·集合·winfrom·字典
jghhh012 小时前
基于C#实现与三菱FX系列PLC串口通信
开发语言·算法·c#·信息与通信
故事不长丨2 小时前
C#队列深度剖析:解锁高效编程的FIFO密码
visualstudio·c#·wpf·多线程·winfrom·队列·queue
李小白杂货铺2 小时前
从基于pdf2htmlEX技术的PDF预览页面提取原始PDF文件的方法原理与实践
pdf·自动化脚本·pdf预览页面·提取pdf文件·自动化提取pdf脚本·pdf2htmlex·原始pdf文件
小许好楠2 小时前
【itext操作pdf】
pdf
bugcome_com3 小时前
C# 反射(Reflection)超全解析
c#
bjzhang755 小时前
Dorisoy.AMS--一款采用C# WinForm框架+SQLite数据库的企业/机构资产管理解决方案
sqlite·c#·资产管理
零度@6 小时前
Java消息中间件-Kafka全解(2026精简版)
java·kafka·c#·linq
2501_941882487 小时前
在开普敦跨区域部署环境中构建高可靠分布式配置中心的设计思路与实现实践
开发语言·c#
zxy28472253018 小时前
利用C#的BotSharp本地部署第一个大模型AI Agent示例(1)
人工智能·c#·对话·ai agent·botsharp