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

相关推荐
WYH28710 小时前
C#控制台输入(Read()、ReadKey()和ReadLine())
开发语言·c#
hqwest11 小时前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
做一位快乐的码农14 小时前
基于.net、C#、asp.net、vs的保护大自然网站的设计与实现
c#·asp.net·.net
DavieLau15 小时前
C#项目WCF接口暴露调用及SOAP接口请求测试(Python版)
xml·服务器·开发语言·python·c#
张人玉15 小时前
C#Encoding
开发语言·c#
hqwest17 小时前
C#WPF实战出真汁05--左侧导航
开发语言·c#·wpf·主界面·窗体设计·视图viewmodel
小码编匠19 小时前
C# Bitmap 类在工控实时图像处理中的高效应用与避坑
后端·c#·.net
小周同学:19 小时前
在 Vue2 中使用 pdf.js + pdf-lib 实现 PDF 预览、手写签名、文字批注与高保真导出
开发语言·前端·javascript·vue.js·pdf
Kyln.Wu1 天前
【python实用小脚本-187】Python一键批量改PDF文字:拖进来秒出新文件——再也不用Acrobat来回导
python·pdf·c#
hqwest1 天前
C#WPF实战出真汁01--项目介绍
开发语言·c#·wpf