第一:在官网下载OpenOffice.org这个工具
第二:编码,本人附上实际开发中用到的编码
创建连接 也就是和工具创建连接
public static XComponentContext createContext() throws Exception,
BootstrapException {
String oooExeFolder = "C:/Program Files/OpenOffice.org 3/program/";
return BootstrapSocketConnector.bootstrap(oooExeFolder);
}
//加载工具
public static XComponentLoader createLoader(XComponentContext context)
throws Exception {
XMultiComponentFactory mcf = context.getServiceManager();
Object desktop = mcf.createInstanceWithContext("com.sun.star.frame.Desktop", context);
return (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktop);
}
//加载文件
public static Object loadDocument(XComponentLoader loader,
String inputFilePath) throws Exception {
PropertyValue[] propertyValues = new PropertyValue[1];
propertyValues[0] = new PropertyValue();
propertyValues[0].Name = "Hidden";
propertyValues[0].Value = new Boolean(true);
File inputFile = new File(inputFilePath);
String inputUrl = "file:///"+ inputFile.getAbsolutePath().replace('\\', '/');
System.out.println("inputUrl地址" + inputUrl);
return loader.loadComponentFromURL(inputUrl, "_blank", 0, propertyValues);
}
//设置转换成的目标文件的属性 具体属性可以参考官网api
public static void convertDocument(Object doc, String outputFilePath,
String convertType) throws Exception {
// Preparing properties for converting the document
PropertyValue[] propertyValues = new PropertyValue[2];
// Setting the flag for overwriting
propertyValues[0] = new PropertyValue();
propertyValues[0].Name = "Overwrite";
propertyValues[0].Value = new Boolean(true);
// Setting the filter name
propertyValues[1] = new PropertyValue();
propertyValues[1].Name = "FilterName";
propertyValues[1].Value = convertType;
//Composing the URL by replacing all backslashs
File outputFile = new File(outputFilePath);
String outputUrl = "file:///"+ outputFile.getAbsolutePath().replace('\\', '/');
// Getting an object that will offer a simple way to store
// a document to a URL.
XStorable storable = (XStorable) UnoRuntime.queryInterface(XStorable.class, doc);
// Storing and converting the document
// storable.storeAsURL(outputUrl, propertyValues);
storable.storeToURL(outputUrl, propertyValues);
}
//关闭文件
public static void closeDocument(Object doc) throws Exception {
//Closing the converted document. Use XCloseable.clsoe if the
//interface is supported, otherwise use XComponent.dispose
XCloseable closeable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, doc);
if (closeable != null) {
closeable.close(false);
} else {
XComponent component = (XComponent) UnoRuntime.queryInterface(XComponent.class, doc);
component.dispose();
}
}
关键代码:也就是实际调用上面几个方法进行文件转换
String convertType = "writer_pdf_Export"; //设置转换成的文件是PDF,也可以转换成html具体可以参考api说明
XComponentContext context = JOD4DocToHTML.createContext();
XComponentLoader compLoader = JOD4DocToHTML.createLoader(context);
Object doc = JOD4DocToHTML.loadDocument(compLoader,"源文件");
JOD4DocToHTML.convertDocument(doc,"输出的目标文件",convertType);
JOD4DocToHTML.closeDocument(doc);
在上面的代码中省略的部分是获取文件流也就是读文件之后进行转换。具体可以动手实践下。
注意bootstrapconnector这个会出现错误。 解决的办法就是重新编译这个jar包。因为jar包的下载的时候版本可能不一样。
若需要jar可以流程。这里貌似不好上传jar包