jodconverter openoffice php,openOffice 转换文件格式

package core;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Date;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import org.artofsolving.jodconverter.OfficeDocumentConverter;

import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;

import org.artofsolving.jodconverter.office.OfficeManager;

//转换文档为pdf

public class OpenOfficePdfConvert {

/**

* @param args

*/

private static OfficeManager officeManager;

private static String OFFICE_HOME = "d:\\Program Files\\OpenOffice.org 3";// 安装OPenOffice

// 的路径

private static int port[] = { 8100 };

//1、客户上传Word文档到服务器

//2、服务器调用OpenOffice程序打开上传的Word文档

//3、OpenOffice将Word文档另存为Html格式

public File convertToHtml(String inputFile, String outputFile)

throws FileNotFoundException {

// 创建保存html的文件

File wantFile = new File(outputFile + File.separator + new Date().getTime()

+ ".html");

// 开启服务器

startService();

// 进行转换

System.out.println("进行文档转换转换:" + inputFile + " --> " + outputFile);

OfficeDocumentConverter converter = new OfficeDocumentConverter(

officeManager);

converter.convert(new File(inputFile), wantFile);

// 关闭服务器

stopService();

System.out.println();

return wantFile;

}

// 打开服务器

public static Boolean startService() {

DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();

try {

System.out.println("准备启动服务....");

configuration.setOfficeHome(OFFICE_HOME);// 设置OpenOffice.org安装目录

configuration.setPortNumbers(port); // 设置转换端口,默认为8100

configuration.setTaskExecutionTimeout(1000 * 60 * 5L);// 设置任务执行超时为5分钟

configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);// 设置任务队列超时为24小时

officeManager = configuration.buildOfficeManager();

officeManager.start(); // 启动服务

System.out.println("office转换服务启动成功!");

return true;

} catch (Exception ce) {

System.out.println("office转换服务启动失败!详细信息:" + ce);

return false;

}

}

// 关闭服务器

public static void stopService() {

System.out.println("关闭office转换服务....");

if (officeManager != null) {

officeManager.stop();

}

System.out.println("关闭office转换成功!");

}

/*

* 进行测试转换是否成功

*/

public static void main(String[] args) {

String inputFile = "c:\\test\\test.docx";

String outputFile = "c:\\test";

OpenOfficePdfConvert opc = new OpenOfficePdfConvert();

try {

opc.convertToHtml(inputFile,outputFile);

} catch (FileNotFoundException e1) {

e1.printStackTrace();

}

/*try {

* 如果想看到不带HTML标签的字符串可以调用这个方法进行简化

System.out.println(toHtmlString(inputFile, outputFile));} catch (FileNotFoundException e) {

e.printStackTrace();

}*/

System.out.println("恭喜您,转换成功...");

}

/**

* 将word转换成html文件,并且获取html文件代码。

*

* @param docFile

*            需要转换的文档

* @param filepath

*            文档中图片的保存位置

* @return 转换成功的html代码

* @throws FileNotFoundException

*/

public static String toHtmlString(String docFile, String filepath)

throws FileNotFoundException {

System.out.println("文档中图片的保存位置 ==>" + filepath);

// 转换word文档

OpenOfficePdfConvert opc = new OpenOfficePdfConvert();

File htmlFile = opc.convertToHtml(docFile, filepath);

// 获取html文件流

StringBuffer htmlSb = new StringBuffer();

try {

BufferedReader br = new BufferedReader(new InputStreamReader(

new FileInputStream(htmlFile)));

while (br.ready()) {

htmlSb.append(br.readLine());

}

br.close();

// 删除临时文件

// htmlFile.delete();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

// HTML文件字符串

String htmlStr = htmlSb.toString();

// 返回经过清洁的html文本

return clearFormat(htmlStr, filepath);

}

/**

* 清除一些不需要的html标记

*

* @param htmlStr  带有复杂html标记的html语句

*

* @return 去除了不需要html标记的语句

*/

protected static String clearFormat(String htmlStr, String docImgPath) {

// 获取body内容的正则

String bodyReg = "

";

Pattern bodyPattern = Pattern.compile(bodyReg);

Matcher bodyMatcher = bodyPattern.matcher(htmlStr);

if (bodyMatcher.find()) {

// 获取BODY内容,并转化BODY标签为DIV

htmlStr = bodyMatcher.group().replaceFirst("

.replaceAll("", "

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值