实现类似百度文库的在线预览office文档


做毕业设计的时候设及到类似“百度文库”的功能模块,在网上搜半天这个办法是最全,最易学的。在此站在巨人的肩膀上详细的介绍一下。

主体思路:先把word文档转通过OpenOffic换成pdf,再把pdf文件通过Swftools转换成swf文件,最后用Flexpaper显示swf文件.

第一步:
1.下载工具OpenOffice、jodconvert-2.2.2.zip、Swftools(pdf2swf)、FlexPaper 

2.安装OpenOffice,本人的安装路径为:C:\Program Files\OpenOffice.org 3 

3.安装Swftools(pdf2swf),本人的安装路径为:D:\Program Files\SWFTools
第二步

1.  将jodconvert-2.2.2.zip解压,把lib中的jar文件放到项目WEB-INF/lib中

2.在WebRoot下新建flexpaper文件夹,然后把FlexPaper_1.4.5flash.zip解压后的内容放到该文件夹中。

3.开启OpenOffice服务,在dos环境中

 cd C:\Program Files\OpenOffice.org 3\program

 soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizar

也可以在项目中开启:

                   String   OpenOffice_HOME = "D:/Program Files/OpenOffice.org 3";    // 这里是OpenOffice的安装目录, 


                     // 假如从文件中读取的URL地址最后一个字符不是 '\',则添加'\' 

                     if(OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '/') {  

                     OpenOffice_HOME +="/"; 

                     } 

                     Process pro = null; 

                    

                     // 启动OpenOffice的服务 

                     String command =OpenOffice_HOME + "program/soffice.exe -headless-accept=\"socket,host=127.0.0.1,port=8100;urp;\""; 

                     try {

                            pro =Runtime.getRuntime().exec(command);

                     } catch (IOException e1) {

                            // TODOAuto-generated catch block

                            e1.printStackTrace();

                     }  


4. 利用Jodconverter编写转换类

 

                OpenOfficeConnection  connection=new SocketOpenOfficeConnection(8100);

                try{

                           connection.connect();

                           DocumentConverter converter=new OpenOfficeDocumentConverter(connection);

                           if(sourceFile.exists()){//如果源文件存在

                                  converter.convert(sourceFile, pdfFile);

                                  pdfFile.createNewFile();

                                  connection.disconnect();

                                  System.out.println("转换为PDF格式 路径"+pdfFile.getPath());

                           }

                           

                    }catch(java.net.ConnectException e){

                           e.printStackTrace();

                           System.out.println("OpenOffice服务未启动");

                           return "failed";

                    }catch(com.artofsolving.jodconverter.openoffice.connection.OpenOfficeExceptione)

                    {

                           e.printStackTrace();

                           System.out.println("读取文件失败");

                           return "failed";

                    }catch(Exception e){

                           e.printStackTrace();

                           System.out.println("读取文件失败");

                           return "failed";

                    }finally{

                           if(connection!=null){

                                  connection.disconnect();

                                  connection=null;

                           }

                    }          

                 }

 


5.从Pdf到swf

                       Runtime  r=Runtime.getRuntime();

                       if(pdfFile.exists()){

                              try{

                                     Process p=r.exec("C:\\ProgramFiles\\SWFTools\\pdf2swf.exe \""+pdfFile.getPath()+"\"  -o \""+swfFile.getPath()+"\" -T 9");

                                     System.out.println("pdf2swf.exe开始转换"); 

                                     swfFile.createNewFile();

                                    System.out.println("---pdf转成swf格式路径---"+swfFile.getPath());                     

                              }catch(Exception e){

                                     e.printStackTrace();

                                     return "failed";

                              }    

                       }else{

                              System.out.println("----PDF文件不存在,无法转换---------");

                       }


 

6.一切转换就绪就差展示了,最后跳转到view页面。在工程下面的WebRoot下的flexpaper下,新建readFile.jsp文件(这边位置随意,只要servlet跳转的时候配置好路径就可以了),代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">    
    <head> 
        <title>FlexPaper</title>         
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
        <style type="text/css" media="screen"> 
            html, body    { height:100%; }
            body { margin:0; padding:0; overflow:auto; }   
            #flashContent { display:none; }
        </style> 
        
        <script type="text/javascript" src="<%=basePath%>flexpaper/js/flexpaper_flash.js"></script>
    </head> 
    <body> 
        <div style="position:absolute;left:10px;top:10px;">
        <input type="hidden" id="swf" value="${swfFileName }">
            <a id="viewerPlaceHolder" style="width:1000px;height:500px;display:block"></a>
            <script type="text/javascript"> 
                var swf = document.getElementById("swf").value;
                alert(swf);
                swf='<%=basePath%>courseware/swf/'+swf;
                var fp = new FlexPaperViewer(    
                         '<%=basePath%>flexpaper/FlexPaperViewer',
                         'viewerPlaceHolder', { config : {
                         SwfFile : escape(swf),
                         Scale : 0.6, 
                         ZoomTransition : 'easeOut',
                         ZoomTime : 0.5,
                         ZoomInterval : 0.2,
                         FitPageOnLoad : true,
                         FitWidthOnLoad : false,
                         PrintEnabled : true,
                         FullScreenAsMaxWindow : false,
                         ProgressiveLoading : false,
                         MinZoomSize : 0.2,
                         MaxZoomSize : 5,
                         SearchMatchAll : false,
                         InitViewMode : 'Portrait',
                         
                         ViewModeToolsVisible : true,
                         ZoomToolsVisible : true,
                         NavToolsVisible : true,
                         CursorToolsVisible : true,
                         SearchToolsVisible : true,
                          
                           localeChain: 'en_US'
                         }});
            </script>
  </div>
  </body>
 </html>





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值