使用openOffice将office文件转成pdf

    1. 下载安装openoffice,下载地址:http://www.openoffice.org/download/
    2. 我安装的目录:
    3. 输入cmd回车

      在命令窗口输入
      soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" –nofirststartwizard
      此时服务就开启了
    4. 将以下代码放到工具类里面,直接调用即可
       1 import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
       2 import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
       3 import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
       4 ...
       5 ...
       6     public File office2Pdf(String srcPath, String pdfPath) throws Exception {
       7         // 源文件目录
       8         File inputFile = new File(srcPath);
       9         if (!inputFile.exists()) {
      10             throw new Exception("程序出现问题,文件不存在");
      11         }
      12         // 输出文件目录
      13         File outputFile = new File(pdfPath);
      14         if (!outputFile.exists()) {
      15             outputFile.createNewFile();
      16         }
      17         // 调用openoffice服务线程
      18         String command = openOfficeCommand;
      19         Process process = Runtime.getRuntime().exec(command);
      20 
      21         // 连接openoffice服务
      22         OpenOfficeConnection connection = new SocketOpenOfficeConnection(openOfficeUrl, openOfficePort);
      23         connection.connect();
      24 
      25         // 转换word到pdf
      26         DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
      27         converter.convert(inputFile, outputFile);
      28         // 关闭连接
      29         connection.disconnect();
      30         // 关闭进程
      31         process.destroy();
      32         return outputFile;
      33     }
      34 ...

       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值