仿百度文库方案[openoffice.org 3+swftools+flexpaper](四) 之 使用swftools将pdf转换为swf(转)...

原文出处:http://www.cnblogs.com/star-studio/archive/2011/12/09/2282483.html

第四步,使用swftools将pdf转换为swf

     建议下载swftools-0.9.1,笔者起先下载的是最新版的swftools-1.0版。貌似转换时出错,缺少什么组件。

     继续笔者的DocConverter项目。笔者使用的开发环境是MyEclipse 9.0。

新建PDF2SWFUtil.java

package com.iori.webapp.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class PDF2SWFUtil {
     
    /**
      * 利用SWFTools工具将pdf转换成swf,转换完后的swf文件与pdf同名
       * @author iori
      * @param fileDir PDF文件存放路径(包括文件名)
       * @param exePath 转换器安装路径
       * @throws IOException
      */
    public static synchronized void pdf2swf(String fileDir, String exePath) throws IOException {
        //文件路径
         String filePath = fileDir.substring(0, fileDir.lastIndexOf("/"));
        //文件名,不带后缀
         String fileName = fileDir.substring((filePath.length() + 1), fileDir.lastIndexOf("."));
        Process pro = null;
        if (isWindowsSystem()) {
            //如果是windows系统
              //命令行命令
        //注意下方语句最后的.swf前面的%。如果文件过大,需要每页分成一个swf异步加载,则用如下语句;如文件不需要拆分成多个swf,只生成一个swf,则把%去        掉即可
String cmd = exePath + " \"" + fileDir + "\" -s flashversion=9 -o \"" + filePath + "/" + fileName + "%.swf\""; //Runtime执行后返回创建的进程对象 pro = Runtime.getRuntime().exec(cmd); } else { //如果是linux系统,路径不能有空格,而且一定不能用双引号,否则无法创建进程 String[] cmd = new String[3]; cmd[0] = exePath; cmd[1] = fileDir; cmd[2] = filePath + "/" + fileName + ".swf"; //Runtime执行后返回创建的进程对象 pro = Runtime.getRuntime().exec(cmd); } //非要读取一遍cmd的输出,要不不会flush生成文件(多线程) new DoOutput(pro.getInputStream()).start(); new DoOutput(pro.getErrorStream()).start(); try { //调用waitFor方法,是为了阻塞当前进程,直到cmd执行完 pro.waitFor(); } catch (InterruptedException e) { e.printStackTrace(); } } /** * 判断是否是windows操作系统 * @author iori * @return */ private static boolean isWindowsSystem() { String p = System.getProperty("os.name"); return p.toLowerCase().indexOf("windows") >= 0 ? true : false; } /** * 多线程内部类 * 读取转换时cmd进程的标准输出流和错误输出流,这样做是因为如果不读取流,进程将死锁 * @author iori */ private static class DoOutput extends Thread { public InputStream is; //构造方法 public DoOutput(InputStream is) { this.is = is; } public void run() { BufferedReader br = new BufferedReader(new InputStreamReader(this.is)); String str = null; try { //这里并没有对流的内容进行处理,只是读了一遍 while ((str = br.readLine()) != null); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } } /** * 测试main方法 * @param args */ public static void main(String[] args) { //转换器安装路径 String exePath = "c:/Program Files/SWFTools/pdf2swf.exe"; try { PDF2SWFUtil.pdf2swf("c:/temp/333.pdf", exePath); } catch (IOException e) { System.err.println("转换出错!"); e.printStackTrace(); } } }

在PDF2SWFUtil.java,右键属性 - >Run as - >Java Application ,输出main的测试结果。

 

在jsp中执行

新建MyPDF2SWFTest.jsp

<%@ page import="java.io.*"%>
<%@ page import="com.artofsolving.jodconverter.openoffice.connection.*"%>
<%@ page import="com.artofsolving.jodconverter.openoffice.connection.*"%>
<%@ page import="com.artofsolving.jodconverter.openoffice.converter.*"%>
<%@ page import="com.artofsolving.jodconverter.*"%>
<%@ page import="java.util.*"%>
<%@ page import="com.iori.webapp.util.*"%>

<%
//转换器安装路径
String exePath = "c:/Program Files/SWFTools/pdf2swf.exe";
try {
    PDF2SWFUtil.pdf2swf("c:/temp/333.pdf", exePath);
} catch (IOException e) {
    System.err.println("转换出错!");
    e.printStackTrace();
}
%>

<!-- 下面这些html可以去掉 -->
<html> 
<head>
<title>Simple jsp page</title>
</head> 
<body>Place your content here</body>
</html>

在项目DocConverter根目录,右键属性 - >Run as - >MyEclipse Server Application

发布到之前安装的Tomcat 6.0的根目录,然后用url路径访问:Http://localhost:8080/DocConverter/MyPDF2SWFTest.jsp 进行测试。

 

PS:原文作者在PDF2SWFUtil.java文件中的转换方式不能正常显示转换后的文件,此处我做了修改,在pdf2swf方法中,

原文作者方法: String cmd = exePath + " \"" + fileDir + "\" -o \"" + filePath + "/" + fileName + ".swf\"";

本人方法:String cmd = exePath + " \"" + fileDir + "\" -s flashversion=9  -o \"" + filePath + "/" + fileName + ".swf\"";

生成版本为9的swf可以正常显示。

转载于:https://www.cnblogs.com/m3Lee/p/3849125.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值