码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA+FlexPaper+OpenOffice+SWFTools文档预览

时间:2015-01-05 16:13:39      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:

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.   
    3. import java.io.BufferedInputStream;  
    4. import java.io.File;  
    5. import java.io.IOException;  
    6. import java.io.InputStream;  
    7.   
    8. import com.artofsolving.jodconverter.DocumentConverter;  
    9. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;  
    10. import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;  
    11. import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;  
    12.   
    13.   
    14. public class DocConverter {  
    15.   
    16.      private static final int environment = 1;// 环境 1:windows 2:linux  
    17.      private String fileString;// (只涉及pdf2swf路径问题)  
    18.      private String outputPath = "";// 输入路径 ,如果不设置就输出在默认的位置  
    19.      private String fileName;  
    20.      private File pdfFile;  
    21.      private File swfFile;  
    22.      private File docFile;  
    23.       
    24.      public DocConverter(String fileString) {  
    25.           ini(fileString);  
    26.      }  
    27.   
    28.      /** 
    29.      * 重新设置file 
    30.      * 
    31.      * @param fileString 
    32.      */  
    33.      public void setFile(String fileString) {  
    34.           ini(fileString);  
    35.      }  
    36.   
    37.      /** 
    38.      * 初始化 
    39.      * 
    40.      * @param fileString 
    41.      */  
    42.      private void ini(String fileString) {  
    43.           this.fileString = fileString;  
    44.           fileName = fileString.substring(0, fileString.lastIndexOf("."));  
    45.           docFile = new File(fileString);  
    46.           pdfFile = new File(fileName + ".pdf");  
    47.           swfFile = new File(fileName + ".swf");  
    48.      }  
    49.       
    50.      /** 
    51.      * 转为PDF 
    52.      * 
    53.      * @param file 
    54.      */  
    55.      private void doc2pdf() throws Exception {  
    56.           if (docFile.exists()) {  
    57.                if (!pdfFile.exists()) {  
    58.                     OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);  
    59.                     try {  
    60.                          connection.connect();  
    61.                          DocumentConverter converter = new OpenOfficeDocumentConverter(connection);  
    62.                          converter.convert(docFile, pdfFile);  
    63.                          // close the connection  
    64.                          connection.disconnect();  
    65.                          System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****");  
    66.                     } catch (java.net.ConnectException e) {  
    67.                          e.printStackTrace();  
    68.                          System.out.println("****swf转换器异常,openoffice服务未启动!****");  
    69.                          throw e;  
    70.                     } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {  
    71.                          e.printStackTrace();  
    72.                          System.out.println("****swf转换器异常,读取转换文件失败****");  
    73.                          throw e;  
    74.                     } catch (Exception e) {  
    75.                          e.printStackTrace();  
    76.                          throw e;  
    77.                     }  
    78.                } else {  
    79.                     System.out.println("****已经转换为pdf,不需要再进行转化****");  
    80.                }  
    81.           } else {  
    82.                System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");  
    83.           }  
    84.      }  
    85.       
    86.      /** 
    87.      * 转换成 swf 
    88.      */  
    89.      @SuppressWarnings("unused")  
    90.      private void pdf2swf() throws Exception {  
    91.           Runtime r = Runtime.getRuntime();  
    92.           if (!swfFile.exists()) {  
    93.                if (pdfFile.exists()) {  
    94.                     if (environment == 1) {// windows环境处理  
    95.                          try {  
    96.                               Process p = r.exec("E:/Program Files/SWFTools/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9");  
    97.                               System.out.print(loadStream(p.getInputStream()));  
    98.                               System.err.print(loadStream(p.getErrorStream()));  
    99.                               System.out.print(loadStream(p.getInputStream()));  
    100.                               System.err.println("****swf转换成功,文件输出:"  
    101.                                         + swfFile.getPath() + "****");  
    102.                               if (pdfFile.exists()) {  
    103.                                    pdfFile.delete();  
    104.                               }  
    105.   
    106.                          } catch (IOException e) {  
    107.                               e.printStackTrace();  
    108.                               throw e;  
    109.                          }  
    110.                     } else if (environment == 2) {// linux环境处理  
    111.                          try {  
    112.                               Process p = r.exec("pdf2swf " + pdfFile.getPath()  
    113.                                         + " -o " + swfFile.getPath() + " -T 9");  
    114.                               System.out.print(loadStream(p.getInputStream()));  
    115.                               System.err.print(loadStream(p.getErrorStream()));  
    116.                               System.err.println("****swf转换成功,文件输出:"  
    117.                                         + swfFile.getPath() + "****");  
    118.                               if (pdfFile.exists()) {  
    119.                                    pdfFile.delete();  
    120.                               }  
    121.                          } catch (Exception e) {  
    122.                               e.printStackTrace();  
    123.                               throw e;  
    124.                          }  
    125.                     }  
    126.                } else {  
    127.                     System.out.println("****pdf不存在,无法转换****");  
    128.                }  
    129.           } else {  
    130.                System.out.println("****swf已经存在不需要转换****");  
    131.           }  
    132.      }  
    133.   
    134.      static String loadStream(InputStream in) throws IOException {  
    135.   
    136.           int ptr = 0;  
    137.           in = new BufferedInputStream(in);  
    138.           StringBuffer buffer = new StringBuffer();  
    139.   
    140.           while ((ptr = in.read()) != -1) {  
    141.                buffer.append((char) ptr);  
    142.           }  
    143.   
    144.           return buffer.toString();  
    145.      }  
    146.      /** 
    147.      * 转换主方法 
    148.      */  
    149.      @SuppressWarnings("unused")  
    150.      public boolean conver() {  
    151.   
    152.           if (swfFile.exists()) {  
    153.                System.out.println("****swf转换器开始工作,该文件已经转换为swf****");  
    154.                return true;  
    155.           }  
    156.   
    157.           if (environment == 1) {  
    158.                System.out.println("****swf转换器开始工作,当前设置运行环境windows****");  
    159.           } else {  
    160.                System.out.println("****swf转换器开始工作,当前设置运行环境linux****");  
    161.           }  
    162.           try {  
    163.                doc2pdf();  
    164.                pdf2swf();  
    165.           } catch (Exception e) {  
    166.                e.printStackTrace();  
    167.                return false;  
    168.           }  
    169.   
    170.           if (swfFile.exists()) {  
    171.                return true;  
    172.           } else {  
    173.                return false;  
    174.           }  
    175.      }  
    176.   
    177.      /** 
    178.      * 返回文件路径 
    179.      * 
    180.      * @param s 
    181.      */  
    182.      public String getswfPath() {  
    183.           if (swfFile.exists()) {  
    184.                String tempString = swfFile.getPath();  
    185.                tempString = tempString.replaceAll("\\\\", "/");  
    186.                return tempString;  
    187.           } else {  
    188.                return "";  
    189.           }  
    190.   
    191.      }  
    192.      /** 
    193.      * 设置输出路径 
    194.      */  
    195.      public void setOutputPath(String outputPath) {  
    196.           this.outputPath = outputPath;  
    197.           if (!outputPath.equals("")) {  
    198.                String realName = fileName.substring(fileName.lastIndexOf("/"),  
    199.                          fileName.lastIndexOf("."));  
    200.                if (outputPath.charAt(outputPath.length()) == ‘/‘) {  
    201.                     swfFile = new File(outputPath + realName + ".swf");  
    202.                } else {  
    203.                     swfFile = new File(outputPath + realName + ".swf");  
    204.                }  
    205.           }  
    206.      }  
    207. }  
  • java文件上传并调用转换器对文件进行转换:
    [java] view plaincopy
     
    1. String newFileName = null;  
    2.                    // 得到当前时间自1970年1月1日0时0秒开始流失的毫秒数,将这个毫秒数作为上传文件的文件名  
    3.                    long now = new Date().getTime();  
    4.                     
    5.                    // 得到保存上传文件的目录的真实路径  
    6.                    String path = ServletActionContext.getServletContext().getRealPath(uploadDir);  
    7.                   path=path.replace( ‘\\‘, ‘/‘ );  
    8.                   newFileName=now+agreement.getAgreeNum()+fileName.substring(fileName.lastIndexOf( "."));  
    9.                     
    10.                   File dir = new File(path);  
    11.                   dir.mkdirs();  
    12.                     
    13.                    //删除旧文件  
    14.                    if(!Stringer.isNullOrEmpty(agreement.getPath()))  
    15.                   {  
    16.                         File oldFile= new File(dir,agreement.getPath().substring(agreement.getPath().lastIndexOf("/")+1));  
    17.                          if(oldFile.exists())  
    18.                         {  
    19.                               oldFile.delete();  
    20.                         }  
    21.                   }  
    22.                     
    23.                     
    24.                   agreement.setPath(uploadDir+ "/"+newFileName);  
    25.                    agreementDao.saveAgreement(agreement);  
    26.                     
    27.                   BufferedOutputStream bos = null;  
    28.                   BufferedInputStream bis = null;  
    29.                    // 将上传的文件保存在本地目录  
    30.                   File newFile = null;  
    31.                    try {  
    32.                         FileInputStream fis = new FileInputStream(file);  
    33.                         bis = new BufferedInputStream(fis);  
    34.   
    35.                         newFile = new File(dir, newFileName);  
    36.                         FileOutputStream fos = new FileOutputStream(newFile);  
    37.                         bos = new BufferedOutputStream(fos);  
    38.   
    39.                          byte[] buf = new byte[1024];  
    40.                          int len = -1;  
    41.                          while ((len = bis.read(buf)) != -1) {  
    42.                               bos.write(buf, 0, len);  
    43.                         }  
    44.                           
    45.                   } catch (Exception e) {  
    46.                         e.printStackTrace();  
    47.                          throw new Exception();  
    48.                   } finally {  
    49.                          try {  
    50.                                if (null != bis)  
    51.                                     bis.close();  
    52.                                if (null != bos)  
    53.                                     bos.close();  
    54.                         } catch (Exception e) {  
    55.                               e.printStackTrace();  
    56.                                throw new Exception();  
    57.                         }  
    58.                   }  
    59.                   DocConverter d = new DocConverter(converfilename);   
    60.             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.   
    16. <title> 文档在线预览系统 </title>  
    17. </head>  
    18. <body>  
    19.         <div style="text-align : center;width: 100%;" id= "c">  
    20.               <id= "viewerPlaceHolder" style="width :820px;height:650px;" ></a>  
    21.                
    22.               <script type="text/javascript" >  
    23.                    var width=$("#c" ).width();  
    24.                    var height=$(document).height();  
    25.                   $( "#viewerPlaceHolder").css("width" ,width*0.94+"px");  
    26.                   $( "#viewerPlaceHolder").css("height" ,height+"px");  
    27.                     $("#viewerPlaceHolder" ).css("padding-left",width*0.03+ "px");  
    28.                   $( "#viewerPlaceHolder").css("display" ,"block" );  
    29.                    var name="${param.name}" ;  
    30.                   name=name.substring(0, name.lastIndexOf( "."))+".swf" ;  
    31.                          var fp = new FlexPaperViewer(   
    32.                                      ‘${ctx}/swf/FlexPaperViewer‘,  
    33.                                      ‘viewerPlaceHolder‘, { config : {  
    34.                                      SwfFile : escape( ‘${ctx}/‘+name),  
    35.                                      Scale : 1.0,  
    36.                                      ZoomTransition : ‘easeOut‘,  
    37.                                      ZoomTime : 0.5,  
    38.                                      ZoomInterval : 0.2,  
    39.                                      FitPageOnLoad : false,  
    40.                                      FitWidthOnLoad : false,  
    41.                                      FullScreenAsMaxWindow : false,  
    42.                                      ProgressiveLoading : false,  
    43.                                      MinZoomSize : 0.2,  
    44.                                      MaxZoomSize : 5,  
    45.                                      SearchMatchAll : false,  
    46.                                      InitViewMode : ‘SinglePage‘,  
    47.                                       
    48.                                      ViewModeToolsVisible : true,  
    49.                                      ZoomToolsVisible : true,  
    50.                                      NavToolsVisible : true,  
    51.                                      CursorToolsVisible : true,  
    52.                                      SearchToolsVisible : true,  
    53.                                       
    54.                                      localeChain: ‘zh_CN‘  
    55.                                      }});  
    56.               </script>              
    57.         </div>  
    58. </body>  
    59. </html>  

4、运行效果:

JAVA+FlexPaper+OpenOffice+SWFTools文档预览

标签:

原文地址:http://www.cnblogs.com/yaowen/p/4203763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!