仿百度文庫方案openoffice.org 3+swftools+flexpaper

來源:互聯網
上載者:User

標籤:download   java   文檔   

最近在給客戶開發XX系統時,客戶要求上傳到管理系統的文檔(包括*.doc;*.docx;*.xls;*.xlsx;*.ppt;*.pptx;*.pdf;)只能預覽不允許下載不允許列印。

就想打到了百度文庫和豆丁網,百度文庫和豆丁網的線上預覽都是利用flash來播放文檔的,在網上查閱了大量資料,終於實現了該項功能,

現將自己的設計和實現整理如下,可下載。

 

如何將文檔轉成flash支援的swf檔案實現線上播放?
1.先用openOffice把*.doc;*.docx;*.xls;*.xlsx;*.ppt;*.pptx;*.pdf類型的文檔轉換成pdf
2.用swftools將pdf轉換成swf,然後利用FlexPaper外掛程式實現線上播放預覽。


openoffice

 OpenOffice.org 是一套跨平台的辦公室軟體套件,能在 Windows、Linux、MacOS X (X11)和 Solaris 等作業系統上執行。它與各個主要的辦公室軟體套件相容。OpenOffice.org 是自由軟體,任何人都可以免費下載、使用及推廣它。

主要模組有writer(文字文件),impress(簡報),Calc(電子錶格),Draw(繪圖),Math(公式),base(資料庫)

最新中文正式版:Apache_OpenOffice_incubating_3.4.1_Win_x86_install_zh-CN。

 

  FlexPaper

FlexPaper是一個開源輕量級的在瀏覽器上顯示各種文檔的組件,被設計用來與PDF2SWF一起使用, 使在Flex中顯示PDF成為可能,而這個過程並無需PDF軟體環境的支援。它可以被當做Flex的庫來使用。另外你也可以通過將一些例如Word、PPT等文檔轉成PDF,然後實現線上瀏覽。
1.安裝必備工具組件
(1)安裝openoffice,openoffice是開源免費的文文書處理軟體,它可以將office文檔轉成pdf檔案(安裝到C:\Program Files (x86)\OpenOffice 4\program),openOfficehttp://www.openoffice.org/download/index.html
(2)安裝完openoffice後必須啟動其server,以命令列方式啟動openoffice server。進入cmd命令列提示符C:\Program Files (x86)\OpenOffice 4\program\
輸入如下命令:
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

 

每次都啟動這個命令可以寫個bat檔案

 
進入windows工作管理員查看有個進程soffice.bin,說明openoffice啟動成功!


(3)安裝swfTools(安裝到C:\Program Files)swftools作用是將pdf轉換為swf檔案以便flexpaper播放。:http://www.swftools.org/download.html
(4)下載flexpaper,:http://flexpaper.devaldi.com/download/
(5)下載OpenDocument文檔轉換器 JODConverter,JODConverter是一個java的OpenDucument檔案轉換器,可以進行許多檔案格式的轉換,它利用OpenOffice來進行轉換工作,它能進行以下的轉換工作:
a.Microsoft Office格式轉換為OpenDucument,以及OpenDucument轉換為Microsoft Office
b.OpenDucument轉換為PDF,Word、Excel、PowerPoint轉換為PDF,RTF轉換為PDF等。
:http://sourceforge.net/projects/jodconverter/files/

 

 

範例程式碼:

 1 將上面解壓的flexpaper檔案中的js檔案夾(包含了flexpaper_flash_debug.js,flexpaper_flash.js,jquery.js,這三個js檔案主要是預覽swf檔案的外掛程式)拷貝至網站根目錄;將FlexPaperViewer.swf拷貝至網站根目錄下(該檔案主要是用在網頁中播放swf檔案的播放器),目錄結構如

 

2 建立onlineRead.jsp檔案

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
    <head>
        <title></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="js/swfobject/swfobject.js"></script>
  <script type="text/javascript" src="js/flexpaper_flash.js"></script>
  <script type="text/javascript">
            <!-- For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection. -->
            var swfVersionStr = "10.0.0";
            <!-- To use express install, set to playerProductInstall.swf, otherwise the empty string. -->
            var xiSwfUrlStr = "playerProductInstall.swf";
           
            var flashvars = {
                  SwfFile : escape("Paper.swf"),
      Scale : 0.6,
      ZoomTransition : "easeOut",
      ZoomTime : 0.5,
        ZoomInterval : 0.1,
        FitPageOnLoad : false,
        FitWidthOnLoad : true,
        PrintEnabled : true,
        FullScreenAsMaxWindow : false,
        ProgressiveLoading : true,
       
        PrintToolsVisible : true,
        ViewModeToolsVisible : true,
        ZoomToolsVisible : true,
        FullScreenVisible : true,
        NavToolsVisible : true,
        CursorToolsVisible : true,
        SearchToolsVisible : true,
       
        localeChain: "en_US"
      };
     
    var params = {
    
       }
            params.quality = "high";
            params.bgcolor = "#ffffff";
            params.allowscriptaccess = "sameDomain";
            params.allowfullscreen = "true";
            var attributes = {};
            attributes.id = "FlexPaperViewer";
            attributes.name = "FlexPaperViewer";
            swfobject.embedSWF(
                "FlexPaperViewer.swf", "flashContent",
                "650", "500",
                swfVersionStr, xiSwfUrlStr,
                flashvars, params, attributes);
   swfobject.createCSS("#flashContent", "display:block;text-align:left;");
        </script>
       
    </head>
    <body>
         <div id="flashContent">
          <p>
    </p>
        </div>
   </body>
