實作類別似百度文庫的線上預覽office文檔

來源:互聯網
上載者:User


做畢業設計的時候設及到類似“百度文庫”的功能模組,在網上搜半天這個辦法是最全,最易學的。在此站在巨人的肩膀上詳細的介紹一下。

主體思路:先把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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.