1.安装openoffice,swfTools软件,配置好java代码的运行环境。
2.启动openOffice服务:
①、进入openoffice安装目录
cd opeonofiice的安装路径/program
②、启动端口监听
soffice -headless -accept="socket,host=127.0.0.1,port=8080;urp;" -nofirststartwizard
③、查看启动是否成功,存在8080端口即启动成功 netstat -an
3.在eclipse端运行以下java代码,实现文档的格式转换,并保存到
JodDemo.java: public class JodDemo { public static int convertPDF2SWF(String sourcePath, String destPath, String fileName) throws IOException { //目标路径不存在则建立目标路径 File dest = new File(destPath); if (!dest.exists()) dest.mkdirs(); //源文件不存在则返回 File source = new File(sourcePath); if (!source.exists()) return 0; //调用pdf2swf命令进行转换 String command = "D:\\SWFTools\\pdf2swf.exe" + " -o \"" + destPath + "\\" + fileName + "\" -s languagedir=D:\\xpdf\\xpdf-chinese-simplified -s flashversion=9 \"" + sourcePath + "\""; Process pro = Runtime.getRuntime().exec(command); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(pro.getInputStream())); while (bufferedReader.readLine() != null); try { pro.waitFor(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return pro.exitValue(); } public static void main(String []args) throws IOException { String a = "世界各地国庆节"; String sourcePath = "d:\\"+a+".pdf"; String destPath = "d:\\swf\\"; String fileName = a+".swf"; JodDemo.convertPDF2SWF(sourcePath, destPath, fileName); } } Office2Pdf.java public class Office2Pdf { public static void main(String[] args) throws Exception { String a = "世界各地国庆节"; off2Pdf(a); } public static void off2Pdf(String fileName) { File inputFile = new File("d:/" + fileName + ".ppt"); File outputFile = new File("d:/" + fileName + ".pdf"); // connect to an OpenOffice.org instance running on port 8100 OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); try { connection.connect(); } catch (ConnectException e) { e.printStackTrace(); } // convert DocumentConverter converter = new OpenOfficeDocumentConverter( connection); converter.convert(inputFile, outputFile); connection.disconnect(); } } Pdf2Swf.java public class Pdf2Swf { //实现由pdf格式到swf格式的转换 public int convertPDF2SWF(String sourcePath, String destPath, String fileName) throws IOException { // 目标路径不存在则建立目标路径 File dest = new File(destPath); if (!dest.exists()) { dest.mkdirs(); } // 源文件不存在则返回 File source = new File(sourcePath); if (!source.exists()) { return 0; } String[] envp = new String[1]; envp[0] = "PATH=D:\\SWFTools\\"; String command = "pdf2swf -z -s flashversion=9 \"" + sourcePath + "\" -o \"" + destPath + fileName + "\""; Process pro = Runtime.getRuntime().exec(command, envp); // System.out.println(command); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(pro.getInputStream())); while (bufferedReader.readLine() != null) { String text = bufferedReader.readLine(); System.out.println(text); } try { pro.waitFor(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 然后在套播放器 command = "swfcombine -z -X 720 -Y 540 \"D:/SWFTools/swfs/rfxview.swf\" viewport=\"" + destPath + fileName + "\" -o \"" + destPath + fileName + "\""; pro = Runtime.getRuntime().exec(command, envp); System.out.println(command); bufferedReader = new BufferedReader(new InputStreamReader(pro .getInputStream())); while (bufferedReader.readLine() != null) { String text = bufferedReader.readLine(); System.out.println(text); } try { pro.waitFor(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return pro.exitValue(); } public static void main(String[] args) { String sourcePath = "d:/document.pdf"; String destPath = "d:/"; String fileName = "document.swf"; try { System.out.println(new Pdf2Swf().convertPDF2SWF(sourcePath, destPath, fileName)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
代码已经在MyEclipse上运行测试无误,可以实现将本地文件实现格式转换,
4.注意:注意代码中加载各个软件的本地路径要正确,防止加载不到软件而报错
在代码运行前要启动openoffice服务,否则不能完成文件格式的转换