做畢業設計的時候設及到類似“百度文庫”的功能模組,在網上搜半天這個辦法是最全,最易學的。在此站在巨人的肩膀上詳細的介紹一下。
主體思路:先把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>