1、JAVA+FlexPaper+swfTools 仿文库文档在线阅读一、文档在线阅读思路 1.用 OpenOffice 把 PPT、 Word、 Excel、 Text 转 换 为 pdf2.用 SWFTool 将 生 成 的 pdf 转 换 成 swf, 然 后 利 用 FlexPlayer 实 现 在线 预 览 播 放二 、 准 备 工 作1.安 装 OpenOffice, 官 网 下 载 地 址 :http:/www.openoffice.org/download/index.html, 最 新 版 为 3.4.1, 我 使 用的 版 本 为3.3.0: http:/ -nologo -h
2、eadless nofirststartwizard参 考 资 料 : http:/ 载JODConverter: http:/ 项 目 中主 要 使 用 lib 目 录 下 的 jar 包 。4.下 载 并 安 装SWFTools: http:/www.swftools.org/download.html, 下 载 exe 文 件安 装 完 成 即 可5.下 载 FlexPlayerhttp:/ java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStream;im
3、port java.io.InputStreamReader;import com.artofsolving.jodconverter.DocumentConverter;import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;import com.artofsolving.jodconverter.openoffice.
4、converter.OpenOfficeDocumentConverter;public class DocConverter private static final int environment = 1;/ 环境 1:windows,2:linux(涉及pdf2swf 路径问题)private String fileString;private String outputPath = “;/ 输入路径,如果不设置就输出在默认位置private String fileName;private File pdfFile;private File swfFile;private File do
5、cFile;public DocConverter(String fileString) ini(fileString);/* 重新设置 file param fileString*/public void setFile(String fileString) ini(fileString);/* 初始化 param fileString*/private void ini(String fileString) this.fileString = fileString;fileName = fileString.substring(0, fileString.lastIndexOf(“.“);
6、docFile = new File(fileString);pdfFile = new File(fileName + “.pdf“);swfFile = new File(fileName + “.swf“);/* 转为 PDF param file*/private void doc2pdf() throws Exception if (docFile.exists() if (!pdfFile.exists() OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);try connection.co
7、nnect();DocumentConverter converter = new OpenOfficeDocumentConverter(connection);converter.convert(docFile, pdfFile);/ close the connectionconnection.disconnect();System.out.println(“*pdf 转换成功, PDF 输出:“ + pdfFile.getPath() + “*“); catch (.ConnectException e) / ToDo Auto-generated catch blocke.print
8、StackTrace();System.out.println(“*swf 转换异常,openoffice 服务未启动!*“);throw e; catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) e.printStackTrace();System.out.println(“*swf 转换器异常,读取转换文件失败*“);throw e; catch (Exception e) e.printStackTrace();throw e; else System.out.println(
9、“*已经转换为 pdf,不需要再进行转化*“); else System.out.println(“*swf 转换器异常,需要转换的文档不存在,无法转换*“);/* 转换成 swf*/private void pdf2swf() throws Exception Runtime r = Runtime.getRuntime();if (!swfFile.exists() if (pdfFile.exists() if (environment = 1)/ windows 环境处理try / 这里根据 SWFTools 安装路径需要进行相应更改Process p = r.exec(“d:/SWF
10、Tools/pdf2swf.exe “ + pdfFile.getPath() + “ -o “ + swfFile.getPath() + “ -T 9“);System.out.print(loadStream(p.getInputStream();System.err.print(loadStream(p.getErrorStream();System.out.print(loadStream(p.getInputStream();System.err.println(“*swf 转换成功,文件输出:“ + swfFile.getPath() + “*“);if (pdfFile.exi
11、sts() pdfFile.delete(); catch (Exception e) e.printStackTrace();throw e; else if (environment = 2)/ linux 环境处理try Process p = r.exec(“pdf2swf “ + pdfFile.getPath() + “ -o “ + swfFile.getPath() + “ -T 9“);System.out.print(loadStream(p.getInputStream();System.err.print(loadStream(p.getErrorStream();Sy
12、stem.err.println(“*swf 转换成功,文件输出:“ + swfFile.getPath() + “*“);if (pdfFile.exists() pdfFile.delete(); catch (Exception e) e.printStackTrace();throw e; else System.out.println(“*pdf 不存在,无法转换 *“); else System.out.println(“*swf 已存在不需要转换*“);static String loadStream(InputStream in) throws IOException int
13、ptr = 0;/把 InputStream 字节流 替换为 BufferedReader 字符流 2013-07-17 修改BufferedReader reader = new BufferedReader(new InputStreamReader(in);StringBuilder buffer = new StringBuilder();while (ptr = reader.read() != -1) buffer.append(char) ptr);return buffer.toString();/* 转换主方法*/public boolean conver() if (swf
14、File.exists() System.out.println(“*swf 转换器开始工作,该文件已经转换为swf*“);return true;if (environment = 1) System.out.println(“*swf 转换器开始工作,当前设置运行环境windows*“); else System.out.println(“*swf 转换器开始工作,当前设置运行环境linux*“);try doc2pdf();pdf2swf(); catch (Exception e) / TODO: Auto-generated catch blocke.printStackTrace(
15、);return false;if (swfFile.exists() return true; else return false;/* 返回文件路径 param s*/public String getswfPath() if (swfFile.exists() String tempString = swfFile.getPath();tempString = tempString.replaceAll(“, “/“);return tempString; else return “;/* 设置输出路径*/public void setOutputPath(String outputPa
16、th) this.outputPath = outputPath;if (!outputPath.equals(“) String realName = fileName.substring(fileName.lastIndexOf(“/“), fileName.lastIndexOf(“.“);if (outputPath.charAt(outputPath.length() = /) swfFile = new File(outputPath + realName + “.swf“); else swfFile = new File(outputPath + realName + “.sw
17、f“);public static void main(String s) DocConverter d = new DocConverter(“E:/TDDOWNLOAD/test.doc“);d.conver();运行结果:3.新建 documentUpload.jsp文档在线预览系统 body margin-top: 100px;background: #fff;font-family: Verdana, Tahoma;a color: #CE4614;#msg-box color: #CE4614;font-size: 0.9em;text-align: center;#msg-box
18、 .logo border-bottom: 5px solid #ECE5D9;margin-bottom: 20px;padding-bottom: 10px;#msg-box .title font-size: 1.4em;font-weight: bold;margin: 0 0 30px 0;#msg-box .nav margin-top: 20px;请上传要处理的文件,过程可能需要几分钟,请稍候片刻。4.新建 docUploadConvertAction.jsp“);%Insert title herebody margin-top: 100px;background: #fff;
19、font-family: Verdana, Tahoma;a color: #CE4614;#msg-box color: #CE4614;font-size: 0.9em;text-align: center;#msg-box .logo border-bottom: 5px solid #ECE5D9;margin-bottom: 20px;padding-bottom: 10px;#msg-box .title font-size: 1.4em;font-weight: bold;margin: 0 0 30px 0;#msg-box .nav margin-top: 20px;5.新建
20、 documentView.jsphtml,body height: 100%;body margin: 0;padding: 0;overflow: auto;#flashContent display: none;文档在线预览系统 var fp = new FlexPaperViewer(FlexPaperViewer,viewerPlaceHolder, config : SwfFile : escape(),/编码设置Scale : 0.6, ZoomTransition : easeOut,/变焦过渡ZoomTime : 0.5,ZoomInterval : 0.2,/缩放滑块 -移
21、动的缩放基础 工具栏FitPageOnLoad : true,/自适应页面FitWidthOnLoad : true,/自适应宽度FullScreenAsMaxWindow : false,/全屏按钮-新页面全屏工具栏ProgressiveLoading : false,/分割加载MinZoomSize : 0.2,/最小缩放MaxZoomSize : 3,/最大缩放SearchMatchAll : true,InitViewMode : Portrait,/初始显示模式(SinglePage,TwoPage,Portrait)ViewModeToolsVisible : true,/显示模式
22、工具栏是否显示ZoomToolsVisible : true,/缩放工具栏是否显示NavToolsVisible : true,/跳页工具栏CursorToolsVisible : false,SearchToolsVisible : true,PrintPaperAsBitmap:false,localeChain: en_US);6.部署后访问:http:/localhost:8080/OfficeOnline/documentUpload.jsp上传成功后预览:7.若出现 swf 无法预览,请访问http:/ 将生成 swf 的文件夹设置为信任文件。8.参考资料:http:/ flexpaper 搜索文字时不能高亮的问题: http:/ flexpaper 去简介去水印等:http:/