Java office 转 PDF

本文介绍了一个名为PDFUtils的SpringBoot工具类,利用ApacheCommonsExec库实现对Office文档的隐式转换为PDF,采用线程同步确保并发安全。示例代码展示了如何将.DOC文件转换为.DPDF并处理转换结果。
摘要由CSDN通过智能技术生成
package com.springboot.springboottopdf.util;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.ExecuteResultHandler;

import java.util.concurrent.Semaphore;

public class PDFUtils {

    public static void convert(String officePath,String outPath) throws Exception {

        DefaultExecutor exec = new DefaultExecutor();

        // 同步等待
        Semaphore semaphore = new Semaphore(1);
        semaphore.acquire();
        ExecuteResultHandler erh = new ExecuteResultHandler() {
            @Override
            public void onProcessComplete(int i) {
                semaphore.release();
                //转换完成逻辑
            }

            @Override
            public void onProcessFailed(ExecuteException e) {
                semaphore.release();
                //转换失败逻辑
                e.printStackTrace();
            }
        };
        String command = "cmd.exe /c soffice --invisible --convert-to pdf --outdir \""+outPath+"\" \""+officePath+"\"";
        System.out.println("执行office文件转换任务,命令为" + command);
        exec.execute(CommandLine.parse(command), erh);
        // 等待执行完成
        semaphore.acquire();
    }

    public static void main(String[] args) throws Exception {
        convert("D:/鸿蒙笔记.doc","D:/");
    }
}

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浮生若梦01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值