實(shí)現(xiàn)doc,ppt,txt等格式文件向可以在flexPaper中預(yù)覽的翻頁(yè)動(dòng)畫(huà)swf的格式轉(zhuǎn)換,一般需要先把doc,ppt,txt等格式的文件先轉(zhuǎn)換為pdf,然后再由pdf轉(zhuǎn)換為swf才能實(shí)現(xiàn)在flexpaper中進(jìn)行預(yù)覽,實(shí)現(xiàn)類(lèi)似百度豆丁的預(yù)覽效果,其轉(zhuǎn)換過(guò)程需要電腦安裝 openoffice,swfTools軟件,通過(guò)java代碼:實(shí)現(xiàn)文檔格式的轉(zhuǎn)換,下面我將我在一個(gè)分布式項(xiàng)目中的一個(gè)文檔預(yù)覽部分的思路與大家共享:
1.安裝openoffice,swfTools軟件,配置好java代碼的運(yùn)行環(huán)境。
2.啟動(dòng)openOffice服務(wù):
①、進(jìn)入openoffice安裝目錄
cd opeonofiice的安裝路徑/program
②、啟動(dòng)端口監(jiān)聽(tīng)
soffice -headless -accept="socket,host=127.0.0.1,port=8080;urp;" -nofirststartwizard
③、查看啟動(dòng)是否成功,存在8080端口即啟動(dòng)成功 netstat -an
3.在eclipse端運(yùn)行以下java代碼,實(shí)現(xiàn)文檔的格式轉(zhuǎn)換,并保存到
JodDemo.java:
public class JodDemo {
public static int convertPDF2SWF(String sourcePath, String destPath, String fileName) throws IOException {
//目標(biāo)路徑不存在則建立目標(biāo)路徑
File dest = new File(destPath);
if (!dest.exists()) dest.mkdirs();
//源文件不存在則返回
File source = new File(sourcePath);
if (!source.exists()) return 0;
//調(diào)用pdf2swf命令進(jìn)行轉(zhuǎn)換
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 = "世界各地國(guó)慶節(jié)";
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 = "世界各地國(guó)慶節(jié)";
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 {
//實(shí)現(xiàn)由pdf格式到swf格式的轉(zhuǎn)換
public int convertPDF2SWF(String sourcePath, String destPath,
String fileName) throws IOException {
// 目標(biāo)路徑不存在則建立目標(biāo)路徑
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();
}
}
}
代碼已經(jīng)在MyEclipse上運(yùn)行測(cè)試無(wú)誤,可以實(shí)現(xiàn)將本地文件實(shí)現(xiàn)格式轉(zhuǎn)換,
4.注意:注意代碼中加載各個(gè)軟件的本地路徑要正確,防止加載不到軟件而報(bào)錯(cuò)
在代碼運(yùn)行前要啟動(dòng)openoffice服務(wù),否則不能完成文件格式的轉(zhuǎn)換