FlexPaper+SWFTools ,java实现在线文档浏览

来源:互联网 发布:淘宝改图标 系统繁忙 编辑:程序博客网 时间:2024/05/23 19:18
1、安装openoffice,OpenOffice_3.4.0_Win_x86_zh-CN.zip,使用新建OpenOfficeService.properties文件,放到src目录下 
Java代码  收藏代码
  1. OO_HOME = C:\\Program Files (x86)\\OpenOffice.org 3  
  2. oo_host = 127.0.0.1  
  3. oo_port = 8100  


2、下载 jodconverter-2.2.2,将lib目录所有jar 复制到项目 lib目录 

3、 下载swftools-0.9.2.exe 安装后,复制安装目录中到 pdf2swf.exe 到项目附件目录中。 

4、下载Flexpaper,引用其中的js 

5、在环境变量中的Path配置如下:(注最后的“;”不要) 
C:\Program Files (x86)\OpenOffice.org 3\program;D:\IdeaProjects\zsbk\zsbk\out\artifacts\zsbk_war_exploded\JS\StreetMap\flexpaper 

Java代码  收藏代码
  1. <%@ page import="com.xxx.util.FlashUtil" %>  
  2. <%--  
  3.   Created by IntelliJ IDEA.  
  4.   Date: 13-7-11  
  5.   Time: 上午8:34  
  6.   To change this template use File | Settings | File Templates.  
  7. --%>  
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  9. <%  
  10.     String applicationPath = application.getRealPath("");  
  11. //    String filePath="http://x.x.x.x/JieJing/";  
  12.     String fileName=request.getParameter("fileName");  
  13.     String contextPath = request.getContextPath();  
  14.     String flashFile = null;  
  15.     String filePath = null;  
  16.     if(fileName.indexOf("gisPhotos")!=-1){  
  17.         fileName = fileName.substring(fileName.lastIndexOf("gisPhotos/"),fileName.length());  
  18.         filePath =applicationPath + "\\JS\\StreetMap";  
  19.         flashFile = new FlashUtil().beginConvert(filePath,fileName);//绝对路径   相对路径  
  20.         flashFile = "\\JS\\StreetMap\\" +flashFile;  
  21.         flashFile = contextPath+flashFile.replace("\\","/");  
  22.     }else{  
  23.         filePath =applicationPath + "\\JS\\StreetMap\\flexpaper";  
  24. //    fileName = new String(fileName.getBytes("ISO-8859-1"),"UTF-8");  
  25.         flashFile = new FlashUtil().beginConvert(filePath,fileName);//绝对路径   相对路径  
  26.     }  
  27. %>  
  28. <html>  
  29. <head>  
  30.     <title>在线文档浏览</title>  
  31.     <style type="text/css" media="screen">  
  32.         html, body  { height:100%; }  
  33.         body { margin:0; padding:0; overflow:auto; }  
  34.     </style>  
  35.   
  36.     <script type="text/javascript" src="js/flexpaper_flash.js"></script>  
  37. </head>  
  38. <body>  
  39. <div style="position:absolute;left:0px;top:0px;width:100%;height:100%;">  
  40.     <a id="viewerPlaceHolder" style="width:100%;height:100%;display:block"></a>  
  41.   
  42.     <script type="text/javascript">  
  43.         var fp = new FlexPaperViewer(  
  44.                 'FlexPaperViewer',  
  45.                 'viewerPlaceHolder', { config : {  
  46.                     SwfFile : escape('<%=flashFile%>'),  
  47.                     Scale : 0.6,  
  48.                     ZoomTransition : 'easeOut',  
  49.                     ZoomTime : 0.5,  
  50.                     ZoomInterval : 0.2,  
  51.                     FitPageOnLoad : true,  
  52.                     FitWidthOnLoad : false,  
  53.                     PrintEnabled : true,  
  54.                     FullScreenAsMaxWindow : false,  
  55.                     ProgressiveLoading : false,  
  56.                     MinZoomSize : 0.2,  
  57.                     MaxZoomSize : 5,  
  58.                     SearchMatchAll : false,  
  59.                     InitViewMode : 'Portrait',  
  60.   
  61.                     ViewModeToolsVisible : true,  
  62.                     ZoomToolsVisible : true,  
  63.                     NavToolsVisible : true,  
  64.                     CursorToolsVisible : true,  
  65.                     SearchToolsVisible : true,  
  66.   
  67.                     localeChain: 'zh_CN'  
  68.                 }});  
  69.     </script>  
  70. </div>  
  71. </body>  
  72. </html>  


