pdf转swf的Java实现,可用于实现类似百度文库的效果

在Java Web开发中,我们往往会遇到将pdf或word文档上传并显示在页面上用于供用户的查看,类似于百度文库或豆丁网的形式,
那么这种需求要如何实现呢?
经过一个下午的摸索与试验,终于小有成果。下面分享一下我的思路。
我们要做的是将pdf转换成swf并在网页上显示,那么我们需要的工具:SWFTools 下载地址:http://www.swftools.org/download.html
下载后进行安装,安装完成后,为了方便使用,我们也可以将此安装路径设置成环境变量,在安装目录下可以看到很多可执行文件(.exe):

 

这些可执行文件提供了多种格式转换成swf的功能,我们所需要的就是pdf2swf.exe这个文件

下面点击开始->运行.输入cmd进入dos终端,我们先试一下pdf2swf.exe是否可以正常使用
命令格式为: pdf2swf [-options] 源文件.pdf -o 目的文件.swf

执行过后,我们会在指定地方发现转换成的swf文件,将此文件在网页上显示时,会发现只有光秃秃的flash,接下来我们要做的是给它加上一个
播放器,看上去更专业一些
在SWFTools的安装路径下(D:\Program Files\SWFTools)有一个rfxview.swf文件,将此文件与转换成的文件进行合并。
命令格式为: swfcombine rfxview.swf viewport=f:\test.swf -o f:\test.swf

Pdf2Swf.java代码:


[java]view plaincopy

  1. package com.maze.util;  

  2.  

  3. import java.io.BufferedReader;  

  4. import java.io.File;  

  5. import java.io.IOException;  

  6. import java.io.InputStreamReader;  

  7.  

  8.  

  9. public class Pdf2Swf {  

  10.      

  11.    //SWFTools的环境安装路径  

  12.    public static String SWFTOOLS_PATH="D:"+File.separator+"Program Files"+File.separator+"SWFTools"+File.separator;  

  13.    //播放器样式文件rfxview.swf的路径  

  14.    public static String RFXVIEW_SWF_PATH="f:"+File.separator+"rfxview.swf";  

  15.      

  16.    public static int convertPDF2SWF(String sourcePath, String destPath, String fileName) throws IOException{  

  17.  

  18.  

  19.        File dest = new File(destPath);       

  20.        if (!dest.exists()) {       

  21.            dest.mkdirs();       

  22.        }       

  23.      

  24.        // 源文件不存在则返回       

  25.        File source = new File(sourcePath);       

  26.        if (!source.exists()) {       

  27.            return -1;       

  28.        }       

  29.          

  30.        String[] envp = new String[1];       

  31.        envp[0] = "PATH="+SWFTOOLS_PATH;       

  32.        String command = "cmd /c \""+SWFTOOLS_PATH+"pdf2swf\" -z -s flashversion=9 " + sourcePath + " -o " + destPath + fileName ;       

  33.        Process pro = Runtime.getRuntime().exec(command, envp);       

  34.            

  35.        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(pro.getInputStream()));       

  36.        while (bufferedReader.readLine() != null) {       

  37.        }       

  38.        try {       

  39.            pro.waitFor();       

  40.        } catch (InterruptedException e) {       

  41.            e.printStackTrace();       

  42.        }       

  43.        // 然后在套播放器       

  44.        command = "cmd /c \""+SWFTOOLS_PATH+"swfcombine\" "+RFXVIEW_SWF_PATH+" viewport=" + destPath + fileName + " -o " + destPath +fileName;       

  45.        pro = Runtime.getRuntime().exec(command, envp);       

  46.        bufferedReader = new BufferedReader(new InputStreamReader(pro.getInputStream()));       

  47.        while (bufferedReader.readLine() != null) {       

  48.        }       

  49.        try {       

  50.            pro.waitFor();       

  51.        } catch (InterruptedException e) {       

  52.            e.printStackTrace();       

  53.        }       

  54.        return pro.exitValue();  

  55.    }  

  56. }  





下面在main函数中进行测试

[java]view plaincopy

  1. <pre name="code" class="java">package com.maze.main;  

  2. import java.io.IOException;  

  3. import com.maze.util.Pdf2Swf;  

  4. public class Test {  

  5.  

  6.    public static void main(String[] args) throws IOException {  

  7.        String sourcePath = "f:\\test.pdf";       

  8.            String destPath = "f:\\";       

  9.            String fileName = "test.swf";       

  10.            Pdf2Swf.convertPDF2SWF(sourcePath,destPath, fileName);  

  11.    }  

  12.  

  13. }  



执行过后生成的swf文件在页面的显示效果:HTML代码:

[html]view plaincopy

  1. <html>  

  2.    <head>  

  3.        <title>test</title>  

  4.    </head>  

  5.    <body>  

  6.        <center>  

  7.            <embed type="application/x-shockwave-flash" src="test.swf" height="800" width="900" >  

  8.            </embed>  

  9.        </center>  

  10.    </body>  

  11. </html>  


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值