在页面中显示文档信息供客户在线查看文档内容
目前activex控件可以较好的完成文档的在线编辑和查看功能,但是找了很多网站都是对外收费的。根据需求并不需要对文档进行在线编辑,只要能够查看,所以采用doc转换为pdf在转换为swf使用flexpaper在线显示。
本章针对win7 64bit 系统 java环境开发
1.安装OpenOffice,swfTools软件,配置好java代码的运行环境。
2.启动openOffice服务:
①、进入openoffice安装目录
cd opeonofiice的安装路径/program
②、启动端口监听
soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard
③、查看启动是否成功,存在8100端口即启动成功 netstat -an
3.现在language包xpdf-chinese-simplified.tar和项目开发使用工具包jodconverter
将jodconverter解压以后,把lib下面的jar包全部添加到项目
doc2pdf处理方法,E:\\tools\\swftools是swfTools安装路径,D:\\xpdf\\xpdf-chinese-simplified为xpdf-chinese-simplified文件夹路径
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
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 = "E:\\tools\\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();
}
}
doc2pdf测试类
8100:opeonofiice中设置的端口,由于启动此服务占用内存比较大,所以使用后将连接释放。
package com.wenhi.cms.common.util.swf;
import java.io.File;
import java.net.ConnectException;
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 Office2Pdf {
public static void main(String[] args) throws Exception {
String a = "业务需求0319";
off2Pdf(a);
}
public static void off2Pdf(String fileName) {
File inputFile = new File("d:/" + fileName + ".doc");
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方式,这种方式有个需要注意的地方,在调用安装程序处理时在系统上配置环境变量并不能
被程序调用,window中测试如此。所以需要写完整路径
Runtime.exec() 有四种调用方法
* public Process exec(String command);
* public Process exec(String [] cmdArray);
* public Process exec(String command, String [] envp);
* public Process exec(String [] cmdArray, String [] envp);
package com.wenhi.cms.common.util.swf;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
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=E:\\tools\\swftools\\";
// String command = "pdf2swf -z -s flashversion=9 \"" + sourcePath
// + "\" -o \"" + destPath + fileName + "\"";
String command = "E:\\tools\\swftools\\pdf2swf.exe -z -s flashversion=9 \"" + sourcePath
+ "\" -o \"" + destPath + fileName + "\"";
Runtime rt = Runtime.getRuntime();
//Process pro = rt.exec(cmd,envp); 这种方法不能使用虽然配置有环境变量但是程序不能够自动调用
Process pro = rt.exec(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) {
e.printStackTrace();
}
// 然后在套播放器
command = "E:\\tools\\swftools\\swfcombine -z -X 720 -Y 540 \"E:/tools/swftools/swfs/rfxview.swf\" viewport=\""
+ destPath + fileName + "\" -o \"" + destPath + fileName + "\"";
pro = Runtime.getRuntime().exec(command);
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) {
e.printStackTrace();
}
return pro.exitValue();
}
public static void main(String[] args) {
String sourcePath = "d:/业务需求0319.pdf";
String destPath = "d:/";
String fileName = "业务需求0319.swf";
try {
System.out.println(new Pdf2Swf().convertPDF2SWF(sourcePath,
destPath, fileName));
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意:注意代码中加载各个软件的本地路径要正确,防止加载不到软件而报错
在代码运行前要启动openoffice服务,否则不能完成文件格式的转换