java实现doc向swf格式的转换 转_java实现doc向swf格式的转换 转
實現doc,ppt,txt等格式文件向可以在flexPaper中預覽的翻頁動畫swf的格式轉換,一般需要先把doc,ppt,txt等格式的文件先轉換為pdf,然后再由pdf轉換為swf才能實現在flexpaper中進行預覽,實現類似百度豆丁的預覽效果,其轉換過程需要電腦安裝 openoffice,swfTools軟件,通過java代碼:實現文檔格式的轉換,下面我將我在一個分布式項目中的一個文檔預覽部分的思路與大家共享:
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服務,否則不能完成文件格式的轉換
總結
以上是生活随笔為你收集整理的java实现doc向swf格式的转换 转_java实现doc向swf格式的转换 转的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java内部类为什么会持有外部类的引用_
- 下一篇: c语言对称矩阵的压缩存储_【数据结构】对