把pdf文档转化为swf并显示


   以前曾经写过一篇把office文档转化为pdf的文章,在web中大多数都是使用flash来查看文档或者其他东西,因为我们不可能都会安装adobe reader,可是flash player我们都会安装。因此使用flash的形式来查看文档还是比较常见的,如豆丁,百度文库等。

下面就说说怎么实现这一个功能吧。

1.下载swftools,并安装在自己的电脑上。我这里把swftools安装在G盘(G:\SWFTools),这一步应该都没什么问题。

2.使用命令把pdf文档转换为swf文件,写命令的时候要特别注意flash的版本问题,默认转换出来的是flash8版本,这里是不能用这个版本的,因为转换后的文档只有1页。

下面就来写一下这个转化程序。

package bao1;
import java.io.*;
public class Converter extends java.lang.Thread{ 

    public static int convertPDF2SWF(String sourcePath, String destPath, String fileName) throws IOException {   
        //目标路径不存在则建立目标路径   
         File dest = new File(destPath);   
        if (!dest.exists()) dest.mkdirs();   
           
        //源文件不存在则返回   
         File source = new File(sourcePath);   
        if (!source.exists()) return 0;   
           
        //调用pdf2swf命令进行转换   
        //调用pdf2swf命令进行转换  
       
        String command= "G:\\SWFTools\\pdf2swf"+" -t "+sourcePath+" -o "+destPath+fileName+" -s flashversion=9 ";   

        Process pro = Runtime.getRuntime().exec(command);   
      
         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(pro.getInputStream()));   
        while (bufferedReader.readLine() != null);   
           
        try {   
             pro.waitFor();   
            // pro2.waitFor();
         } catch (InterruptedException e) {   
            // TODO Auto-generated catch block   
             e.printStackTrace();   
         }   
           
        return pro.exitValue();   
           
     }   
       
    
}  

   需要注意的是不能省略-t,-o,-s 左右的空格。看一下这个命令就知道为什么了

 

下面是显示文件的页面

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page language="java" import="java.io.File"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>显示所有的pdf文档</title>

<style type="text/css">
  #lb{
     list-style:none;
     line-height:50px;
     cursor:pointer;
  }
</style>
</head>
<body>
  <div id="msg">
   <% 
     String path=this.getServletContext().getRealPath("/document");
     File f=new File(path);
    String files[]=f.list();
     for(int i=0;i<files.length;i++)
     {
    	 %>
    	
    	  <li id="lb"><a href="show.jsp?mc=<%=files[i]%>"><%=files[i]%></a></li>
    	 <%
     }
   %>
   </div>
   
</body>
</html>


这个jsp用来把pdf文档转换成swf;

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page language="java" import="bao1.Converter,java.io.*"
	%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>处理</title>

		
</head>
<body>
	<%
		  String s = new String(request.getParameter("mc").getBytes("ISO8859-1"),"UTF-8");
		   
		  Converter.convertPDF2SWF(this.getServletContext().getRealPath("/") +"document"+java.io.File.separator+s,this.getServletContext().getRealPath("/"),"temp.swf");
			%>
			  <img src='../images/loading.gif'/>
			<%
		   response.setHeader("refresh","10;URL=xianshi.jsp");
			
			
			
	%>
	  
	
	
	 
</body>
</html>



最后写一个页面显示swf,这里就要使用flexpaper了,使用方法看看源码就知道了

<%@ 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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</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_debug.js"></script>
		<script type="text/javascript" src="../js/jquery.js"></script>
</head>
<body>
  <div style="position:absolute;left:10px;top:10px;">
	        <a id="viewerPlaceHolder" style="width:660px;height:480px;display:block"></a>
	        345
	        <script type="text/javascript"> 
	        
				var fp = new FlexPaperViewer(	
						 '../FlexPaperViewer',
						 'viewerPlaceHolder', { config : {
						 SwfFile : '../temp.swf',
						 Scale : 0.6, 
						 ZoomTransition : 'easeOut',
						 ZoomTime : 0.5,
						 ZoomInterval : 0.2,
						 FitPageOnLoad : false,
						 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>

下面是显示结果,截图如下


需要注意的是,由于IE上可能有缓存,所以显示出来的文档可能不对。建议使用火狐浏览器查看。另外并不是每个pdf文档都可以转换成功的,这点需要注意。

如果有什么疑问可以找我,我的QQ是1214273312,加我要注明“csdn博客网友”。如果代码有错误或有什么疑问,请大家提出来一起讨论

另外源码在csdn上有,大家可以自己去下载。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值