JAVA进阶之路-openoffice+pfd.js实现在线预览文档

首先我们需要准备需要的文件
    1、pdf.js的文件,可以直接百度去官网下载,下载下来官网还会自带一个demo。

    2、下载openoffice并安装好.

           1)windos环境下的直接百度下载安装就好了。

           2)linux下载的安装可以参考这个https://blog.csdn.net/u013132051/article/details/53304562

我们先说下实现过程吧.pdf.js是预览pdf文件的,openoffice就是用来将office文件转换成pdf格式。这样一来我们就可以实现pdf预览的功能.

 

     

解压出来的文件夹:

官网这里面的demo是直接可以使用的,也就是说可以直接预览文件

我们可以直接打开viewer.html进行预览.那么他是怎么指定文件预览的呢。答案是在viewer.js这个文件里面,viewer.js里面有一个DEFAULT_URL的变量来指定需要预览的文件,除此之外,我们还可以在网址后面加一个参数,例如file:///C:/Users/Shinelon/Desktop/blog/pdf/pdfjs-1.9.426-dist/web/viewer.html?file=文件路径+文件名。这样也可以实现文件预览。

这样我们就可以直接将此文件放入到项目当中去。利用第二种预览方式也就是在路径后面加上file=文件路径+文件名的方式去预览pdf文件.

pdf.js的用法就是以上内容了。我们再来说说openoffice.openoffice我们下载安装好以后,我们需要开启服务这个开启服务不能直接打开,而是需要通过命令行操作

package com.hnbits.exam.study.action;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;


import org.apache.commons.io.FileExistsException;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.ExternalOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.hnbits.examfront.examexercise.action.FrontExerciseAction;
import com.hnbits.util.FileUtil;
import com.hnbits.util.PropertiesUtil;
import com.hnbits.util.StringUtil;
import com.sun.star.io.IOException;


public class WordToPdf {
   public static final String PARAMETER_OFFICE_PORT = PropertiesUtil.getCommonConfig().getProperty("system.openoffice.port");
   public static final String PARAMETER_OFFICE_HOME = PropertiesUtil.getCommonConfig().getProperty("system.openoffice.home");
   private final static Logger log = LoggerFactory.getLogger(WordToPdf.class);
   private static OfficeManager officeManager;
   private static OfficeDocumentConverter documentConverter;
        static{
           DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();          
           configuration.setPortNumber(Integer.parseInt(PARAMETER_OFFICE_PORT));
           configuration.setOfficeHome(new File(PARAMETER_OFFICE_HOME));
           String cmd="tasklist /fi  \"imagename eq soffice.bin\"";
           try{
               officeManager = configuration.buildOfficeManager();
               Process proc=Runtime.getRuntime().exec(cmd);
               BufferedReader  bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
               String line = null;
               Boolean is=true;
               while ((line = bufferedReader.readLine()) != null) {
                   if (line.contains("soffice.bin")) {
                       is=false;
                   }
               }
        	   if(is){
            	   officeManager.start();  
                   log.debug("office转换服务启动成功!");
        	   }else {
        		    ExternalOfficeManagerConfiguration externalProcessOfficeManager = new  
        		            ExternalOfficeManagerConfiguration();  
        		    externalProcessOfficeManager.setConnectOnStart(true);  
        		    externalProcessOfficeManager.setPortNumber(8100);  
        		    officeManager = externalProcessOfficeManager.buildOfficeManager();  
    		       officeManager.start();  
    		       log.debug("office转换服务启动成功!");
        	   }
           }catch(Exception e){
        	   log.error("office转换服务启动失败!"+e.getMessage());
           }
       }
       
       public static void stopService() {
       	log.debug("关闭office转换服务....");
           if (officeManager != null) {
               officeManager.stop();
           }
           log.debug("关闭office转换成功!");
       }
       
       public static int convert(File inputFile,File outputFile) throws Exception{  
       	try{
       		log.debug("文档转换开始:" + inputFile + " --> " + outputFile);
       		if (inputFile.exists()) {// 找不到源文件, 则返回  
   	            if (!outputFile.exists()) { // 假如目标路径不存在, 则新建该路径  
   	                outputFile.createNewFile();  
   	            }  
              String path=inputFile.getPath().substring(inputFile.getPath().length()-25);
       	      if(path.indexOf(".pdf")!=-1){
       	    	  FileUtil.CopyFile(inputFile.getPath(), outputFile.getPath(),log);
  	            	return -1;
	            }
   	       		documentConverter = new OfficeDocumentConverter(officeManager);
   	            documentConverter.convert(inputFile, outputFile); 
   	            return 1;
       		}
   	      }
       	catch(Exception e){
       		throw e;
       	}
       	finally{
       		log.debug("文档转换转换---- 结束----");
       	}
		return 0;
          
       }  
  
   }
	   if(is){
            	   officeManager.start();  
                   log.debug("office转换服务启动成功!");
        	   }else {
        		    ExternalOfficeManagerConfiguration externalProcessOfficeManager = new  
        		            ExternalOfficeManagerConfiguration();  
        		    externalProcessOfficeManager.setConnectOnStart(true);  
        		    externalProcessOfficeManager.setPortNumber(8100);  
        		    officeManager = externalProcessOfficeManager.buildOfficeManager();  
    		       officeManager.start();  
    		       log.debug("office转换服务启动成功!");
        	   }
           }catch(Exception e){
        	   log.error("office转换服务启动失败!"+e.getMessage());
           }
       }
       
       public static void stopService() {
       	log.debug("关闭office转换服务....");
           if (officeManager != null) {
               officeManager.stop();
           }
           log.debug("关闭office转换成功!");
       }
       
       public static int convert(File inputFile,File outputFile) throws Exception{  
       	try{
       		log.debug("文档转换开始:" + inputFile + " --> " + outputFile);
       		if (inputFile.exists()) {// 找不到源文件, 则返回  
   	            if (!outputFile.exists()) { // 假如目标路径不存在, 则新建该路径  
   	                outputFile.createNewFile();  
   	            }  
              String path=inputFile.getPath().substring(inputFile.getPath().length()-25);
       	      if(path.indexOf(".pdf")!=-1){
       	    	  FileUtil.CopyFile(inputFile.getPath(), outputFile.getPath(),log);
  	            	return -1;
	            }
   	       		documentConverter = new OfficeDocumentConverter(officeManager);
   	            documentConverter.convert(inputFile, outputFile); 
   	            return 1;
       		}
   	      }
       	catch(Exception e){
       		throw e;
       	}
       	finally{
       		log.debug("文档转换转换---- 结束----");
       	}
		return 0;
          
       }  
  
   }

上面就是一个完整的例子。也是经过我爬过很多坑而撸出来的代码!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值