</html>

 

3  建立文檔轉換類OFFICE2SWFUtil.java

package com.iori.webapp.util;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;

/**
 * 2014-7-24
 * @author DTF
 */
public class OFFICE2SWFUtil  implements Serializable{
 
 private String  pdf2swfTool= "D:/Program Files/SWFTools/pdf2swf.exe";
 
 private static final long serialVersionUID = 8410551332651163557L;
 
 
 
 private String fileType = ".doc;.xls;.ppt;.docx;.xlsx;.pptx";
 /**
  * office file(.doc;.xls;.ppt;.docx;.xlsx;.pptx)
  * */
 private File officFile = null;
 /**
  * pdf file
  * */
 private File pdfFile = null;
 /**
  * swfFile
  * */
 private File swfFile = null;
 
 private File swfURL = null;
 /***
  * test1.doc ---> test1 file name
  * */
 private String abFileName = null;
 
 /**
  * test1.doc ---> doc
  * */
 private String bcFileName = null;
 
 /**
  * fileURL
  * */
 private String fileURL = null;
 
 public OFFICE2SWFUtil(){
  
 }
 
 public OFFICE2SWFUtil(String fileUrl){
   officFile = new File(fileUrl);
 }
 
 public void toChange() throws IOException{
  setFileURL(officFile.getParent()); //檔案路徑
  String fileName = officFile.getName(); //檔案名稱
  setAbFileName(fileName.substring(0,fileName.lastIndexOf("."))); //檔案真實的名稱
  setBcFileName(fileName.substring(fileName.lastIndexOf("."),fileName.length())); //檔案尾碼
  if(officFile.exists()){ //判斷檔案是否存在
   if(isContext(this.getBcFileName())){ //判斷是否是office 檔案
    pdfFile = new File(getFileURL()+"/"+getAbFileName()+".pdf");
    swfURL = new File(getFileURL()+"/"+getAbFileName());
    if(!swfURL.exists()){
     swfURL.mkdirs();
    }
    
    DOC2PDFUtil dp = new DOC2PDFUtil(officFile, pdfFile);
    dp.run();
    PDF2SWFUtil.pdf2swf(getFileURL()+"/"+getAbFileName()+".pdf",getFileURL()+"/"+getAbFileName()+"/"+getAbFileName(), pdf2swfTool);
    new File(getFileURL()+"/"+getAbFileName()+".pdf").delete();
   }else if(this.getBcFileName().equals(".pdf")){ //判斷是否是 pdf檔案
    PDF2SWFUtil.pdf2swf(getFileURL()+"/"+getAbFileName()+".pdf",getFileURL()+"/"+getAbFileName()+"/"+getAbFileName(), pdf2swfTool);
   }
  }
 }
 
 private boolean isContext(String bcFileName){
  boolean flag = false;
  if(bcFileName != null && !"".endsWith(bcFileName)){
   String[] typeList = fileType.split(";");
   for(String type : typeList){
    
    if(type.equals(bcFileName)){
     return true;
    }
   }
  }
  return flag;
 }
   
 
 public static void main(String[] args) {
  try {
   new OFFICE2SWFUtil("D:/temp/1.docx").toChange();
   new OFFICE2SWFUtil("D:/temp/test1.xls").toChange();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 public File getOfficFile() {
  return officFile;
 }

 public void setOfficFile(File officFile) {
  this.officFile = officFile;
 }

 public File getPdfFile() {
  return pdfFile;
 }

 public void setPdfFile(File pdfFile) {
  this.pdfFile = pdfFile;
 }

 public File getSwfFile() {
  return swfFile;
 }

 public void setSwfFile(File swfFile) {
  this.swfFile = swfFile;
 }

 public String getAbFileName() {
  return abFileName;
 }

 public void setAbFileName(String abFileName) {
  this.abFileName = abFileName;
 }

 public String getBcFileName() {
  return bcFileName;
 }

 public void setBcFileName(String bcFileName) {
  this.bcFileName = bcFileName;
 }

 public String getFileURL() {
  return fileURL;
 }

 public void setFileURL(String fileURL) {
  this.fileURL = fileURL;
 }

 public File getSwfURL() {
  return swfURL;
 }

 public void setSwfURL(File swfURL) {
  this.swfURL = swfURL;
 }
 
}

 

 

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.