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; } }