Java 使用flexpaper + openoffice + jodconverter + Swftools开发类似百度文库在线阅读

先将flexpaper + openoffice + jodconverter + Swftools下载下来。

我是用的版本:

FlexPaper_1.4.5_flash.zip

jodconverter-2.2.2.zip

Apache_OpenOffice_4.0.1_Win_x86_install_zh-CN.exe

swftools-2013-04-09-1007.exe

 

1、将OpenOffice和swftools安装,安装目录最好不要有空格

2、设置环境变量   SWFTOOLS_HOME=D:\Program Files\SWFTools;并将其添加包path中%SWFTOOLS_HOME%;(不知是否是必须的)

   

3、把jodconverter-2.2.2\lib下的jar包拷贝到系统lib文件夹下

4、解压FlexPaper_1.4.5_flash将其拷贝到系统WebContent下

5、新建Pdf2swf.java

package com.lbl;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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;

/**
 * 文件大了效率太低, 有待提高性能,或使用其他插件转换pdf
 * @author Administrator
 *
 */
public class Pdf2swf extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private File sourceFile;		//转换源文件
	private File pdfFile;			//PDF目标文件
	private File swfFile;			//SWF目标文件
	private Runtime r;				
	
	public void init() throws ServletException {
		sourceFile = new File("D:/a.ppt");
		pdfFile = new File("D:/a.pdf")	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//转换成pdf文件
		if(sourceFile.exists()) {
			if(!pdfFile.exists()) {
				OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
				try {
					connection.connect();
					DocumentConverter converter = new OpenOfficeDocumentConverter(connection);   
					converter.convert(sourceFile, pdfFile);
					pdfFile.createNewFile();
					connection.disconnect();  				} catch (java.net.ConnectException e) {
					e.printStackTrace();
					System.out.println("OpenOffice服务未启动");
					throw e;
				} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
					e.printStackTrace();
					System.out.println("读取文件失败");
					throw e;
				} catch (Exception e){
					e.printStackTrace();

				}
			} else {
				System.out.println("已转换为PDF,无需再次转换");
			}
		} else {
			System.out.println("要转换的文件不存在");
		} 
		//转换成swf文件
		r = Runtime.getRuntime();
		
		String path = this.getServletConfig().getServletContext().getRealPath("/swfFile/");
		swfFile = new File(path + File.separator + "aa.swf");
		
		
		if(!swfFile.exists()){
			File file = new File(path);
			if(!file.exists()){
				file.mkdirs();
			}
			
			if(pdfFile.exists()) {
				try {
					Process p = r.exec("D:/Program Files/SWFTools/pdf2swf.exe " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
					p.waitFor();
					swfFile.createNewFile();
					if(pdfFile.exists()) {
						pdfFile.delete();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				System.out.println("PDF文件不存在,无法转换");
			}
		} else {
			System.out.println("已经转为SWF文件,无需再次转换");
		}
		HttpSession session = req.getSession();
		session.setAttribute("fileName", swfFile.getName());
		resp.sendRedirect(req.getContextPath()+"/FlexPaper_1.4.5_flash/readFile.jsp");
	}
}


 

 

6、新建jsp:(最好将新建的jsp放到FlexPaper_1.4.5_flash文件夹中,我放到其他地方文档无法显示。哪位大牛知道如何解决还请赐教)

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">	
    <head> 
        <title>FlexPaper</title>         
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
        <style type="text/css" media="screen"> 
			html, body	{ height:100%; }
			body { margin:0; padding:0; overflow:auto; }   
			#flashContent { display:none; }
        </style> 
		
		<script type="text/javascript" src="js/flexpaper_flash.js"></script>
    </head> 
    <body> 
    as
    	<div style="position:absolute;left:10px;top:10px;">
	        <a id="viewerPlaceHolder" style="width:660px;height:480px;display:block"></a>
	        
	        <script type="text/javascript"> 
				var fp = new FlexPaperViewer(	
						 'FlexPaperViewer',
						 'viewerPlaceHolder', { config : {
						 SwfFile : escape('../swfFile/aa.swf'),
						 Scale : 0.6, 
						 ZoomTransition : 'easeOut',
						 ZoomTime : 0.5,
						 ZoomInterval : 0.2,
						 FitPageOnLoad : true,
						 FitWidthOnLoad : false,
						 PrintEnabled : true,
						 FullScreenAsMaxWindow : false,
						 ProgressiveLoading : false,
						 MinZoomSize : 0.2,
						 MaxZoomSize : 5,
						 SearchMatchAll : false,
						 InitViewMode : 'Portrait',
						 
						 ViewModeToolsVisible : true,
						 ZoomToolsVisible : true,
						 NavToolsVisible : true,
						 CursorToolsVisible : true,
						 SearchToolsVisible : true,
  						
  						 localeChain: 'en_US'
						 }});
	        </script>
        </div>
   </body> 
</html> 


7、

需要注意的问题

①   

ConvertServlet中的p.waitFor();很重要,如果没有改行代码,可能出现PDF不能成功转换成SWF的情况。

②   

新建readFile.jsp的时候一定要把<head>标签中间的<base  href="<%=basePath%>">删掉,否则FlexPaper不能浏览文件。

③   

FlexPaper不能正常浏览文件的时候,可以试试在官网添加信任。网址http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html#119065

④   

readFile.jsp中动态显示文件名称的写法:第一种SwfFile :  escape('http://localhost:8080/readonline/swfFile/<%=(String)session.getAttribute("fileName")%>');  第二种:escape('../swfFile/<%=(String)session.getAttribute("fileName")%>')

⑤   

项目执行需要启动OpenOffice服务,在系统命令窗口执行命令。

cd  C:\Program Files\OpenOffice.org 3\program

 

soffice  -headless -accept="socket,host=127.0.0.1,port=8100;urp;"  -nofirststartwizard

 

 

参考文献:

http://blog.csdn.net/will_awoke/article/details/6603493

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

http://flexpaper.studylead.com/

http://user.qzone.qq.com/271069593/blog/1369667108

http://www.myexception.cn/flex/1214800.html

http://my.oschina.net/wamdy/blog/94180

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐崇拜234

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值