JAVA+FlexPaper+OpenOffice+SWFTools文档预览

http://blog.csdn.net/core_star/article/details/10148047

1、软件环境:

  • openoffice:启动openoffice服务:soffice.exe -headless -nologo -norestore -accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager
  • swftools

2、所需组件:

  • flexpaper : flexpaper_flash_debug.js,flexpaper_flash.js,jquery.js , FlexPaperViewer.swf
  • OpenDocument文档转换器 JODConverter, 主要用它的jodconverter-2.2.2.jar包

3、程序源码:

  • java转换器的程序代码:

    [java] view plaincopy

    1. package correy.erp.util.converter;
    2. import java.io.BufferedInputStream;
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.io.InputStream;
    6. import com.artofsolving.jodconverter.DocumentConverter;
    7. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
    8. import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
    9. import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
    10. public class DocConverter {
    11. private static final int environment = 1;// 环境 1:windows 2:linux
    12. private String fileString;// (只涉及pdf2swf路径问题)
    13. private String outputPath = "";// 输入路径 ,如果不设置就输出在默认的位置
    14. private String fileName;
    15. private File pdfFile;
    16. private File swfFile;
    17. private File docFile;
    18. public DocConverter(String fileString) {
    19. ini(fileString);
    20. }
    21. /**
    22. * 重新设置file
    23. *
    24. * @param fileString
    25. */
    26. public void setFile(String fileString) {
    27. ini(fileString);
    28. }
    29. /**
    30. * 初始化
    31. *
    32. * @param fileString
    33. */
    34. private void ini(String fileString) {
    35. this.fileString = fileString;
    36. fileName = fileString.substring(0, fileString.lastIndexOf("."));
    37. docFile = new File(fileString);
    38. pdfFile = new File(fileName + ".pdf");
    39. swfFile = new File(fileName + ".swf");
    40. }
    41. /**
    42. * 转为PDF
    43. *
    44. * @param file
    45. */
    46. private void doc2pdf() throws Exception {
    47. if (docFile.exists()) {
    48. if (!pdfFile.exists()) {
    49. OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
    50. try {
    51. connection.connect();
    52. DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
    53. converter.convert(docFile, pdfFile);
    54. // close the connection
    55. connection.disconnect();
    56. System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****");
    57. } catch (java.net.ConnectException e) {
    58. e.printStackTrace();
    59. System.out.println("****swf转换器异常,openoffice服务未启动!****");
    60. throw e;
    61. } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
    62. e.printStackTrace();
    63. System.out.println("****swf转换器异常,读取转换文件失败****");
    64. throw e;
    65. } catch (Exception e) {
    66. e.printStackTrace();
    67. throw e;
    68. }
    69. } else {
    70. System.out.println("****已经转换为pdf,不需要再进行转化****");
    71. }
    72. } else {
    73. System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");
    74. }
    75. }
    76. /**
    77. * 转换成 swf
    78. */
    79. @SuppressWarnings("unused")
    80. private void pdf2swf() throws Exception {
    81. Runtime r = Runtime.getRuntime();
    82. if (!swfFile.exists()) {
    83. if (pdfFile.exists()) {
    84. if (environment == 1) {// windows环境处理
    85. try {
    86. Process p = r.exec("E:/Program Files/SWFTools/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9");
    87. System.out.print(loadStream(p.getInputStream()));
    88. System.err.print(loadStream(p.getErrorStream()));
    89. System.out.print(loadStream(p.getInputStream()));
    90. System.err.println("****swf转换成功,文件输出:"
    91. + swfFile.getPath() + "****");
    92. if (pdfFile.exists()) {
    93. pdfFile.delete();
    94. }
    95. } catch (IOException e) {
    96. e.printStackTrace();
    97. throw e;
    98. }
    99. } else if (environment == 2) {// linux环境处理
    100. try {
    101. Process p = r.exec("pdf2swf " + pdfFile.getPath()
    102. + " -o " + swfFile.getPath() + " -T 9");
    103. System.out.print(loadStream(p.getInputStream()));
    104. System.err.print(loadStream(p.getErrorStream()));
    105. System.err.println("****swf转换成功,文件输出:"
    106. + swfFile.getPath() + "****");
    107. if (pdfFile.exists()) {
    108. pdfFile.delete();
    109. }
    110. } catch (Exception e) {
    111. e.printStackTrace();
    112. throw e;
    113. }
    114. }
    115. } else {
    116. System.out.println("****pdf不存在,无法转换****");
    117. }
    118. } else {
    119. System.out.println("****swf已经存在不需要转换****");
    120. }
    121. }
    122. static String loadStream(InputStream in) throws IOException {
    123. int ptr = 0;
    124. in = new BufferedInputStream(in);
    125. StringBuffer buffer = new StringBuffer();
    126. while ((ptr = in.read()) != -1) {
    127. buffer.append((char) ptr);
    128. }
    129. return buffer.toString();
    130. }
    131. /**
    132. * 转换主方法
    133. */
    134. @SuppressWarnings("unused")
    135. public boolean conver() {
    136. if (swfFile.exists()) {
    137. System.out.println("****swf转换器开始工作,该文件已经转换为swf****");
    138. return true;
    139. }
    140. if (environment == 1) {
    141. System.out.println("****swf转换器开始工作,当前设置运行环境windows****");
    142. } else {
    143. System.out.println("****swf转换器开始工作,当前设置运行环境linux****");
    144. }
    145. try {
    146. doc2pdf();
    147. pdf2swf();
    148. } catch (Exception e) {
    149. e.printStackTrace();
    150. return false;
    151. }
    152. if (swfFile.exists()) {
    153. return true;
    154. } else {
    155. return false;
    156. }
    157. }
    158. /**
    159. * 返回文件路径
    160. *
    161. * @param s
    162. */
    163. public String getswfPath() {
    164. if (swfFile.exists()) {
    165. String tempString = swfFile.getPath();
    166. tempString = tempString.replaceAll("\\\\", "/");
    167. return tempString;
    168. } else {
    169. return "";
    170. }
    171. }
    172. /**
    173. * 设置输出路径
    174. */
    175. public void setOutputPath(String outputPath) {
    176. this.outputPath = outputPath;
    177. if (!outputPath.equals("")) {
    178. String realName = fileName.substring(fileName.lastIndexOf("/"),
    179. fileName.lastIndexOf("."));
    180. if (outputPath.charAt(outputPath.length()) == ‘/‘) {
    181. swfFile = new File(outputPath + realName + ".swf");
    182. } else {
    183. swfFile = new File(outputPath + realName + ".swf");
    184. }
    185. }
    186. }
    187. }
  • java文件上传并调用转换器对文件进行转换:

    [java] view plaincopy

    1. String newFileName = null;
    2. // 得到当前时间自1970年1月1日0时0秒开始流失的毫秒数,将这个毫秒数作为上传文件的文件名
    3. long now = new Date().getTime();
    4. // 得到保存上传文件的目录的真实路径
    5. String path = ServletActionContext.getServletContext().getRealPath(uploadDir);
    6. path=path.replace( ‘\\‘, ‘/‘ );
    7. newFileName=now+agreement.getAgreeNum()+fileName.substring(fileName.lastIndexOf( "."));
    8. File dir = new File(path);
    9. dir.mkdirs();
    10. //删除旧文件
    11. if(!Stringer.isNullOrEmpty(agreement.getPath()))
    12. {
    13. File oldFile= new File(dir,agreement.getPath().substring(agreement.getPath().lastIndexOf("/")+1));
    14. if(oldFile.exists())
    15. {
    16. oldFile.delete();
    17. }
    18. }
    19. agreement.setPath(uploadDir+ "/"+newFileName);
    20. agreementDao.saveAgreement(agreement);
    21. BufferedOutputStream bos = null;
    22. BufferedInputStream bis = null;
    23. // 将上传的文件保存在本地目录
    24. File newFile = null;
    25. try {
    26. FileInputStream fis = new FileInputStream(file);
    27. bis = new BufferedInputStream(fis);
    28. newFile = new File(dir, newFileName);
    29. FileOutputStream fos = new FileOutputStream(newFile);
    30. bos = new BufferedOutputStream(fos);
    31. byte[] buf = new byte[1024];
    32. int len = -1;
    33. while ((len = bis.read(buf)) != -1) {
    34. bos.write(buf, 0, len);
    35. }
    36. } catch (Exception e) {
    37. e.printStackTrace();
    38. throw new Exception();
    39. } finally {
    40. try {
    41. if (null != bis)
    42. bis.close();
    43. if (null != bos)
    44. bos.close();
    45. } catch (Exception e) {
    46. e.printStackTrace();
    47. throw new Exception();
    48. }
    49. }
    50. DocConverter d = new DocConverter(converfilename);
    51. d.conver();
  • JSP页面预览文档

    [html] view plaincopy

    1. <%@ page language= "java" contentType ="text/html; charset=UTF-8"
    2. pageEncoding="UTF-8" %>
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
    4. <html>
    5. <head>
    6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    7. <script type="text/javascript" src=" ${ctx}/js/flexpaper/jquery.js"></ script>
    8. <script type="text/javascript" src=" ${ctx}/js/flexpaper/flexpaper_flash.js"></script >
    9. <script type="text/javascript" src=" ${ctx}/js/flexpaper/flexpaper_flash_debug.js"></script >
    10. <style type="text/css" media="screen">
    11. html, body   { height:100%; }
    12. body { margin :0; padding:0; overflow:auto ; }
    13. #flashContent { display :none; }
    14. </style>
    15. <title> 文档在线预览系统 </title>
    16. </head>
    17. <body>
    18. <div style="text-align : center;width: 100%;" id= "c">
    19. <a id= "viewerPlaceHolder" style="width :820px;height:650px;" ></a>
    20. <script type="text/javascript" >
    21. var width=$("#c" ).width();
    22. var height=$(document).height();
    23. $( "#viewerPlaceHolder").css("width" ,width*0.94+"px");
    24. $( "#viewerPlaceHolder").css("height" ,height+"px");
    25. $("#viewerPlaceHolder" ).css("padding-left",width*0.03+ "px");
    26. $( "#viewerPlaceHolder").css("display" ,"block" );
    27. var name="${param.name}" ;
    28. name=name.substring(0, name.lastIndexOf( "."))+".swf" ;
    29. var fp = new FlexPaperViewer(
    30. ‘${ctx}/swf/FlexPaperViewer‘,
    31. ‘viewerPlaceHolder‘, { config : {
    32. SwfFile : escape( ‘${ctx}/‘+name),
    33. Scale : 1.0,
    34. ZoomTransition : ‘easeOut‘,
    35. ZoomTime : 0.5,
    36. ZoomInterval : 0.2,
    37. FitPageOnLoad : false,
    38. FitWidthOnLoad : false,
    39. FullScreenAsMaxWindow : false,
    40. ProgressiveLoading : false,
    41. MinZoomSize : 0.2,
    42. MaxZoomSize : 5,
    43. SearchMatchAll : false,
    44. InitViewMode : ‘SinglePage‘,
    45. ViewModeToolsVisible : true,
    46. ZoomToolsVisible : true,
    47. NavToolsVisible : true,
    48. CursorToolsVisible : true,
    49. SearchToolsVisible : true,
    50. localeChain: ‘zh_CN‘
    51. }});
    52. </script>
    53. </div>
    54. </body>
    55. </html>

4、运行效果:

时间: 2024-08-30 12:05:38

JAVA+FlexPaper+OpenOffice+SWFTools文档预览的相关文章

文档预览的设计思考

文档预览服务的功能设计 1. 安全控制 2.浏览器兼容 3.适应不同的终端 4.加载优化 5.易扩展 6.集群部署 7.客户端SDK的开发(Java后端SDK.前端的Web SDK) 已有的一些方案 百度文库 I Doc View

微软office web apps 服务器搭建之在线文档预览(二)

上一篇文章已经介绍了整个安装过程了.只要在浏览器中输入文档转换server的ip,会自动跳转,出现如下页面. 那么就可以实现本地文档预览了,你可以试试.(注意:是本地哦,路径不要写错,类似“\\file\share”.如果不能预览,那么还要注意文档的权限哦,不多说.) 看到这个,就算完成80%以上的工作了,那么现在只需要集成到自己的项目中. 以下也主要是参考:http://www.cnblogs.com/poissonnotes/p/3277280.html 这篇文章也是超级经典.(上篇安装的也

微软office web apps 服务器搭建之在线文档预览(一)

office web apps安装 系统要求为Windows Server 2012, 注意:转换文档需要两台服务器,一台为转换server,另外一台为域控server.(至于为什么要两台,这个请自行google,微软是这样要求的,我也不懂) 公司的系统现在正是使用这个实现文档在线预览的,不久的将来一定会上线.后面的文章我会附上即将上线的文档转换服务的截图. 以前公司使用的再现预览方案是把文档转换成pdf文件,再然后转换成图片,实现预览.但是同步转换的时候总是出现问题,服务器内存消耗严重,apa

在线文档预览方案-office web apps

最近再做项目时,要在手机端实现在线文档预览的功能.于是百度了一下实现方案,大致是将文档转换成pdf,然后在通过插件实现预览.这些方案没有具体实现代码,也没有在线预览的地址,再加上项目时间紧迫.只能考虑其它方案,这时微软的office web apps方案映入眼帘,于是和同事一起用一台PC机折腾了几天终于完成了部署,希望通过本篇记录下安装过程和遇到的坑.目前使用该方案的有 明道 微软,我部署的服务地址:http://myscloud.vicp.cc/op/generate.aspx 下面是在线预览

pdf 文档预览

最近在写一个功能, 网站在浏览器上实现文件预览功能, 我前端的处理方式, 可以使用H5 的新标签<embed> 或者<iframe> src 属性给个文件的地址,就可以预览了,当然如果是图片或者pdf 格式的文件, 是可以的,但是doc,ppt 等文档文件必须 要转成pdf 格式的文件.后端用openOffice.org,参考文档:http://www.cnblogs.com/star-studio/archive/2011/12/09/2281807.html:先将文件下载到本地

iOS文档预览功能教程

本文转载至 http://blog.csdn.net/devday/article/details/6580444 文档iosuinavigationcontrollerextensionmicrosoftcomponents ios 4 sdk中支技文档的预览功能,何为预览?就是你打印文件时的预览功能.其用到quicklook.framework,它支持的文档格式有: iWork documents, Microsoft Office, Rich Text Format, PDF, image

在线文档预览

word excel powerpoint website

android studio布局提示文档预览,发布自动取消

Java模拟实现百度文档在线浏览

Java模拟实现百度文档在线浏览 这个思路是我参考网上而来,代码是我实现. 采用Apache下面的OpenOffice将资源文件转化为pdf文件,然后将pdf文件转化为swf文件,用FlexPaper浏览. ok, A.下载OpenOffice (转换资源文件) B.下载JodConverter(调用OpenOffice) C.下载Swftools(Pdf2Swf) D.下载 FlexPaper(浏览swf文件) 这里我已经全部下载好了,大家只需要下载:http://down.51cto.com