Java代码  收藏代码
  1. package com.xxx.util;  
  2.   
  3.   
  4. import java.io.BufferedInputStream;  
  5. import java.io.File;  
  6. import java.io.FileNotFoundException;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.net.ConnectException;  
  10.   
  11. import com.artofsolving.jodconverter.DocumentConverter;  
  12. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;  
  13. import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;  
  14. import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;  
  15.   
  16. /** 
  17.  * Created with IntelliJ IDEA. 
  18.  * Date: 13-7-11 
  19.  * Time: 上午10:46 
  20.  * To change this template use File | Settings | File Templates. 
  21.  */  
  22. public class FlashUtil {  
  23.   
  24.     public static  void main(String[] args){  
  25.         String outPath = new FlashUtil().beginConvert("","附件.pdf");  
  26.         System.out.println("生成swf文件:" + outPath);  
  27. //        boolean outPath = new FlashUtil().isExistFlash("123.pdf");  
  28. //        System.out.println("是否存在swf文件:" + outPath);  
  29.     }  
  30.     private static final String DOC = ".doc";  
  31.     private static final String DOCX = ".docx";  
  32.     private static final String XLS = ".xls";  
  33.     private static final String XLSX = ".xlsx";  
  34.     private static final String PDF = ".pdf";  
  35.     private static final String SWF = ".swf";  
  36.     private static final String TOOL = "pdf2swf.exe";  
  37.   
  38.     /** 
  39.      * 入口方法-通过此方法转换文件至swf格式 
  40.      * @param filePath 上传文件所在文件夹的绝对路径 
  41.      * @param fileName  文件名称 
  42.      * @return          生成swf文件名 
  43.      */  
  44.     public String beginConvert(String filePath,String fileName) {  
  45.         String outFile = "";  
  46.         String fileNameOnly = "";  
  47.         String fileExt = "";  
  48.         if (null != fileName && fileName.indexOf(".") > 0) {  
  49.             int index = fileName.indexOf(".");  
  50.             fileNameOnly = fileName.substring(0, index);  
  51.             fileExt = fileName.substring(index).toLowerCase();  
  52.         }  
  53.         String inputFile = filePath + File.separator + fileName;  
  54.         String outputFile = "";  
  55.         //如果是flash文件,直接显示  
  56.         if(fileExt.equals(SWF)){  
  57.             outFile = fileName;  
  58.         }else {  
  59.             //主要是针对中文汉字转拼音  
  60.             fileNameOnly = new CnToSpell().getPinYin(fileNameOnly);  
  61.             //如果存在对应的flash文件  
  62.             boolean  isExistFlash = isExistFlash(filePath,fileNameOnly);  
  63.             if(isExistFlash){  
  64.                 outFile = fileNameOnly + SWF;  
  65.             }else {  
  66.                 //如果是office文档,先转为pdf文件  
  67.                 if (fileExt.equals(DOC) || fileExt.equals(DOCX) || fileExt.equals(XLS)  
  68.                         || fileExt.equals(XLSX)) {  
  69.                     outputFile = filePath + File.separator + fileNameOnly + PDF;  
  70.                     File pdfFile = new File(outputFile);  
  71.                     if(!pdfFile.exists()){//判断pdf文件是否已经生成  
  72.                         office2PDF(inputFile, outputFile);  
  73.                     }  
  74.                     inputFile = outputFile;  
  75.                     fileExt = PDF;  
  76.                 }  
  77.                 if (fileExt.equals(PDF)) {  
  78.                     outputFile = filePath + File.separator + fileNameOnly + SWF;  
  79.                     outputFile = outputFile.replace("\\","/");  
  80.                     File swfFile = new File(outputFile);  
  81.                     if(!swfFile.exists()){//判断swf文件是否已经生成  
  82.                         File parentFolder = swfFile.getParentFile();  
  83.                         if(parentFolder!=null&&!parentFolder.exists()){  
  84.                             parentFolder.mkdirs();  
  85.                         }  
  86.                         String toolFile = null;  
  87.                         if(filePath.indexOf("flexpaper")==-1){  
  88.                             toolFile = filePath + File.separator +"flexpaper"+ File.separator + TOOL;  
  89.                         }else{  
  90.                             toolFile = filePath + File.separator + TOOL;  
  91.                         }  
  92.                         convertPdf2Swf(inputFile, outputFile, toolFile);  
  93.                     }  
  94.                     outFile = fileNameOnly + SWF;  
  95.                 }  
  96.             }  
  97.         }  
  98.         return outFile;  
  99.     }  
  100.   
  101.     /** 
  102.      * 将pdf文件转换成swf文件 
  103.      * @param sourceFile pdf文件绝对路径 
  104.      * @param outFile    swf文件绝对路径 
  105.      * @param toolFile   转换工具绝对路径 
  106.      */  
  107.     private void convertPdf2Swf(String sourceFile, String outFile,  
  108.                                 String toolFile) {  
  109.         String command = toolFile + " \"" + sourceFile + "\" -o  \"" + outFile  
  110.                 + "\" -s flashversion=9 ";  
  111.         try {  
  112.             Process process = Runtime.getRuntime().exec(command);  
  113.             System.out.println(loadStream(process.getInputStream()));  
  114.             System.err.println(loadStream(process.getErrorStream()));  
  115.             System.out.println(loadStream(process.getInputStream()));  
  116.             System.out.println("###--Msg: swf 转换成功");  
  117.         } catch (Exception e) {  
  118.             e.printStackTrace();  
  119.         }  
  120.     }  
  121.   
  122.     /** 
  123.      * 检测文件夹下是否已存在对应的flash文件 
  124.      * @return 
  125.      */  
  126.         private boolean isExistFlash(String filePath,String fileNameOnly){  
  127.             String fileName = fileNameOnly.substring(fileNameOnly.lastIndexOf("/")+1);  
  128.             String newFilePath = fileNameOnly.substring(0 ,fileNameOnly.lastIndexOf("/")+1);  
  129.             File file = new File(filePath + File.separator+newFilePath);  
  130.             if(!file.exists()){//判断是否已经生成新文件夹,然后再去判断文件夹是否存在对应的flash文件  
  131.                 return false;  
  132.             }  
  133.             File[] files = file.listFiles();  
  134.             for(int j=0;j<files.length;j++){  
  135.                 if(files[j].isFile()){  
  136.                     String filesName = files[j].getName();  
  137.                     if(filesName.indexOf(".")!=-1){  
  138.                         if(SWF.equals(filesName.substring(filesName.lastIndexOf(".")).toLowerCase())){  
  139.                                 if(fileName.equals(filesName.substring(0,filesName.lastIndexOf(".")))){  
  140.                                     return true;  
  141.                                 }  
  142.                         }  
  143.                     }  
  144.                 }  
  145.             }  
  146.             return false;  
  147.         }  
  148.   
  149.     /** 
  150.      * office文档转pdf文件 
  151.      * @param sourceFile    office文档绝对路径 
  152.      * @param destFile      pdf文件绝对路径 
  153.      * @return 
  154.      */  
  155.     private int office2PDF(String sourceFile, String destFile) {  
  156.         String OpenOffice_HOME = CommUtil.OpenOffice_HOME;  
  157.         String host_Str = CommUtil.host_Str;  
  158.         String port_Str = CommUtil.port_Str;  
  159.         try {  
  160.             File inputFile = new File(sourceFile);  
  161.             if (!inputFile.exists()) {  
  162.                 return -1// 找不到源文件  
  163.             }  
  164.             // 如果目标路径不存在, 则新建该路径  
  165.             File outputFile = new File(destFile);  
  166.             if (!outputFile.getParentFile().exists()) {  
  167.                 outputFile.getParentFile().mkdirs();  
  168.             }  
  169.             // 启动OpenOffice的服务  
  170.             String command = OpenOffice_HOME  
  171.                     + "\\program\\soffice.exe -headless -accept=\"socket,host="  
  172.                     + host_Str + ",port=" + port_Str + ";urp;\"";  
  173.             System.out.println("###\n" + command);  
  174.             Process pro = Runtime.getRuntime().exec(command);  
  175.             // 连接openoffice服务  
  176.             OpenOfficeConnection connection = new SocketOpenOfficeConnection(  
  177.                     host_Str, Integer.parseInt(port_Str));  
  178.             connection.connect();  
  179.             // 转换  
  180.             DocumentConverter converter = new OpenOfficeDocumentConverter(  
  181.                     connection);  
  182.             converter.convert(inputFile, outputFile);  
  183.   
  184.             // 关闭连接和服务  
  185.             connection.disconnect();  
  186.             pro.destroy();  
  187.   
  188.             return 0;  
  189.         } catch (FileNotFoundException e) {  
  190.             System.out.println("文件未找到!");  
  191.             e.printStackTrace();  
  192.             return -1;  
  193.         } catch (ConnectException e) {  
  194.             System.out.println("OpenOffice服务监听异常!");  
  195.             e.printStackTrace();  
  196.         } catch (IOException e) {  
  197.             e.printStackTrace();  
  198.         }  
  199.         return 1;  
  200.     }  
  201.   
  202.     static String loadStream(InputStream in) throws IOException{  
  203.         int ptr = 0;  
  204.         in = new BufferedInputStream(in);  
  205.         StringBuffer buffer = new StringBuffer();  
  206.   
  207.         while ((ptr=in.read())!= -1){  
  208.             buffer.append((char)ptr);  
  209.         }  
  210.         return buffer.toString();  
  211.     }  
  212. }  



Java代码  收藏代码
  1. package xxx.platform.util;  
  2.   
  3. import net.sourceforge.pinyin4j.PinyinHelper;  
  4. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;  
  5. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;  
  6. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;  
  7. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;  
  8. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;  
  9.   
  10. /** 
  11.  * Created with IntelliJ IDEA. 
  12.  * User: xxx 
  13.  * Date: 13-7-12 
  14.  * Time: 下午3:52 
  15.  * To change this template use File | Settings | File Templates. 
  16.  */  
  17. public class CnToSpell {  
  18.   
  19.     public  String getPinYin(String src) {  
  20.         char[] t1 = null;  
  21.         t1 = src.toCharArray();  
  22.         String[] t2 = new String[t1.length];  
  23.         // 设置汉字拼音输出的格式  
  24.         HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();  
  25.         t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
  26.         t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
  27.         t3.setVCharType(HanyuPinyinVCharType.WITH_V);  
  28.         String t4 = "";  
  29.         int t0 = t1.length;  
  30.         try {  
  31.             for (int i = 0; i < t0; i++) {  
  32.                 // 判断是否为汉字字符  
  33.                 if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {  
  34.                     t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);// 将汉字的几种全拼都存到t2数组中  
  35.                     t4 += t2[0];// 取出该汉字全拼的第一种读音并连接到字符串t4后  
  36.                 } else {  
  37.                     // 如果不是汉字字符,直接取出字符并连接到字符串t4后  
  38.                     t4 += Character.toString(t1[i]);  
  39.                 }  
  40.             }  
  41.         } catch (BadHanyuPinyinOutputFormatCombination e) {  
  42.             e.printStackTrace();  
  43.         }  
  44.         if(t4.indexOf("【")!=-1){  
  45.             t4 = t4.replace("【","[");  
  46.         }  
  47.         if(t4.indexOf("】")!=-1){  
  48.             t4 = t4.replace("】","]");  
  49.         }  
  50.         if(t4.indexOf("(")!=-1){  
  51.             t4 = t4.replace("(","(");  
  52.         }  
  53.         if(t4.indexOf(")")!=-1){  
  54.             t4 = t4.replace(")",")");  
  55.         }  
  56.         return t4;  
  57.     }  
  58.   
  59.   
  60.     public  String getPinYinHeadChar(String str) {  
  61.         String convert = "";  
  62.         for (int j = 0; j < str.length(); j++) {  
  63.             char word = str.charAt(j);  
  64.             // 提取汉字的首字母  
  65.             String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);  
  66.             if (pinyinArray != null) {  
  67.                 convert += pinyinArray[0].charAt(0);  
  68.             } else {  
  69.                 convert += word;  
  70.             }  
  71.         }  
  72.         return convert;  
  73.     }  
  74.   
  75.   
  76.     public  String getCnASCII(String cnStr) {  
  77.         StringBuffer strBuf = new StringBuffer();  
  78.         // 将字符串转换成字节序列  
  79.         byte[] bGBK = cnStr.getBytes();  
  80.         for (int i = 0; i < bGBK.length; i++) {  
  81.             strBuf.append(Integer.toHexString(bGBK[i] & 0xff));  
  82.         }  
  83.         return strBuf.toString();  
  84.     }  
  85.   
  86.     public static void main(String[] args) {  
  87.         CnToSpell cnToSpell = new CnToSpell();  
  88.         String cnStr = "中华人民共和国(A-C)(12)_12345";  
  89.         System.out.println(cnToSpell.getPinYin(cnStr));  
  90.         System.out.println(cnToSpell.getPinYinHeadChar(cnStr));  
  91.         System.out.println(cnToSpell.getCnASCII(cnStr));  
  92.     }  
  93. }  


