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

相关推荐
小码编匠9 小时前
WPF 中的高级交互通过右键拖动实现图像灵活缩放
后端·c#·.net
唐青枫16 小时前
C#.NET 定时任务与队列利器:Hangfire 完整教程
c#·.net
hez20101 天前
Runtime Async - 步入高性能异步时代
c#·.net·.net core·clr
mudtools1 天前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫2 天前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools2 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
玩泥巴的2 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
word·二次开发·office·com互操作
大飞pkz3 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
唐青枫3 天前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net
未来之窗软件服务3 天前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