java 在线预览_java 实现在线预览功能

/****/

packagecom.nd.core.util;importjava.io.File;importjava.util.regex.Pattern;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;importorg.artofsolving.jodconverter.OfficeDocumentConverter;importorg.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;importorg.artofsolving.jodconverter.office.OfficeManager;importorg.springframework.util.StringUtils;/*** 这是一个工具类,主要是为了使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx)

* 转化为pdf文件

* Office2010的没测试

*

* @date 2017-03-03

*@authorjjc

**/

public classOffice2PDF {private static final Log LOG = LogFactory.getLog(Office2PDF.class);/*** 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件

*

*@paraminputFilePath

* 源文件路径,如:"e:/test.docx"

*@return

*/

public staticFile openOfficeToPDF(String inputFilePath) {returnoffice2pdf(inputFilePath);

}/*** 根据操作系统的名称,获取OpenOffice.org 3的安装目录

* 如我的OpenOffice.org 3安装在:C:/Program Files (x86)/OpenOffice.org 3

*

*@returnOpenOffice.org 3的安装目录*/

public staticString getOfficeHome() {

String osName= System.getProperty("os.name");

System.out.println("操作系统名称:" +osName);if (Pattern.matches("Linux.*", osName)) {return "/opt/openoffice.org3";

}else if (Pattern.matches("Windows.*", osName)) {return "C:/Program Files/OpenOffice 4";

}else if (Pattern.matches("Mac.*", osName)) {return "/Applications/OpenOffice.org.app/Contents/";

}return null;

}/*** 连接OpenOffice.org 并且启动OpenOffice.org

*

*@return

*/

public staticOfficeManager getOfficeManager() {

DefaultOfficeManagerConfiguration config= newDefaultOfficeManagerConfiguration();//设置OpenOffice.org 3的安装目录

config.setOfficeHome(getOfficeHome());//启动OpenOffice的服务

OfficeManager officeManager =config.buildOfficeManager();

officeManager.start();returnofficeManager;

}/*** 转换文件

*

*@paraminputFile

*@paramoutputFilePath_end

*@paraminputFilePath

*@paramoutputFilePath

*@paramconverter*/

public staticFile converterFile(File inputFile, String outputFilePath_end, String inputFilePath,

OfficeDocumentConverter converter) {

File outputFile= newFile(outputFilePath_end);//假如目标路径不存在,则新建该路径

if (!outputFile.getParentFile().exists()) {

outputFile.getParentFile().mkdirs();

}

converter.convert(inputFile, outputFile);

System.out.println("文件:" + inputFilePath + "\n转换为\n目标文件:" + outputFile + "\n成功!");returnoutputFile;

}/*** 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件

*

*@paraminputFilePath

* 源文件路径,如:"e:/test.docx"

*@paramoutputFilePath

* 目标文件路径,如:"e:/test_docx.pdf"

*@return

*/

public staticFile office2pdf(String inputFilePath) {

OfficeManager officeManager= null;try{if(StringUtils.isEmpty(inputFilePath)) {

LOG.info("输入文件地址为空,转换终止!");return null;

}

File inputFile= newFile(inputFilePath);//转换后的文件路径

String outputFilePath_end =getOutputFilePath(inputFilePath);if (!inputFile.exists()) {

LOG.info("输入文件不存在,转换终止!");return null;

}//获取OpenOffice的安装路劲

officeManager =getOfficeManager();//连接OpenOffice

OfficeDocumentConverter converter = newOfficeDocumentConverter(officeManager);returnconverterFile(inputFile, outputFilePath_end, inputFilePath, converter);

}catch(Exception e) {

LOG.error("转化出错!", e);

}finally{//停止openOffice

if (officeManager != null) {

officeManager.stop();

}

}return null;

}/*** 获取输出文件

*

*@paraminputFilePath

*@return

*/

public staticString getOutputFilePath(String inputFilePath) {

String outputFilePath= inputFilePath.replaceAll("." + getPostfix(inputFilePath), ".pdf");returnoutputFilePath;

}/*** 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx"

*

*@paraminputFilePath

*@return

*/

public staticString getPostfix(String inputFilePath) {return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1);

}public static voidmain(String[] args) {

Office2PDF.openOfficeToPDF("/Users/JJC/Downloads/20170302汽修服务测试反馈.docx");

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值