您的位置:首页 > 运维架构

利用openOffice实现各种文档转换成pdf格式

2010-11-23 11:12 537 查看
我曾试过很多PDF格式的转换,包括itext技术、jacob、jcom等,但是转换的效果都不是很好,而且又比较复杂。现在我介绍的这个方法是利用OpenOffice提供的各种Jar包来实现的,感觉应该我目前我发现的最好的,如果各位有更好的办法,大家互相交流一下。

1、从官网上下载OpenOffice安装程序

http://download.openoffice.org/

2、安装好OpenOffice之后,在安装的根目录的URE/java下面找到几个Jar包:

juh.jar

jurt.jar

ridl.jar

unoloader.jar

把上面的几个Jar包加到classpath下面,我安装的是OpenOffice3.2版本,默认的安装路径是:C:/Program Files/OpenOffice.org 3

3、编写JAVA代码,这是我自己测试成功的,给大家分享出来

package com.moxm.test.convertPDF.test;

import com.sun.star.beans.PropertyValue;

import com.sun.star.comp.helper.BootstrapException;

import com.sun.star.frame.XComponentLoader;

import com.sun.star.frame.XStorable;

import com.sun.star.lang.XComponent;

import com.sun.star.lang.XMultiComponentFactory;

import com.sun.star.uno.UnoRuntime;

import com.sun.star.uno.XComponentContext;

import com.sun.star.util.XCloseable;

import ooo.connector.BootstrapSocketConnector;

public class Word2PdfTest2 {

private static XComponentContext createContext() throws BootstrapException {

// get the remote office component context

//return Bootstrap.bootstrap();

String oooExecFolder = "C:/Program Files/OpenOffice.org 3/program/";//必须指向根目录下面的program目录

return BootstrapSocketConnector.bootstrap(oooExecFolder);

}

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 (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktop);

}

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

String inputUrl = "file:///" + inputFilePath.replaceAll("
", "%20").replaceAll("////", "/");

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[3];

// 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;

propertyValues[2] = new PropertyValue();

propertyValues[2].Name = "InitialView";

propertyValues[2].Value = new Long(1);

// Composing the URL by replacing all backslashs

String outputUrl = "file:///" + outputFilePath.replace("
", "%20").replaceAll("////", "/");

// 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.storeToURL(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 = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, doc);

if (closeable != null) {

closeable.close(false);

} else {

XComponent component = (XComponent) UnoRuntime.queryInterface(XComponent.class, doc);

component.dispose();

}

}

public static void main(String[] args) {

String inputFilePath = "e://moxm//test.doc";

String outputFilePath = "e://moxm//test.pdf";

// the given type to convert to

String 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 (BootstrapException e) {

e.printStackTrace(System.err);

System.exit(1);

} catch (Exception e) {

e.printStackTrace(System.err);

System.exit(1);

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: