欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

通过openOffice将office文件转成pdf

时间:2021-09-18 08:13:25|栏目:JAVA代码|点击:

下载安装openoffice,下载地址:http://www.openoffice.org/download/我安装的目录:

输入cmd回车


在命令窗口输入

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" ?Cnofirststartwizard

此时服务就开启了将以下代码放到工具类里面,直接调用即可

import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
...
...
  public File office2Pdf(String srcPath, String pdfPath) throws Exception {
    // 源文件目录
    File inputFile = new File(srcPath);
    if (!inputFile.exists()) {
      throw new Exception("程序出现问题,文件不存在");
    }
    // 输出文件目录
    File outputFile = new File(pdfPath);
    if (!outputFile.exists()) {
      outputFile.createNewFile();
    }
    // 调用openoffice服务线程
    String command = openOfficeCommand;
    Process process = Runtime.getRuntime().exec(command);

    // 连接openoffice服务
    OpenOfficeConnection connection = new SocketOpenOfficeConnection(openOfficeUrl, openOfficePort);
    connection.connect();

    // 转换word到pdf
    DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
    converter.convert(inputFile, outputFile);
    // 关闭连接
    connection.disconnect();
    // 关闭进程
    process.destroy();
    return outputFile;
  }
...

上一篇:以实例讲解Objective-C中的KVO与KVC机制

栏    目:JAVA代码

下一篇:Java如何基于反射机制获取不同的类

本文标题:通过openOffice将office文件转成pdf

本文地址:http://www.codeinn.net/misctech/175832.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有