使用FlexPaper查看文档

安装插件  百度搜索

调用

//创建swf文件 第一个参数进入转换的文件路径  第二个参数输出路径的文件路径
   Office2Swf os = new Office2Swf("", main.getFileUrl());
   os.officeToPdf();

配置文件  配置pdf2swf的路径 以及文件输出路径

LUCENE_PATH=D\:\\tomcat-7.0.55\\webapps\\PICCAssets\\uploadfile\\accept\\
PDF2SWF_PATH=D\:\\pdf2swf\\

 

Office2Swf   的具体实现

package com.atsc.struts.valuesystem.dao;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.util.PropertyResourceBundle;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.atsc.common.ConfigManager;
public class Office2Swf {
 private String inputFile;
 private String outputFile;
 private String hpath;
 String fileName = "";
 String pdfPath = "";
 String LUCENE_PATH = "";
 String pdf2swf = "";
 public String getInputFile() {
  return inputFile;
 }
 public void setInputFile(String inputFile) {
  this.inputFile = inputFile;
 }
 public String getOutputFile() {
  return outputFile;
 }
 public void setOutputFile(String outputFile) {
  this.outputFile = outputFile;
 }
 /**
  * 初始化文件上传路径   以及 文件输出路径
  * pdf2swf路径   文件路径LUCENE_PATH
  * @param inputFile
  * @param outputFile
  */
 public Office2Swf(String inputFile, String outputFile) {
  this.inputFile = inputFile;
  this.outputFile = outputFile;
  PropertyResourceBundle configPic;
  try {
   configPic = new PropertyResourceBundle(ConfigManager.class.getClassLoader().getResourceAsStream("config.properties"));
   LUCENE_PATH = configPic.getString("LUCENE_PATH");
   pdf2swf = configPic.getString("PDF2SWF_PATH");
  } catch (IOException e1) {
   e1.printStackTrace();
  }
 }
 /**
  * 转化office文档为pdf
  * @throws FileNotFoundException
  */
 public void officeToPdf() throws FileNotFoundException {
  String stroffice = outputFile.split("/")[2];
  String hpath1 = getClass().getClassLoader().getResource("/").getPath();
  String hpath2 = hpath1.replace("HTAssets", "upload").replace("WEB-INF", "uploadfile").replace("classes", "accept");
  hpath = hpath2.substring(1);
  String strPdf = stroffice.substring(0, stroffice.lastIndexOf(".")) + ".pdf";
  fileName = LUCENE_PATH + stroffice;
  pdfPath = LUCENE_PATH + strPdf;
  File inputF = new File(fileName);
  File outputF = new File(pdfPath);
  OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
  try {
   connection.connect();
   DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
   converter.convert(inputF, outputF);
   System.out.println("转化成功");
  } catch (ConnectException e) {
   System.out.println("打开openoffice失败");
   e.printStackTrace();
  } finally {
   connection.disconnect();
   connection = null;
  }
  this.pdfToSwf();
 }
 /**
  * 转化pdf为swf
  */
 public void pdfToSwf() {
  String swfpath = pdfPath.substring(0, pdfPath.lastIndexOf("."));
  System.out.println("转化的SWF路径:" + swfpath);
  try {
   Process p = Runtime.getRuntime().exec(pdf2swf + "pdf2swf.exe -o " + swfpath + ".swf -s flashversion=9 " + pdfPath);
   BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
   while (br.readLine() != null) {
    System.out.println("正在转化swf中,请稍等。。。。。。。");
   }
  } catch (IOException e) {
   System.out.println("读取文件出现问题");
   e.printStackTrace();
  } finally {
   System.out.println("转化swf完成");
  }
 }
}

页面调用swf文件

需要引入的flexpaper插件的js

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/flexpaper_flash.js"></script>
<script type="text/javascript" src="js/flexpaper_flash_debug.js"></script>

 <%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
String filename=request.getAttribute("filename").toString();
String hpath = (request.getAttribute("hpath").toString())+"upload/uploadfile/accept/";
out.write("<SCRIPT language="+"'"+"JavaScript"+"'"+">var filename="+"'"+filename+"'"+";var hpath="+"'"+hpath+"'"+";</SCRIPT>");
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/flexpaper_flash.js"></script>
<script type="text/javascript" src="js/flexpaper_flash_debug.js"></script>
<style type="text/css" media="screen">
html,body {
 height: 100%;
}
body {
 margin: 0;
 padding: 0;
 overflow: auto;
}
#flashContent {
 display: none;
}
</style>
<title>文档在线预览系统</title>
</head>
<body>
 <div style="position:absolute;left:50px;top:10px;">
  <a id="viewerPlaceHolder"
   style="width:820px;height:650px;display:block"></a>
  <script type="text/javascript">
          var fpath = "swfview/"+filename; 
          var hpath = "http://192.168.1.10:8080/PICCAssets/uploadfile/accept/";
          var fp = new FlexPaperViewer( 
       'FlexPaperViewer',
       'viewerPlaceHolder', { config : {
       SwfFile : escape(hpath+filename),
       //SwfFile : escape("d:/swf"+filename),
       Scale : 1.2, 
       ZoomTransition : 'easeOut',
       ZoomTime : 0.5,
       ZoomInterval : 0.2,
       FitPageOnLoad : true,
       FitWidthOnLoad : true,
       FullScreenAsMaxWindow : false,
       ProgressiveLoading : false,
       MinZoomSize : 0.2,
       MaxZoomSize : 5,
       SearchMatchAll : false,
       //InitViewMode : 'TwoPage',
       ViewModeToolsVisible : true,
       ZoomToolsVisible : true,
       NavToolsVisible : true,
       CursorToolsVisible : false,
       //SearchToolsVisible : true,
         localeChain: 'zh_CN'
       }});
          </script>
 </div>
</body>
</html>

你可能感兴趣的:(使用FlexPaper查看文档)