仿豆丁网在线浏览文件方案openoffice.org 3+swftools+flexpaper
最近在给客户开发XX系统时,客户要求上传到管理系统的文档(包括*.doc;*.docx;*.xls;*.xlsx;*.ppt;*.pptx;*.pdf;)只能预览不允许下载不允许打印。 就想打到了百度文库和豆丁网,百度文库和豆丁网的在线预览都是利用flash来播放文档的,在网上查阅了大量资料,终于实现了该项功能, 现将自己的设计和实现整理如下,可下载。 ? 将文档转成flash支持的swf文件实现在线播放
?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 4program),openOffice下载地址 http://www.openoffice.org/download/index.html (2)安装完openoffice后必须启动其server,以命令行方式启动openoffice server。进入cmd命令行提示符C:Program Files (x86)OpenOffice 4program 输入如下命令: soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard ? 每次都启动这个命令可以写个bat文件 ?
? ? 示例代码: ?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",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; } }
4.启动Tomcat输入以下路径:http://localhost:8080/DocConverter/onlineRead.jsp 运行效果如下: の (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |