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

相关推荐
小老鼠爱大米22 分钟前
[C#] WPF - 资源URI
c#·wpf·uri
阿蒙Amon9 天前
《C#图解教程 第5版》深度推荐
开发语言·c#
暖馒9 天前
C#委托与事件的区别
开发语言·c#
JosieBook9 天前
【C#】C#异步编程:异步延时 vs 阻塞延时深度对比
c#·多线程·异步·阻塞
工业3D_大熊9 天前
3D模式格式转换工具HOOPS Exchange如何将3D PDF转换为STEP格式?
3d·pdf·3d格式转换·3d模型格式转换·cad格式转换·cad数据格式转换·3d模型可视化
甄天9 天前
WPF中MVVM和MVVMLight模式
c#·wpf·visual studio
冰茶_10 天前
ASP.NET Core API文档与测试实战指南
后端·学习·http·ui·c#·asp.net
_oP_i10 天前
实现 “WebView2 获取word选中内容
开发语言·c#·word
Kookoos10 天前
ABP vNext + Azure Application Insights:APM 监控与性能诊断最佳实践
后端·c#·.net·abp vnext
专注VB编程开发20年10 天前
asp.net core Razor动态语言编程代替asp.net .aspx更高级吗?
开发语言·后端·c#·asp.net·.net