加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

把pdf文档转化为swf并显示

发布时间:2020-12-15 06:47:44 所属栏目:百科 来源:网络整理
导读:?? 以前曾经写过一篇把office文档转化为pdf的文章,在web中大多数都是使用flash来查看文档或者其他东西,因为我们不可能都会安装adobe reader,可是flash player我们都会安装。因此使用flash的形式来查看文档还是比较常见的,如豆丁,百度文库等。 下面就说


?? 以前曾经写过一篇把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:SWFToolspdf2swf"+" -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上有,大家可以自己去下载。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读