OpenOffice 4+jodconverter+SWFTools+flexpape_flash实现文件在线预览

 

笔者使用openOffice提供服务,以及jodconverter提供jar将office文件转化为pdf文件;再将pdf文件转化为swf文件;最后在页面应用flexpape_flas通过flash实现预览功能。

文件目录如下所示:

后台代码PDF2Page.java代码 :

代码转换本地"D:\\test.docx"文件,若需要转换其他文件,修改文件地址接收即可。

package com.housoo.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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;
@WebServlet("/pdfShow")
public class PDF2Page extends HttpServlet{

	protected void service(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException ,java.io.IOException {
	 	String srcPath = "D:\\test.docx";
        String desPath = "D:\\test.pdf";
        // office转换为pdf
        officeToPdf(srcPath, desPath);
        // 获取或者创建目标swf存储路径
        String realPath = request.getServletContext().getRealPath("/file");
        File file = new File(realPath);
        if(!file.exists()){
        	file.mkdir();
        }
        //目标swf文件名称
        String swfpath="ssssss.swf";
        //pdf文件转swf文件
        pdf2SWF(desPath,file.getAbsolutePath()+"/"+swfpath);
        System.out.println("转换完成!");
        request.setAttribute("swfpath", "file/"+swfpath);
        request.getRequestDispatcher("index.jsp").forward(request, response);
	}
	
	/**
	 * 
	 * @Description: office转pdf
	 * @param @param srcPath
	 * @param @param desPath
	 * @param @throws IOException
	 * @param @throws ConnectException   
	 * @return void  
	 * @throws
	 * @author zsg
	 * @date 2018年4月2日
	 */
	private void officeToPdf(String srcPath, String desPath)
			throws IOException, ConnectException {
		File inputFile = new File(srcPath);
        // 输出文件目录
        File outputFile = new File(desPath);
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().exists();
        }
        // 调用openoffice服务线程
        String command = "C:/Program Files (x86)/OpenOffice 4/program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
        Process p = Runtime.getRuntime().exec(command);
        // 连接openoffice服务
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(
                "127.0.0.1", 8100);
        connection.connect();
        // 转换word到pdf
        DocumentConverter converter = new OpenOfficeDocumentConverter(
                connection);
        converter.convert(inputFile, outputFile);
        // 关闭连接
        connection.disconnect();

        // 关闭进程
        p.destroy();
	};
	
	/**
	 * 
	 * @Description: pdf转swf文件
	 * @param @param sourceFile
	 * @param @param destFile
	 * @param @return   
	 * @return int  
	 * @throws
	 * @author zsg
	 * @date 2018年4月2日
	 */
	 public static int pdf2SWF(String sourceFile, String destFile) {  
	        // 目标路径不存在则建立目标路径  
	        File dest = new File(destFile);  
	        if (!dest.getParentFile().exists())  
	            dest.getParentFile().mkdirs();  
	        // 源文件不存在则返回 -1  
	        File source = new File(sourceFile);  
	        if (!source.exists())  
	            return -1;  
	        //swf文件路径
	        String SWFTools_HOME = "C:\\Program Files (x86)\\SWFTools";
	        // 如果从文件中读取的URL地址最后一个字符不是 '\',则添加'\'  
	        try {  
	            // 调用pdf2swf命令进行转换swfextract -i - sourceFilePath.pdf -o destFilePath.swf  
	            String command =  SWFTools_HOME + "\\pdf2swf.exe  -i " + sourceFile + " -o "  
	                    + destFile;  
	            Process pro = Runtime.getRuntime().exec(command);  
	  
	            BufferedReader bufferedReader = new BufferedReader(  
	                    new InputStreamReader(pro.getInputStream()));  
	            while (bufferedReader.readLine() != null) {  
	  
	            }  
	            pro.waitFor();  
	            return pro.exitValue();  
	        } catch (InterruptedException e) {  
	            e.printStackTrace();  
	        } catch (IOException e1) {  
	            e1.printStackTrace();  
	        }  
	  
	        return 1;  
	    }  
}

前台代码:index.jsp

<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
<script type="text/javascript" src="js/jquery-1.8.3.js"></script>
<script type="text/javascript" src="js/flexpaper_flash.js"></script>
<script type="text/javascript" src="js/flexpaper_flash_debug.js"></script>
<!--  <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="js/flexpaper.js"></script>
<script type="text/javascript" src="js/flexpaper_handlers.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 fp=new FlexPaperViewer('FlexPaperViewer','viewerPlaceHolder',{config:{SwfFile:escape('${swfpath}'),Scale:1.2,
            ZoomTransition:'easeOut',ZoomTime:0.5,ZoomInterval:0.2,FitPageOnLoad:false,FitWidthOnload:false,
            FullScreenAsMaxWindow:false,ProgressiveLoading:false,MinZoomSize:0.2,MaxZoomSize:5,SearchMatchAll:false,
            InitViewMode:'SinglePage',RenderingOrder : 'flash',ViewModeToolsVisible:true,ZoomToolsVisible:true,NavToolsVisible:true,CursorToolsVisible:true,
            SearchToolsVisible:true,localeChain:'en_US'}});
          </script>
   </div>
</body>
</html>

所需工具地址链接:https://pan.baidu.com/s/1f5eIbrfKCDWpCrpX83_lrg 密码:st4d

其中openoffice、swftoos的安装路径必须换成你的安装路径。jodconverter文件夹中lib文件夹中所有文件都要导入项目。flexpaper需导入相应js文件,以及FlexPaperViewer.swf文件(文件必须与显示页面在同一路径下,参考文件目录)。

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值