用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

pdf转换成swf的代码

2014-02-24 作者: ooP举报

[java]代码库

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
 
/**
 * PDF转SWF工具
 */
public class PdfToSwf {
 
    /**
     * pdf文件后缀名
     */
    public static final String FILE_NAME_OF_PDF = "pdf";
    /**
     * swf文件后缀名
     */
    public static final String FILE_NAME_OF_SWF = "swf";
 
    /**
     * 获得文件的路径
     *
     * @param file
     *            文件的路径 ,如:"c:/test/test.swf"
     * @return 文件的路径
     */
    public static String getFilePath(String file) {
        String result = file.substring(0, file.lastIndexOf("/"));
        if (file.substring(2, 3) == "/") {
            result = file.substring(0, file.lastIndexOf("/"));
        } else if (file.substring(2, 3) == "\\") {
            result = file.substring(0, file.lastIndexOf("\\"));
        }
        return result;
    }
 
    /**
     * 新建一个目录
     *
     * @param folderPath
     *            新建目录的路径 如:"c:\\newFolder"
     */
    public static void newFolder(String folderPath) {
        try {
            File myFolderPath = new File(folderPath.toString());
            if (!myFolderPath.exists()) {
                myFolderPath.mkdir();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 转化pdf为swf文件
     *
     * @param sourcePath
     *            pdf文件路径 ,如:"c:/hello.pdf"
     * @param destPath
     *            swf文件路径,如:"c:/test/test.swf"
     * @return 正常情况下返回:0,失败情况返回:1
     * @throws IOException
     */
    public static int convertPDF2SWF(String sourcePath, String destPath)
            throws IOException {
        // 如果目标文件的路径是新的,则新建路径
        newFolder(getFilePath(destPath));
 
        // 源文件不存在则返回
        File source = new File(sourcePath);
        if (!source.exists()) {
            return 0;
        }
 
        String path = PropertiesUtil.getValueByPropertyName(
                ClassLoader.getSystemResourceAsStream("toPdf.properties"), "SWFTOOLS_PATH");
 
        // 调用pdf2swf命令进行转换
        String command = path
                + "/pdf2swf.exe  -t \""
                + sourcePath
                + "\" -o  \""
                + destPath
                + "\" -s flashversion=9 -s languagedir=D:\\xpdf\\xpdf-chinese-simplified ";
        // 调用外部程序
        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();
        }
        return process.exitValue();
    }
 
    /**
     * pdf文件转换为swf文件操作
     *
     * @param sourcePath
     *            pdf文件路径 ,如:"c:/hello.pdf"
     * @param destPath
     *            swf文件路径,如:"c:/test/test.swf"
     * @return
     */
    public static boolean pdf2swf(String sourcePath, String destPath) {
        boolean flag = false;
        try {
            PdfToSwf.convertPDF2SWF(sourcePath, destPath);
            flag = true;
        } catch (Exception ex) {
            flag = false;
        }
        return flag;
    }
 
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...