java pdf转swf_pdf转换成swf的代码

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

}

}

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值