pdf2swf

来源:互联网 发布:2017网络信息安全考试 编辑:程序博客网 时间:2024/05/11 05:30
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.InputStreamReader;import com.artofsolving.jodconverter.DocumentConverter;import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;public class DocConverter {private String SWFTools_Windows = "C:/SWFTools/pdf2swf.exe ";private int environment;// 环境1:windows,2:linux(涉及pdf2swf路径问题)public int getEnvironment() {return environment;}public void setEnvironment(int environment) {this.environment = environment;}private String fileName;private File pdfFile;private File swfFile;private File docFile;private File odtFile;public DocConverter(String fileString) {ini(fileString);}public DocConverter(String fileString, int environment) {this.environment = environment;ini(fileString);}/* * 初始化 @param fileString */private void ini(String fileString) {try {fileName = fileString.substring(0, fileString.lastIndexOf("/"));docFile = new File(fileString);String s = fileString.substring(fileString.lastIndexOf("/") + 1, fileString.lastIndexOf("."));fileName = fileName + "/" + s;// 用于处理TXT文档转化为PDF格式乱码,获取上传文件的名称(不需要后面的格式)String txtName = fileString.substring(fileString.lastIndexOf("."));// 判断上传的文件是否是TXT文件if (txtName.equalsIgnoreCase(".txt")) {// 定义相应的ODT格式文件名称odtFile = new File(fileName + ".odt");// 将上传的文档重新copy一份,并且修改为ODT格式,然后有ODT格式转化为PDF格式this.copyFile(docFile, odtFile);pdfFile = new File(fileName + ".pdf"); // 用于处理PDF文档} else if (txtName.equals(".pdf") || txtName.equals(".PDF")) {pdfFile = new File(fileName + ".pdf");// this.copyFile(docFile, pdfFile);} else {pdfFile = new File(fileName + ".pdf");}swfFile = new File(fileName + ".swf");} catch (Exception e) {e.printStackTrace();}}/** * @Title: copyFile @Description: docFile2 @param: @param odtFile2 @return: *         void @author: hl @time: 2014-4-17 下午9:41:52 @throws */private void copyFile(File sourceFile, File targetFile) throws Exception {// 新建文件输入流并对它进行缓冲 FileInputStream input = new FileInputStream(sourceFile);BufferedInputStream inBuff = new BufferedInputStream(input);//  新建文件输出流并对它进行缓冲FileOutputStream output = new FileOutputStream(targetFile);BufferedOutputStream outBuff = new BufferedOutputStream(output);//  缓冲数组 byte[] b = new byte[1024 * 5];int len;while ((len = inBuff.read(b)) != -1) {outBuff.write(b, 0, len);}//  刷新此缓冲的输出流outBuff.flush();//  关闭流inBuff.close();outBuff.close();output.close();input.close();}/* * 转为PDF @param file */private void doc2pdf() throws Exception {if (docFile.exists()) {if (!pdfFile.exists()) {OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);try {connection.connect();DocumentConverter converter = new OpenOfficeDocumentConverter(connection);converter.convert(docFile, pdfFile);// close the connectionconnection.disconnect();} catch (java.net.ConnectException e) {// ToDo Auto-generated catch blocke.printStackTrace();throw e;} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {e.printStackTrace();throw e;} catch (Exception e) {e.printStackTrace();throw e;}}}}/* * 转换成swf */private void pdf2swf() throws Exception {if (!swfFile.exists()) {if (pdfFile.exists()) {Runtime r = Runtime.getRuntime();if (environment == 1) {// windows环境处理Process p = null;try {String[] cmd = new String[7];cmd[0] = SWFTools_Windows;cmd[1] = "-i";cmd[2] = pdfFile.getPath().trim();cmd[3] = "-o";cmd[4] = swfFile.getPath().trim();cmd[5] = "-s";cmd[6] = "languagedir=C:\\xpdf";p = Runtime.getRuntime().exec(cmd);InputStream is2 = p.getErrorStream();BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));while (br2.readLine() != null);p.waitFor();p.exitValue();// // 如果不读取流则targetFile.exists() 文件不存在,但是程序没有问题if (pdfFile.exists()) {pdfFile.delete();}} catch (Exception e) {e.printStackTrace();throw e;} finally {if (p != null) {p.destroy();}p = null;}} else if (environment == 2) {// linux环境处理try {r.exec("pdf2swf " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");} catch (Exception e) {e.printStackTrace();throw new RuntimeException();}}}}}/* * 转换主方法 */public boolean conver() {if (swfFile.exists()) {return true;}try {doc2pdf();pdf2swf();} catch (Exception e) {return false;}if (swfFile.exists()) {return true;} else {return false;}}/* * 返回文件路径 @param s */public String getswfPath() {if (swfFile.exists()) {return swfFile.getPath();} else {return "";}}/* * 设置输出路径 */public void setOutputPath(String outputPath) {if (!outputPath.equals("")) {String realName = fileName.substring(fileName.lastIndexOf("/"), fileName.lastIndexOf("."));if (outputPath.charAt(outputPath.length()) == '/') {swfFile = new File(outputPath + realName + ".swf");} else {swfFile = new File(outputPath + realName + ".swf");}}}}

中文可能会出现乱码:

解决方案下载:

http://download.csdn.net/detail/anshichuxuezhe/9748124

放到C盘根目录下

0 0
原创粉丝点击