[Java] pdf转换成swf的代码 →→→→→进入此内容的聊天室

来自 , 2019-04-22, 写在 Java, 查看 117 次.
URL http://www.code666.cn/view/aff16212
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6.  
  7. /**
  8.  * PDF转SWF工具
  9.  */
  10. public class PdfToSwf {
  11.  
  12.         /**
  13.          * pdf文件后缀名
  14.          */
  15.         public static final String FILE_NAME_OF_PDF = "pdf";
  16.         /**
  17.          * swf文件后缀名
  18.          */
  19.         public static final String FILE_NAME_OF_SWF = "swf";
  20.  
  21.         /**
  22.          * 获得文件的路径
  23.          *
  24.          * @param file
  25.          *            文件的路径 ,如:"c:/test/test.swf"
  26.          * @return 文件的路径
  27.          */
  28.         public static String getFilePath(String file) {
  29.                 String result = file.substring(0, file.lastIndexOf("/"));
  30.                 if (file.substring(2, 3) == "/") {
  31.                         result = file.substring(0, file.lastIndexOf("/"));
  32.                 } else if (file.substring(2, 3) == "\\") {
  33.                         result = file.substring(0, file.lastIndexOf("\\"));
  34.                 }
  35.                 return result;
  36.         }
  37.  
  38.         /**
  39.          * 新建一个目录
  40.          *
  41.          * @param folderPath
  42.          *            新建目录的路径 如:"c:\\newFolder"
  43.          */
  44.         public static void newFolder(String folderPath) {
  45.                 try {
  46.                         File myFolderPath = new File(folderPath.toString());
  47.                         if (!myFolderPath.exists()) {
  48.                                 myFolderPath.mkdir();
  49.                         }
  50.                 } catch (Exception e) {
  51.                         e.printStackTrace();
  52.                 }
  53.         }
  54.  
  55.         /**
  56.          * 转化pdf为swf文件
  57.          *
  58.          * @param sourcePath
  59.          *            pdf文件路径 ,如:"c:/hello.pdf"
  60.          * @param destPath
  61.          *            swf文件路径,如:"c:/test/test.swf"
  62.          * @return 正常情况下返回:0,失败情况返回:1
  63.          * @throws IOException
  64.          */
  65.         public static int convertPDF2SWF(String sourcePath, String destPath)
  66.                         throws IOException {
  67.                 // 如果目标文件的路径是新的,则新建路径
  68.                 newFolder(getFilePath(destPath));
  69.  
  70.                 // 源文件不存在则返回
  71.                 File source = new File(sourcePath);
  72.                 if (!source.exists()) {
  73.                         return 0;
  74.                 }
  75.  
  76.                 String path = PropertiesUtil.getValueByPropertyName(
  77.                                 ClassLoader.getSystemResourceAsStream("toPdf.properties"), "SWFTOOLS_PATH");
  78.  
  79.                 // 调用pdf2swf命令进行转换
  80.                 String command = path
  81.                                 + "/pdf2swf.exe  -t \""
  82.                                 + sourcePath
  83.                                 + "\" -o  \""
  84.                                 + destPath
  85.                                 + "\" -s flashversion=9 -s languagedir=D:\\xpdf\\xpdf-chinese-simplified ";
  86.                 // 调用外部程序
  87.                 Process process = Runtime.getRuntime().exec(command);
  88.                 final InputStream is1 = process.getInputStream();
  89.                 new Thread(new Runnable() {
  90.                         public void run() {
  91.                                 BufferedReader br = new BufferedReader(new InputStreamReader(
  92.                                                 is1));
  93.                                 try {
  94.                                         while (br.readLine() != null)
  95.                                                 ;
  96.                                 } catch (IOException e) {
  97.                                         e.printStackTrace();
  98.                                 }
  99.                         }
  100.                 }).start(); // 启动单独的线程来清空process.getInputStream()的缓冲区
  101.                 InputStream is2 = process.getErrorStream();
  102.                 BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));
  103.                 // 保存输出结果流
  104.                 StringBuilder buf = new StringBuilder();
  105.                 String line = null;
  106.                 while ((line = br2.readLine()) != null)
  107.                         // 循环等待ffmpeg进程结束
  108.                         buf.append(line);
  109.                 while (br2.readLine() != null)
  110.                         ;
  111.                 try {
  112.                         process.waitFor();
  113.                 } catch (InterruptedException e) {
  114.                         e.printStackTrace();
  115.                 }
  116.                 return process.exitValue();
  117.         }
  118.  
  119.         /**
  120.          * pdf文件转换为swf文件操作
  121.          *
  122.          * @param sourcePath
  123.          *            pdf文件路径 ,如:"c:/hello.pdf"
  124.          * @param destPath
  125.          *            swf文件路径,如:"c:/test/test.swf"
  126.          * @return
  127.          */
  128.         public static boolean pdf2swf(String sourcePath, String destPath) {
  129.                 boolean flag = false;
  130.                 try {
  131.                         PdfToSwf.convertPDF2SWF(sourcePath, destPath);
  132.                         flag = true;
  133.                 } catch (Exception ex) {
  134.                         flag = false;
  135.                 }
  136.                 return flag;
  137.         }
  138.  
  139. }
  140.  

回复 "pdf转换成swf的代码"

这儿你可以回复上面这条便签

captcha