转载

首先我们需要准备需要的文件
    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我们下载安装好以后,我们需要开启服务这个开启服务不能直接打开,而是需要通过命令行操作


 
 
  1. package com.hnbits.exam.study.action;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.FileReader;
  8. import java.io.InputStreamReader;
  9. import java.io.PrintStream;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import org.apache.commons.io.FileExistsException;
  13. import org.artofsolving.jodconverter.OfficeDocumentConverter;
  14. import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
  15. import org.artofsolving.jodconverter.office.ExternalOfficeManagerConfiguration;
  16. import org.artofsolving.jodconverter.office.OfficeManager;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
  20. import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
  21. import com.hnbits.examfront.examexercise.action.FrontExerciseAction;
  22. import com.hnbits.util.FileUtil;
  23. import com.hnbits.util.PropertiesUtil;
  24. import com.hnbits.util.StringUtil;
  25. import com.sun.star.io.IOException;
  26. public class WordToPdf {
  27.     public static final String PARAMETER_OFFICE_PORT = PropertiesUtil.getCommonConfig().getProperty( "system.openoffice.port");
  28.     public static final String PARAMETER_OFFICE_HOME = PropertiesUtil.getCommonConfig().getProperty( "system.openoffice.home");
  29.     private final static Logger log = LoggerFactory.getLogger(WordToPdf.class);
  30.     private static OfficeManager officeManager;
  31.     private static OfficeDocumentConverter documentConverter;
  32.         static{
  33.            DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();          
  34.            configuration.setPortNumber(Integer.parseInt(PARAMETER_OFFICE_PORT));
  35.            configuration.setOfficeHome( new File(PARAMETER_OFFICE_HOME));
  36.            String cmd= "tasklist /fi  \"imagename eq soffice.bin\"";
  37.             try{
  38.                officeManager = configuration.buildOfficeManager();
  39.                Process proc=Runtime.getRuntime().exec(cmd);
  40.                BufferedReader  bufferedReader = new BufferedReader( new InputStreamReader(proc.getInputStream()));
  41.                String line = null;
  42.                Boolean is= true;
  43.                 while ((line = bufferedReader.readLine()) != null) {
  44.                     if (line.contains( "soffice.bin")) {
  45.                        is= false;
  46.                    }
  47.                }
  48.             if(is){
  49.                officeManager.start();  
  50.                    log.debug( "office转换服务启动成功!");
  51.            } else {
  52.             ExternalOfficeManagerConfiguration externalProcessOfficeManager = new  
  53.                     ExternalOfficeManagerConfiguration();  
  54.             externalProcessOfficeManager.setConnectOnStart( true);  
  55.             externalProcessOfficeManager.setPortNumber( 8100);  
  56.             officeManager = externalProcessOfficeManager.buildOfficeManager();  
  57.            officeManager.start();  
  58.            log.debug( "office转换服务启动成功!");
  59.            }
  60.            } catch(Exception e){
  61.            log.error( "office转换服务启动失败!"+e.getMessage());
  62.            }
  63.        }
  64.        
  65.         public static void stopService() {
  66.         log.debug( "关闭office转换服务....");
  67.             if (officeManager != null) {
  68.                officeManager.stop();
  69.            }
  70.            log.debug( "关闭office转换成功!");
  71.        }
  72.        
  73.         public static int convert(File inputFile,File outputFile) throws Exception{  
  74.         try{
  75.         log.debug( "文档转换开始:" + inputFile + " --> " + outputFile);
  76.         if (inputFile.exists()) { // 找不到源文件, 则返回  
  77.                 if (!outputFile.exists()) { // 假如目标路径不存在, 则新建该路径  
  78.                     outputFile.createNewFile();  
  79.                 }  
  80.               String path=inputFile.getPath().substring(inputFile.getPath().length()- 25);
  81.               if(path.indexOf( ".pdf")!=- 1){
  82.               FileUtil.CopyFile(inputFile.getPath(), outputFile.getPath(),log);
  83.               return - 1;
  84.             }
  85.             documentConverter = new OfficeDocumentConverter(officeManager);
  86.                 documentConverter.convert(inputFile, outputFile); 
  87.                 return 1;
  88.         }
  89.           }
  90.         catch(Exception e){
  91.         throw e;
  92.         }
  93.         finally{
  94.         log.debug( "文档转换转换---- 结束----");
  95.         }
  96. return 0;
  97.           
  98.        }  
  99.   
  100.    }
   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;                   }         }

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值