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 文件的路径
*/
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();
}
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
)
// 如果目标文件的路径是新的,则新建路径
newFolder(getFilePath(destPath));
// 源文件不存在则返回
if (!source.exists()) {
return 0;
}
String path
= PropertiesUtil.
getValueByPropertyName(
ClassLoader.
getSystemResourceAsStream("toPdf.properties"),
"SWFTOOLS_PATH");
// 调用pdf2swf命令进行转换
+ "/pdf2swf.exe -t \""
+ sourcePath
+ "\" -o \""
+ destPath
+ "\" -s flashversion=9 -s languagedir=D:\\xpdf\\xpdf-chinese-simplified ";
// 调用外部程序
public void run() {
is1));
try {
while (br.readLine() != null)
;
e.printStackTrace();
}
}
}).start(); // 启动单独的线程来清空process.getInputStream()的缓冲区
// 保存输出结果流
StringBuilder buf = new StringBuilder();
while ((line = br2.readLine()) != null)
// 循环等待ffmpeg进程结束
buf.append(line);
while (br2.readLine() != null)
;
try {
process.waitFor();
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;
flag = false;
}
return flag;
}
}