Java代码  收藏代码
  1. package xxx.platform.util;  
  2.   
  3. import java.util.ResourceBundle;  
  4.   
  5. /** 
  6.  * Created with IntelliJ IDEA. 
  7.  * User: xxx 
  8.  * Date: 13-7-12 
  9.  * Time: 上午8:42 
  10.  * To change this template use File | Settings | File Templates. 
  11.  */  
  12. public class CommUtil {  
  13.     public static final String OpenOffice_HOME;  
  14.     public static final String host_Str;  
  15.     public static final String port_Str;  
  16.   
  17.     static {  
  18.         ResourceBundle rb = ResourceBundle.getBundle("OpenOfficeService");  
  19.         OpenOffice_HOME = rb.getString("OO_HOME");  
  20.         host_Str = rb.getString("oo_host");  
  21.         port_Str = rb.getString("oo_port");  
  22.     }  
  23.   
  24.     public static int strToInt(String str){  
  25.         int i =0;  
  26.         if(str!=null&&!"".equals(str)){  
  27.             i = Integer.parseInt(str);  
  28.         }  
  29.         return i;  
  30.     }  
  31.     public static double strToDouble(String str){  
  32.         double d =0.0;  
  33.         if(str!=null&&!"".equals(str)){  
  34.             d = Double.parseDouble(str);  
  35.         }  
  36.         return d;  
  37.     }  
  38. }  


参考: 
http://blog.csdn.net/authorzhh/article/details/7470048 

http://www.cnblogs.com/qinpeifeng107/archive/2011/08/29/2158879.html
0 0