java项目通过openoffice将word等office文档转换成pdf

记录

记录java项目通过openoffice将word等office文档转换成pdf

maven依赖

 <dependency>
     <groupId>com.artofsolving</groupId>
      <artifactId>jodconverter</artifactId>
      <version>2.2.2</version>
 </dependency>

保存文件工具类

/**
     * 保存文件
     *
     * @param inputStream
     * @param path 需要保存的路径
     * @param fileName 保存的文件名
     */
    public static void savePicWord(InputStream inputStream, String path, String fileName) {
        OutputStream os = null;
        try {
            // 2、保存到临时文件
            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流保存到本地文件
            File tempFile = new File(path);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }
            os = new FileOutputStream(path + File.separator + fileName);
            // 开始读取
            while ((len = inputStream.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 完毕,关闭所有链接
            try {
                os.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

转化工具类工具类

public class DocConverter {

    private static final int environment = 1;// 环境1:windows,2:linux(涉及pdf2swf路径问题)
    private String srcFilePath;
    private String outputPath = "";// 输入路径,如果不设置就输出在默认位置
    private String srcFileName;
    private File pdfFile;
    private File docFile;

    public DocConverter(String srcFilePath) {
        ini(srcFilePath);
    }

    /*
     * 重新设置 file @param srcFilePath 
     */
    public void setFile(String srcFilePath) {
        ini(srcFilePath);
    }

    /*
     * 初始化 @param srcFilePath 源文件路径
     */
    private void ini(String srcFilePath) {
        try {
            this.srcFilePath= srcFilePath;
            srcFileName= this.srcFilePath.substring(0, this.srcFilePath.lastIndexOf("."));
            docFile = new File(this.srcFilePath);
            pdfFile = new File(srcFileName+ ".pdf");//创建空文件
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 转为pdf格式
     *
     * @throws Exception
     */
    private void doc2pdf() throws Exception {
        if (docFile.exists()) {
           /* if (!pdfFile.exists()) {*/
            OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
            try {
                connection.connect();
                DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
                converter.convert(docFile, pdfFile);
                // close the connection
                connection.disconnect();
                System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath() + "****");
            } catch (java.net.ConnectException e) {
                // ToDo Auto-generated catch block
                e.printStackTrace();
                System.out.println("****swf转换异常,openoffice服务未启动!****");
                throw e;
            } catch (OpenOfficeException e) {
                e.printStackTrace();
                System.out.println("****pdf转换器异常,读取转换文件失败****");
                throw e;
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
           /* } else {
                System.out.println("****已经转换为pdf,不需要再进行转化****");
            }*/
        } else {
            System.out.println("****pdf转换器异常,需要转换的文档不存在,无法转换****");
        }
    }

    /**
     * 把InputStream字节流 替换为BufferedReader字符流
     *
     * @param in
     * @return
     * @throws IOException
     */
    static String loadStream(InputStream in) throws IOException {
        int ptr = 0;
        //把InputStream字节流 替换为BufferedReader字符流
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder buffer = new StringBuilder();
        while ((ptr = reader.read()) != -1) {
            buffer.append((char) ptr);
        }
        return buffer.toString();
    }

    /**
     * 调用该方法执行pdf的转化
     *
     * @return
     */
    public boolean conver() {
        try {
            if (environment == 1) {
                System.out.println("****swf转换器开始工作,当前设置运行环境windows****");
            } else {
                System.out.println("****swf转换器开始工作,当前设置运行环境linux****");
            }
            doc2pdf();//将文件转换为pdf
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

	/**
	 *调试
	 */
	public static void main(String[] args) {
		  DocConverter d = new DocConverter("D:/text.doc");
          d.conver();//调用方法,分别创建两个dpf和swf
	}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值