文档在线阅读的实现(类百度文库)

2013年01月10日 Java, 技术分享 暂无评论 阅读 7,627 views 次

Office文档的在线阅读,现在的一般实现思路如下:

1、使用openoffice将office文档转换成PDF

2、使用swftools将PDF转换成swf

3、使用flexpaper播放swf文件

最终的效果如下图:

flexpager效果具体实现如下:

office文档转PDF

需要的软件及类库

  • 需要软件openoffice 3.4.1,可以从openoffice官网下载。
  • jodconverter类库,3.0beta4。

mvenv的信息如下:其中openoffice.version=3.2.1(3.4.1的类库在maven仓库上没有,所以使用这个版本的)

  1. <dependency>  
  2.     <groupId>com.artofsolving</groupId>  
  3.     <artifactId>jodconverter-core</artifactId>  
  4.     <version>3.0-beta-4</version>  
  5. </dependency>  
  6. <dependency>  
  7.           <groupId>org.openoffice</groupId>  
  8.           <artifactId>ridl</artifactId>  
  9.           <version>${openoffice.version}</version>  
  10.       </dependency>  
  11.       <dependency>  
  12.           <groupId>org.openoffice</groupId>  
  13.           <artifactId>juh</artifactId>  
  14.           <version>${openoffice.version}</version>  
  15.       </dependency>  
  16.       <dependency>  
  17.           <groupId>org.openoffice</groupId>  
  18.           <artifactId>jurt</artifactId>  
  19.           <version>${openoffice.version}</version>  
  20.       </dependency>  
  21.       <dependency>  
  22.           <groupId>org.openoffice</groupId>  
  23.           <artifactId>unoil</artifactId>  
  24.           <version>${openoffice.version}</version>  
  25.       </dependency>       

第一步:安装openoffice到操作系统里面

第二步:复制相关的jar包到系统的依赖库里面。具体的jar包见上面的代码。

第三步:编写转换代码,核心代码如下:

  1. /**  
  2.      * 将office文档转换成PDF文档  
  3.      * @Date 2012-12-11上午10:01:59   
  4.      * @Author huqiwen  
  5.      * @param inputFile 文件在磁盘上的物理路径  
  6.      * @param pdfFile 目标文件在磁盘上的物理路径  
  7.      * @return  
  8.      */  
  9.     public static File convert2PDF(String inputFile, String pdfFile) {   
  10.         if (StringUtils.isEmpty(inputFile)||StringUtils.isEmpty(pdfFile)) {   
  11.             return null;   
  12.         }   
  13.         startService();   
  14.         logger.info("进行文档转换转换:" + inputFile + " --> " + pdfFile);      
  15.         OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);   
  16.         File targerpdfFile = new File(pdfFile);   
  17.         converter.convert(new File(inputFile), targerpdfFile);   
  18.         stopService();   
  19.         return targerpdfFile;   
  20.     }   
  21.   
  22.     public static void startService() {   
  23.         DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();   
  24.         try {   
  25.             configuration.setOfficeHome(openoffice_home);// 设置OpenOffice.org安装目录   
  26.             configuration.setTaskExecutionTimeout(1000 * 60 * 5L);// 设置任务执行超时为5分钟   
  27.             configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);// 设置任务队列超时为24小时   
  28.   
  29.             officeManager = configuration.buildOfficeManager();   
  30.             officeManager.start(); // 启动服务   
  31.             logger.info("office转换服务启动成功……");   
  32.         } catch (Exception ce) {   
  33.             logger.info("office转换服务启动失败!详细信息");   
  34.         }   
  35.     }   
  36.   
  37.     public static void stopService() {   
  38.         logger.info("关闭office转换服务....");   
  39.         if (officeManager != null) {   
  40.             officeManager.stop();   
  41.         }   
  42.         logger.info("关闭office转换成功!");   
  43.     }  

PDF转SWF

上面的过程是完成了office文档到PDF的转换,要想在flexpaper里面播放,还需要将PDF转换成SWF格式的,SWFTOOS是一个工具可以将pdf、图片等格式的内容转换成SWF格式,这里我们主要使用的PDF转换功能。

swftools的官方网站:http://www.swftools.org/

