利用OpenOffice.org工具实现预览效果

 

 第一:在官网下载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包

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值