第二步:pdf转swf
发布时间:2020-12-15 18:02:57 所属栏目:百科 来源:网络整理
导读:今天做第二步: PDF(OpenOffice+JodConverter)=SWF(pdf2swf) 做之前,我们也要先做一些准备: 1.下载SWFTools 下载地址 :http://www.swftools.org/download.html 我下载的是: swftools-2012-10-15-1307.exe 2.安装SWFTools 注意:这里的是我电脑的SWFTools
今天做第二步:PDF(OpenOffice+JodConverter)=>SWF(pdf2swf) 做之前,我们也要先做一些准备: 1.下载SWFTools 下载地址:http://www.swftools.org/download.html 我下载的是:swftools-2012-10-15-1307.exe 2.安装SWFTools 注意:这里的是我电脑的SWFTools安装目,因为程序中需要用到....所以这里需要注意一下.. 到这里,我们就安装完成啦... 3.新建一个java project /pdf2swf/src/com/b510/pdf2swf/PDF2SWF.java ? 1 /** 2 * 3 */ 4 package com.b510.pdf2swf; 5 6 import java.io.BufferedReader; 7 java.io.File; 8 java.io.IOException; 9 java.io.InputStream; 10 java.io.InputStreamReader; 11 java.util.Date; 12 13 14 * PDF转SWF工具 15 16 * @date 2012-11-5 17 * @author xhw 18 19 20 public class PDF2SWF { 21 22 23 * SWTOOLS的安装路径,我的SWFTools安装目录为:"C:/Program Files (x86)/SWFTools" 24 25 static final String SWFTOOLS_PATH = "C:/Program Files (x86)/SWFTools"; 26 27 * pdf文件后缀名 28 29 final String FILE_NAME_OF_PDF = "pdf" 30 31 * swf文件后缀名 32 33 final String FILE_NAME_OF_SWF = "swf" 34 35 36 * 获得文件的路径 37 * 38 * @param file 39 * 文件的路径,如:"c:/test/test.swf" 40 @return 文件的路径 41 42 static String getFilePath(String file) { 43 String result = file.substring(0,file.lastIndexOf("/")); 44 if (file.substring(2,3) == "/") { 45 result = file.substring(0,128)"> 46 } else 47 result = file.substring(0,file.lastIndexOf("" 48 } 49 return result; 50 } 51 52 53 * 新建一个目录 54 55 folderPath 56 * 新建目录的路径 如:"c:newFolder" 57 58 void newFolder(String folderPath) { 59 try { 60 File myFolderPath = new File(folderPath.toString()); 61 if (!myFolderPath.exists()) { 62 myFolderPath.mkdir(); 63 } 64 } catch (Exception e) { 65 System.out.println("新建目录操作出错"); 66 e.printStackTrace(); 67 68 69 70 71 * the exit value of the subprocess represented by this Process object. By 72 * convention,the value 0 indicates normal termination. 73 74 sourcePath 75 * pdf文件路径 ,如:"c:/hello.pdf" 76 destPath 77 * swf文件路径,128)"> 78 正常情况下返回:0,失败情况返回:1 79 @throws IOException 80 81 int convertPDF2SWF(String sourcePath,String destPath) throws IOException { 82 // 如果目标文件的路径是新的,则新建路径 83 newFolder(getFilePath(destPath)); 84 85 源文件不存在则返回 86 File source = File(sourcePath); 87 source.exists()) { 88 return 0 89 90 91 调用pdf2swf命令进行转换 92 String command = SWFTOOLS_PATH + "/pdf2swf.exe -t "" + sourcePath + "" -o "" + destPath + "" -s flashversion=9 -s languagedir=D:xpdfxpdf-chinese-simplified " 93 System.out.println("命令操作:" + command + "n开始转换..." 94 调用外部程序 95 Process process = Runtime.getRuntime().exec(command); 96 final InputStream is1 = process.getInputStream(); 97 new Thread( Runnable() { 98 run() { 99 BufferedReader br = new BufferedReader( InputStreamReader(is1)); 100 101 while (br.readLine() != null) 102 ; 103 } (IOException e) { 104 e.printStackTrace(); 105 } 106 107 }).start(); 启动单独的线程来清空process.getInputStream()的缓冲区 108 InputStream is2 = process.getErrorStream(); 109 BufferedReader br2 = InputStreamReader(is2)); 110 保存输出结果流 111 StringBuilder buf = StringBuilder(); 112 String line = 113 while ((line = br2.readLine()) != 114 循环等待ffmpeg进程结束 115 buf.append(line); 116 while (br2.readLine() != 117 ; 118 119 process.waitFor(); 120 } (InterruptedException e) { 121 122 123 System.out.println("转换结束..."124 process.exitValue(); 125 126 127 128 * pdf文件转换为swf文件操作 129 130 131 132 133 134 135 pdf2swf(String sourcePath,String destPath) { 136 long begin_time = Date().getTime(); 137 138 PDF2SWF.convertPDF2SWF(sourcePath,destPath); 139 } (Exception ex) { 140 System.out.println("转换过程失败!!"141 142 long end_time = 143 System.out.println("转换共耗时 :[" + (end_time - begin_time) + "]ms"144 System.out.println("转换文件成功!!"145 146 147 void main(String[] args) 148 String sourcePath = "e:/test_1352107155307." + FILE_NAME_OF_PDF; 149 String destPath = "e:/hello/test_1352107155307_" + new Date().getTime() + "." + FILE_NAME_OF_SWF; 150 pdf2swf(sourcePath,128)">151 152 } ? 4.运行效果:
5.后台运行情况 1 命令操作:C:/Program Files (x86)/SWFTools/pdf2swf.exe -t "e:/test_1352107155307.pdf" -o "e:/hello/test_1352107155307_1352171476399.swf" -s flashversion=9 -s languagedir=D:xpdfxpdf-chinese-simplified
2 开始转换...
3 转换结束...
4 转换共耗时 :[1226]ms
5 转换文件成功!!
|