使用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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值