使用OpenOffice.org文档转换

1.安装了OpenOffice.org主程序以及SDK

引入sdk下的jar:

unoil.jar

ridl.jar

jurt.jar

juh.jar

2.下载bootstrapconnector.jar()

 http://user.services.openoffice.org/en/forum/viewtopic.php?f=44&t=2520

3.代码展示

首先,我们打开并连接一个OOo程序,这需要创建一个XComponentContext对象:

private static XComponentContext createContext() throws Exception {
    // get the remote office component context
    // return Bootstrap.bootstrap();
    String oooExeFolder = "C:/Program Files/OpenOffice.org 3/program/";
    return BootstrapSocketConnector.bootstrap(oooExeFolder);
}

然后创建一个XComponentLoader对象:

private static XComponentLoader createLoader(XComponentContext context) throws Exception {
    // get the remote office service manager
    XMultiComponentFactory mcf = context.getServiceManager();
    Object desktop = mcf.createInstanceWithContext("com.sun.star.frame.Desktop", context);
    return UnoRuntime.queryInterface(XComponentLoader.class, desktop);
}

从Loader对象可以加载一篇文档:

private static Object loadDocument(XComponentLoader loader, String inputFilePath) throws Exception {
    // Preparing properties for loading the document
    PropertyValue[] propertyValues = new PropertyValue[1];
    propertyValues[0] = new PropertyValue();
    propertyValues[0].Name = "Hidden";
    propertyValues[0].Value = new Boolean(true);
    
    // Composing the URL by replacing all backslashs
    File inputFile = new File(inputFilePath);
    String inputUrl = "file:///" + inputFile.getAbsolutePath().replace('\\', '/');

    return loader.loadComponentFromURL(inputUrl, "_blank", 0, propertyValues);
}

接着自然就是文档转换了:

private 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 = UnoRuntime.queryInterface(XStorable.class, doc);
    // Storing and converting the document
    storable.storeAsURL(outputUrl, propertyValues);
}

最后还要关闭文档:

private 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 = UnoRuntime.queryInterface(XCloseable.class, doc);
    
    if (closeable != null) {
    	closeable.close(false);
    } else {
        XComponent component = UnoRuntime.queryInterface(XComponent.class, doc);
        component.dispose();
    }
}

最后便是将上面四个步骤串联起来:

public static void main(String args[]) {
    String inputFilePath = "D:\\convert\\input.txt";
    String outputFilePath = "D:\\convert\\output.doc";
    
    // the given type to convert to
    String convertType = "swriter: MS Word 97";
    //pdf:convertType = "writer_pdf_Export"    
    
    try {
        XComponentContext context = createContext();
        System.out.println("connected to a running office ...");
        
        XComponentLoader compLoader = createLoader(context);
        System.out.println("loader created ...");
        
        Object doc = loadDocument(compLoader, inputFilePath);
        System.out.println("document loaded ...");
        
        convertDocument(doc, outputFilePath, convertType);
        System.out.println("document converted ...");
        
        closeDocument(doc);
        System.out.println("document closed ...");
        
        System.exit(0);
    } catch (Exception e) {
        e.printStackTrace(System.err);
        System.exit(1);            
    }
}
原文出处:http://blog.zhaojie.me/2010/05/convert-document-to-pdf-via-openoffice.html 
参考文档:http://wiki.services.openoffice.org/wiki/Main_Page

你可能感兴趣的:(PHP,F#,Blog,Office,sun)