Pdf文件转成.swf格式文件,使用到swfTOOLs工具(安装包见附件)
实现此功能的Java程序代码如下(见附件pdf2swf文件):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
public class PDF2SWF {
public static final String SWFTOOLS_PATH = "C:/Program Files (x86)/SWFTools";
public static int convertPDF2SWF(String sourcePath, String destPath) throws IOException {
String command = SWFTOOLS_PATH + "/pdf2swf.exe -t \"" + sourcePath + "\" -o \"" + destPath + "\" -s flashversion=9 -s languagedir=D:\\xpdf\\xpdf-chinese-simplified ";
System.out.println("命令操作:" + command + "\n开始转换...");
// 调用外部程序
Process process = Runtime.getRuntime().exec(command);
final InputStream is1 = process.getInputStream();
new Thread(new Runnable() {
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(is1));
try {
while (br.readLine() != null);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start(); // 启动单独的线程来清空process.getInputStream()的缓冲区
InputStream is2 = process.getErrorStream();
BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));
// 保存输出结果流
StringBuilder buf = new StringBuilder();
String line = null;
while ((line = br2.readLine()) != null)
// 循环等待ffmpeg进程结束
buf.append(line);
while (br2.readLine() != null)
;
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("转换结束...");
return process.exitValue();
}
public static void pdf2swf(String sourcePath, String destPath) {
long begin_time = new Date().getTime();
try {
PDF2SWF.convertPDF2SWF(sourcePath, destPath);
}catch (Exception ex) {
System.out.println("转换过程失败!!");
}
long end_time = new Date().getTime();
System.out.println("转换共耗时 :[" + (end_time - begin_time) + "]ms");
System.out.println("转换文件成功!!");
}
public static void main(String[] args) throws IOException {
String sourcePath = "d:/123.pdf";
String destPath = "d:/456.swf";
pdf2swf(sourcePath, destPath);
}
}
如要实现一页pdf转成一个swf文件,只需在destPath的文件路径后加%
如:String destPath = "d:/456%.swf";
使用flexpaper实现swf文件的展示(见附件showSWF文件中的hello.jsp):
其中使用到了FlexPaper_2.2.4.zip文件中的部分文件如下:
JSP代码如下:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>FlexPaper AdaptiveUI</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1,user-scalable=0,width=device-width" />
<style type="text/css" media="screen">
html, body { height:100%; }
body { margin:0; padding:0; overflow:auto; }
#flashContent { display:none; }
</style>
<link rel="stylesheet" type="text/css" href="css/flexpaper.css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/flexpaper.js"></script>
<script type="text/javascript" src="js/flexpaper_handlers.js"></script>
</head>
<body>
<div id="documentViewer" class="flexpaper_viewer" style="position:absolute;left:10px;top:10px;width:80%;height:80%"></div>
<script type="text/javascript">
$('#documentViewer').FlexPaperViewer(
{ config : {
SWFFile : 'Paper.swf',
Scale : 1,
ZoomTransition : 'easeOut',
ZoomTime : 0.5,
ZoomInterval : 0.2,
FitPageOnLoad : false,
FitWidthOnLoad : false,
FullScreenAsMaxWindow : false,
ProgressiveLoading : true,
MinZoomSize : 0.2,
MaxZoomSize : 5,
SearchMatchAll : false,
InitViewMode : 'Portrait',
RenderingOrder : 'flash,html',
MixedMode : true,
ViewModeToolsVisible : true,
ZoomToolsVisible : true,
NavToolsVisible : true,
CursorToolsVisible : true,
SearchToolsVisible : true,
localeChain: 'zh_CN'
}});
</script>
</body>
</html>
解决Flexpaper分页分段加载问题的博客地址:
http://www.blogjava.net/jforeverg/archive/2011/07/06/353813.html
Java+FlexPaper+swfTools仿百度文库文档在线预览系统设计与实现地址:
http://blog.youkuaiyun.com/hil2000/article/details/8459940
附件下载链接地址:
http://pan.baidu.com/s/1i3L045Z