上一篇文章说的是word,ppt,excel转pdf。其实和这次pdf转swf是一个项目里面的,只不过我分了2步来说
pdf转成swf需要用到的工具是SWFTools,里面有一个exe文件叫pdf2swf,就是我们之后要调的工具
里面还有一个叫做rfxview.swf的文件,是我们嵌套进去的swf模版
嵌套模板了之后,得到的swf不仅有我们所需要最核心的文件内容,还有swf模版上的翻页、全屏、变大变小等,和百度文库差不多。不过我觉得没有百度文库的好看,找了很长时间也没找到好的模板。。。
下载地址
http://115.com/file/dpu1pevd
上code咯
核心代码 pdf2swf.java
package com.maze.util; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; public class Pdf2Swf { //工具文件夹路径 public static String SWFTOOLS_PATH="D:/SWFTools/"; //播放器模板路径 public static String RFXVIEW_SWF_PATH="D:/SWFTools/rfxview.swf"; 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 -1; } //pdf转成swf String[] envp = new String[1]; envp[0] = "PATH="+SWFTOOLS_PATH; String command = "cmd /c \""+SWFTOOLS_PATH+"pdf2swf\" -z -s flashversion=9 " + sourcePath + " -o " + destPath + fileName ; Process pro = Runtime.getRuntime().exec(command, envp); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(pro.getInputStream())); while (bufferedReader.readLine() != null) { } try { pro.waitFor(); } catch (InterruptedException e) { e.printStackTrace(); } //套用播放器 command = "cmd /c \""+SWFTOOLS_PATH+"swfcombine\" "+RFXVIEW_SWF_PATH+" viewport=" + destPath + fileName + " -o " + destPath +fileName; pro = Runtime.getRuntime().exec(command, envp); bufferedReader = new BufferedReader(new InputStreamReader(pro.getInputStream())); try { pro.waitFor(); } catch (InterruptedException e) { e.printStackTrace(); } return pro.exitValue(); } }
测试代码test.java
package com.maze.main; import java.io.IOException; import com.maze.util.Pdf2Swf; public class Test { public static void main(String[] args) throws IOException { String sourcePath = "d:\\java.pdf"; String destPath = "d:\\"; String fileName = "test.swf"; Pdf2Swf.convertPDF2SWF(sourcePath,destPath, fileName); } }