利用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);
}
}
}
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);
}
}
}
相关文章推荐
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- ppt文档格式转换成pdf如何实现
- Java利用OpenOffice将word等office文档转换成PDF
- 实现pdf图片格式转换成txt文档的方法
- Java利用OpenOffice将word等office文档转换成PDF
- Java利用OpenOffice将word等office文档转换成PDF
- Java利用OpenOffice将word等office文档转换成PDF
- 利用OpenOffice将word等office文档转换成PDF
- 利用jacob完成文档格式的转换(PDF)
- 利用 jodconverter 和openoffice 结合 把doc、ppt 等一系列文档转换成pdf
- Java利用OpenOffice将word等office文档转换成PDF
- PDF格式文档转换成Excel怎么实现
- winform利用itextsharp.dll实现图片文件转换PDF格式文件
- 利用OpenOffice.org和Swftools进行实现—doc-->pdf-->swf--格式的转换
- C#实现office文档转换为PDF格式
- Java利用OpenOffice将word等office文档转换成PDF(附件在最后)
- Java利用OpenOffice将word等office文档转换成PDF
- ubuntu中实现tif格式转换为pdf或者其它各种格式的方法
- Java利用OpenOffice将word等office文档转换成PDF