加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

第二步: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 转换文件成功!!
====================================================================

/pdf2swf/src/com/b510/pdf2swf/ConvertToSwf.java

java.util.ArrayList; 12 java.util.List; 13 14 nlb @version 1.0 把pdf,jpeg,font,gif,pgn,wav转化为swf文件 18 */ 19 ConvertToSwf { 20 21 private final String CONVERTFILETYPE = "pdf,jpg,png,wav"; private String swftoolsPath; 23 24 swftoolsPath 25 * 用于进行把文件转化为swf的工具地址 26 27 public ConvertToSwf(String swftoolsPath) { 28 this.swftoolsPath=swftoolsPath; 29 } * 把文件转化为swf格式支持"pdf,wav" 32 33 sourceFilePath 34 * 要进行转化为swf文件的地址 35 swfFilePath * 转化后的swf的文件地址 @return 38 39 boolean convertFileToSwf(String sourceFilePath,String swfFilePath) { 40 System.out.println("开始转化文件到swf格式" 41 if (swftoolsPath == null || swftoolsPath == "") { 42 System.out.println("未指定要进行swf转化工具的地址!!!" 43 return false 44 } 45 String filetype = sourceFilePath.substring(sourceFilePath 46 .lastIndexOf(".") + 1); 47 判读上传文件类型是否符合转换为pdf 48 System.out.println("判断文件类型通过"if (CONVERTFILETYPE.indexOf(filetype.toLowerCase()) == -1 50 System.out.println("当前文件不符合要转化为SWF的文件类型!!!" 51 52 53 File sourceFile = File(sourceFilePath); 54 55 sourceFile.exists()) { 56 System.out.println("要进行swf的文件不存在!!!" 57 58 59 System.out.println("准备转换的文件路径存在" 60 swftoolsPath.endsWith(File.separator)) { 61 swftoolsPath += File.separator; 63 StringBuilder commandBuidler = StringBuilder(swftoolsPath); 64 File swfFile = File(swfFilePath); 65 swfFile.getParentFile().exists()) { swfFile.getParentFile().mkdirs(); 68 if (filetype.toLowerCase().equals("jpg")) { 69 filetype = "jpeg" 70 71 List<String> command = new ArrayList<String>(); 72 command.add(this.swftoolsPath+""+filetype.toLowerCase()+"2swf.exe");从配置文件里读取 73 command.add("-z"); 74 command.add("-s" 75 command.add("flashversion=9" 76 command.add("-s" 77 command.add("poly2bitmap");加入poly2bitmap的目的是为了防止出现大文件或图形过多的文件转换时的出错,没有生成swf文件的异常 command.add(sourceFilePath); 79 command.add("-o" 80 command.add(swfFilePath); 81 { 82 ProcessBuilder processBuilder = ProcessBuilder(); processBuilder.command(command); 84 Process process = processBuilder.start(); 85 System.out.println("开始生成swf文件.." 86 dealWith(process); 87 { 88 process.waitFor();等待子进程的结束,子进程就是系统调用文件转换这个新进程 89 } (InterruptedException e) { 90 e.printStackTrace(); 91 } 92 File swf = 93 swf.exists()) { 94 95 } 96 System.out.println("转化SWF文件成功!!!" 97 } (IOException e) { TODO Auto-generated catch block 99 System.out.println("转化为SWF文件失败!!!"100 e.printStackTrace(); 101 103 104 true106 main(String[] args) { 107 ConvertToSwf a=new ConvertToSwf("C:/Program Files (x86)/SWFTools"108 long begin_time=109 a.convertFileToSwf("e:/test_1352107155307.pdf","e:/test_1352107155307.swf"long end_time=111 System.out.println("result:"+(end_time-begin_time)); 112 113 void dealWith(final Process pro){ 114 下面是处理堵塞的情况 115 116 Thread(){ 117 run(){ 118 BufferedReader br1 = InputStreamReader(pro.getInputStream())); String text; 120 121 while ( (text = br1.readLine()) != ) { System.out.println(text); 123 } 124 } (IOException e) { e.printStackTrace(); 126 } 127 } }.start(); 129 } (Exception e) { e.printStackTrace(); } 132 133 134 135 BufferedReader br2 = new InputStreamReader(pro.getErrorStream()));这定不要忘记处理出理时产生的信息,不然会堵塞不前的 136 137 138 while( (text = br2.readLine()) != ){ 139 System.err.println(text); 140 141 } 142 143 144 146 } 147 148 149 } 150 }
效果是一样的,不过后台的运行情况不同:

1 开始转化文件到swf格式 判断文件类型通过 准备转换的文件路径存在 4 开始生成swf文件.. 5 NOTICE File contains links 6 NOTICE Writing SWF file e:/test_1352107155307.swf 7 转化SWF文件成功!!! 8 result:4131
这个程序处理的时间要长一点.....

源码下载地址:http://files.cnblogs.com/hongten/pdf2swf.rar

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读