核心内代码如下:

  1.    /**  
  2.     * 把PDF文件转换为SWF的格式,虽然SWFTOOS支持多种格式,但这里主要转换PDF文件  
  3.     * @Date 2012-12-11上午11:07:14   
  4.     * @Author huqiwen  
  5.     * @param sourceFilePath  
  6.     * @param swfFilePath  
  7.     * @param fileType  
  8.     * @return  
  9.     * @throws IOException  
  10.     */  
  11. public static boolean convertPDFToSwf(String sourceFilePath,String swfFilePath,String fileType){   
  12.     if(!swfToolExist()){   
  13.         logger.warn("未指定要进行swf转化工具的地址!!!");   
  14.         return false;   
  15.     }   
  16.   
  17.        //判读要转换的文件类型是否符合转换为pdf   
  18.     if(!Constants.PDF_FILE.equalsIgnoreCase(fileType)){   
  19.         logger.warn("当前文件不符合要转化为SWF的文件类型!!!");   
  20.         return false;   
  21.     }   
  22.     File sourceFile=new File(sourceFilePath);   
  23.     if(!sourceFile.exists()){   
  24.         logger.warn("要进行转换文件不存在!!!");   
  25.         return false;   
  26.     }   
  27.   
  28.        //检查flash文件的目录是否存在,不存在则创建   
  29.        File swfFile=new File(swfFilePath);   
  30.        if (!FileUtil.mkdirs(swfFile)) {   
  31.            logger.error("目录创建失败[" + swfFile.getPath() + "]");   
  32.            return false;   
  33.        }   
  34.   
  35.     try {   
  36.         if(!swftoolsPath.endsWith(File.separator)){   
  37.             swftoolsPath+=File.separator;   
  38.         }   
  39.            
  40.         List<String> commandBuidler = new ArrayList<String>();    
  41.         //加载系统的字体目录   
  42.         String systemFontsDir = properties.getProperty("file.fonts.dir""C:\\WINDOWS\\Fonts");   
  43.            
  44.         if (StringUtils.isNotEmpty(systemFontsDir) && Constants.PDF_FILE.equals(fileType)) {   
  45.             commandBuidler.add(swftoolsPath + fileType+"2swf.exe");   
  46.             commandBuidler.add("-F");   
  47.             commandBuidler.add(systemFontsDir);   
  48.             commandBuidler.add("-f");   
  49.             commandBuidler.add(sourceFilePath);   
  50.             commandBuidler.add("-o");   
  51.             commandBuidler.add(swfFilePath);   
  52.             commandBuidler.add("-T 9 ");   
  53.             if (sourceFile.length() > 1024*1024*10) { //大于10M加上以下命令   
  54.                 commandBuidler.add(" -s poly2bitmap ");   
  55.             }   
  56.         } else {   
  57.             logger.warn("只提供PDF文档的格式转换");   
  58.             return false;   
  59.         }   
  60.         ProcessBuilder processBuilder = new  ProcessBuilder();       
  61.         processBuilder.command(commandBuidler);       
  62.         Process process = processBuilder.start();          
  63.            
  64.         AbsInputStreamWathThread inputWathThread = new  InputStreamWathThread(process);       
  65.         inputWathThread.start();       
  66.         AbsInputStreamWathThread errorInputWathThread = new  ErrorInputStreamWathThread(process);       
  67.         errorInputWathThread.start();       
  68.                
  69.         process.waitFor();// 等待子进程的结束,子进程就是系统调用文件转换这个新进程   
  70.         inputWathThread.setOver(true); // 转换完,停止流的处理   
  71.         errorInputWathThread.setOver(true);   
  72.     } catch (IOException e) {   
  73.         e.printStackTrace();   
  74.     } catch (InterruptedException e) {   
  75.         e.printStackTrace();   
  76.     }   
  77.        
  78.     return true;   
  79. }  

使用flexpaper播放

flexpaper的官方网站:http://flexpaper.devaldi.com/

从网站上下载免费版本即可,http://flexpaper.devaldi.com/download/

1、在页面中加载flexpaper.js和flexpaper_handlers.js两个JS文件

2、在页面中要显示flexpaper的地方使用以下JS:

下面的jsDirectory的目录虽然叫js目录,但这个目录的目的主要是定位FlexPaperViewer.swf的位置的,比如现在jsDirectory : "${basepath}/resources/js/",   ,则将  FlexPaperViewer.swf放在${basepath}/resources/下面

  1. $('#nodeId').FlexPaperViewer(   
  2.                 { config:{   
  3.                     SWFFile : swfurl,   
  4.                     jsDirectory : "${basepath}/resources/js/",   
  5.                     Scale : 0.9,   
  6.                     ZoomTransition : 'easeOut',   
  7.                     ZoomTime : 0.5,   
  8.                     FitPageOnLoad : true//加载后适合高度   
  9.                     FitWidthOnLoad : true,//加载后适合宽度   
  10.                     FullScreenAsMaxWindow : false//是否支持全屏   
  11.                     ProgressiveLoading : false,  //是否支持延迟加载   
  12.        
  13.                     ViewModeToolsVisible : true,   
  14.                     ZoomToolsVisible : true,   
  15.                     NavToolsVisible : true,   
  16.                     CursorToolsVisible : true,   
  17.                     SearchToolsVisible : true,   
  18.                     localeChain: 'zh_CN'   
  19.                 }}   
  20.         );  
用户头像

给我留言

您必须 登录 才能发表留言!

Copyright © IT人生录 保留所有权利.   主题设计 知更鸟 滇ICP备16001547号

用户登录

分享到: