1.准备工作:下载OpenOfice及PDF转换工具pdf2swf.exe
OpenOfice下载地址:http://www.openoffice.org/download/index.html
Pdf2swf工具下载:http://www.openoffice.org/download/index.html
2.安装上面两个工具以及启动OpenOfice服务:
a 安装简略
b OpenOfice启动:
进入OpenOffice的安装目录找到program目录,输入以下命令:
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
3.建工程如图结构:
重要的两段代码
1 后台
package servlet;
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;
public class ConvertServlet extends HttpServlet{
private static final long serialVersionUID = -2146334159911637280L;
private File sourceFile; //转换源文件
private File pdfFile; //PDF目标文件
private File swfFile; //SWF目标文件
private Runtime r;
public void init() throws ServletException {
sourceFile = new File("D:/common/portal/apache-tomcat-6.0.20/webapps/ReadOnline/swfFile/01.ppt");
pdfFile = new File("D:/common/portal/apache-tomcat-6.0.20/webapps/ReadOnline/swfFile/01.pdf");
swfFile = new File("D:/common/portal/apache-tomcat-6.0.20/webapps/ReadOnline/swfFile/01.swf");
System.out.println("第一步:生成文件对象,准备转换");
}
@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();
System.out.println("第二步:转换为PDF格式 路径" + pdfFile.getPath());
} 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();
try {
throw e;
} catch (Exception e1) {
e1.printStackTrace();
}
}
} else {
System.out.println("已转换为PDF,无需再次转换");
}
} else {
System.out.println("要转换的文件不存在");
}
//转换成swf文件
r = Runtime.getRuntime();
if(!swfFile.exists()){
System.out.println("转换成SWF");
if(pdfFile.exists()) {
try {
System.out.println("SWF转换中");
Process p = r.exec("C:/Program Files/SWFTools/pdf2swf.exe " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
p.waitFor();
swfFile.createNewFile();
System.out.println("第三步:转换为SWF格式 路径:" + swfFile.getPath());
System.out.println("第四步:转换为SWF格式名称:" + swfFile.getName());
if(pdfFile.exists()) {
pdfFile.delete();
}
} catch (Exception e) {
e.printStackTrace();
try {
throw e;
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.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/readFile.jsp");
}
}
前台jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>在线阅读</title>
<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>
<div style="position:absolute;left:200px;top:10px;">
<a id="viewerPlaceHolder" style="width:1000px;height:800px;display:block"></a>
<script type="text/javascript">
var fp = new FlexPaperViewer(
'FlexPaperViewer',
'viewerPlaceHolder', { config : {
SwfFile : escape('/ReadOnline/swfFile/<%=(String)session.getAttribute("fileName")%>'),
Scale : 0.6,
ZoomTransition : 'easeOut',
ZoomTime : 0.5,
ZoomInterval : 0.2,
FitPageOnLoad : true,
FitWidthOnLoad : false,
PrintEnabled : false,
FullScreenAsMaxWindow : false,
ProgressiveLoading : true,
MinZoomSize : 0.2,
MaxZoomSize : 5,
SearchMatchAll : false,
InitViewMode : 'Portrait',
ViewModeToolsVisible : true,
ZoomToolsVisible : true,
NavToolsVisible : true,
CursorToolsVisible : true,
SearchToolsVisible : true,
localeChain: 'zh_CN'
}});
</script>
</div>
</body>
</html>
附上源码: http://download.youkuaiyun.com/detail/mcbeath/4480452
参考网址: http://www.cnblogs.com/qinpeifeng107/archive/2011/08/29/2158879.html