openoffice 安装 以及结合jodconverter
由于最近项目用到了 openoffice 进行文件类型的转换。
进行了初步的学习。
软件要求
1.openoffice 目前最新版本是OpenOffice.org 3.4.1。 3.3以下的都含有jar的下载。
3.4就不含有了。官方网址:http://www.openoffice.org/download/other.html#tested-sdk
2.用于调用openoffice的第三方插件 jodconverter 目前最新版本
jodconverter-core-3.0-beta-4-dist
官网地址:http://code.google.com/p/jodconverter/
使用步骤:
openoffice 双击安装后。
在命令行输入如下命令:
cd C:\Program Files\OpenOffice.org 3\program
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"
-nofirststartwizard
port 可以更改避免更其他端口冲突。命令输入后 就启动了openoffice服务。
下面是封装的 fileutil
import java.io.File;import java.io.IOException;import org.apache.tomcat.util.http.fileupload.FileUtils;import org.artofsolving.jodconverter.OfficeDocumentConverter;import org.artofsolving.jodconverter.office.ExternalOfficeManagerConfiguration;import org.artofsolving.jodconverter.office.OfficeManager;import org.icepdf.core.exceptions.PDFException;import org.icepdf.core.exceptions.PDFSecurityException;public class FileUtil {private static OfficeManager officeManager;public static synchronized void initOpenOfficeManager(){if(FileUtil.officeManager!=null)return;FileUtil.officeManager = new ExternalOfficeManagerConfiguration().buildOfficeManager();FileUtil.officeManager.start();}public static void destriyOpenOfficeManager(){FileUtil.officeManager.stop();}private static OfficeManager getOfficeManager(){if(FileUtil.officeManager==null)FileUtil.initOpenOfficeManager();return FileUtil.officeManager;}//根据文件路径清空其路径所在的文件夹中的所有文件public static void cleanDirectory(String url) throws IOException{File file = new File(url);if(!file.isDirectory()){file = new File(url.substring(0, url.lastIndexOf("/")));}if(file.exists()&&file.isDirectory())FileUtils.cleanDirectory(file);}/** * office 文件转 PDF * @param input 源文件路径 * @param output 目标文件路径 * @return 是否生成成功 */public static void office2PDF(String input,String output){File inputFile = new File(input); File outputFile = new File(output); //OfficeManager officeManager = new ExternalOfficeManagerConfiguration().buildOfficeManager(); //officeManager.start(); OfficeDocumentConverter converter = new OfficeDocumentConverter(FileUtil.getOfficeManager()); converter.convert(inputFile,outputFile); //officeManager.stop(); }public static DocumentState getDocumentState(String filePath) throws PDFException, IOException, PDFSecurityException{return FileUtil.getDocumentState(filePath,1.0f,0f,1);}public static DocumentState getDocumentState(String filePath,float zoom,float rotation,int pageCursor) throws PDFException, IOException, PDFSecurityException{DocumentState state = new DocumentState(filePath,zoom,rotation,pageCursor);state.openDocument();return state;}}