Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。本人用过itext(文本可以,图片样式都不行).openoffice,(不能支持docx格式)效果都不是很好,经过摸索,jcom好像目前支持最好的,最高支持word 2007,以为微软更新太快了。如果有其他更好的方式,请分享出来,共同学习进步。
(jcom +swftool +FlexPage方式实现)
jodconcerter 2.2.1.rar下载地址:
http://sourceforge.net/projects/jodconverter/files/
(里面lib目录下含多个要用到的jar文件,全拷进去)
Jcom方式(可以转docx——》pdf)
window下:引入jcom.dll文件。安装Adobe Acrobat Professional (下载注册机,电话激活)
注册机下载地址:ed2k://|file|[Adobe.Acrobat.8.1.Pro.%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87%E5%AE%98%E6%96%B9%E9%9B%86%E6%88%90%E7%89%88].Acrobat.8.Pro.Keygen.exe|53760|C7FDEC684ABB2CFEB1239F9FBB8EE31E|/
JCom-2.2.0-1.jar压缩包,将里面的jcom.jar包考到lib目录下,把jcom.dll引入到工程项目下(不然,就报Exception in thread "main" java.lang.UnsatisfiedLinkError: no jcom in java.library.path错误)。
Acrobat_Pro_8.1.2.zip压缩包,安装,用于生成pdf文件用的。
package com.test;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import jp.ne.so_net.ga2.no_ji.jcom.IDispatch;
import jp.ne.so_net.ga2.no_ji.jcom.ReleaseManager;
/**
* doc docx格式转换
*
* @author Administrator
*
*/
public class DocConverter {
private static final int environment = 1;// 环境 1:windows 2:linux
// (只涉及pdf2swf路径问题)
private String fileString;
private String outputPath = "";// 输入路径 ,如果不设置就输出在默认的位置
private String fileName;
private File pdfFile;
private File swfFile;
private File docFile;
public DocConverter(String fileString) {
ini(fileString);
}
/**
* 重新设置file
*
* @param fileString
*/
public void setFile(String fileString) {
ini(fileString);
}
/**
* 初始化
*
* @param fileString
*/
private void ini(String fileString) {
this.fileString = fileString;
fileName = fileString.substring(0, fileString.lastIndexOf("."));
docFile = new File(fileString);
pdfFile = new File(fileName + ".pdf");
swfFile = new File(fileName + ".swf");
}
/**
* 转为PDF
*
* @param file
*/
private void doc2pdf() throws Exception {
ReleaseManager rm = null;
IDispatch app = null;
try {
rm=new ReleaseManager();
app = new IDispatch(rm, "PDFMakerAPI.PDFMakerApp");
app.method("CreatePDF",new Object[]{docFile.getPath(),pdfFile.getPath()});
} catch (Exception e) {
throw e;
} finally {
try {
app=null;
rm.release();
rm = null;
} catch (Exception e) {
throw e;
}
}
}
/**
* 转换成 swf
*/
private void pdf2swf() throws Exception {
Runtime r = Runtime.getRuntime();
if (!swfFile.exists()) {
if (pdfFile.exists()) {
if (environment == 1) {// windows环境处理
try {
Process p = r.exec("D:\\SWFTools\\pdf2swf.exe "
+ pdfFile.getPath() + " -o "
+ swfFile.getPath() + " -T 9");
System.out.print(loadStream(p.getInputStream()));
System.err.print(loadStream(p.getErrorStream()));
System.out.print(loadStream(p.getInputStream()));
System.err.println("****swf转换成功,文件输出:"
+ swfFile.getPath() + "****");
if (pdfFile.exists()) {
pdfFile.delete();
}
} catch (IOException e) {
e.printStackTrace();
throw e;
}
} else if (environment == 2) {// linux环境处理
try {
Process p = r.exec("pdf2swf " + pdfFile.getPath()
+ " -o " + swfFile.getPath() + " -T 9");
System.out.print(loadStream(p.getInputStream()));
System.err.print(loadStream(p.getErrorStream()));
System.err.println("****swf转换成功,文件输出:"
+ swfFile.getPath() + "****");
if (pdfFile.exists()) {
pdfFile.delete();
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
} else {
System.out.println("****pdf不存在,无法转换****");
}
} else {
System.out.println("****swf已经存在不需要转换****");
}
}
static String loadStream(InputStream in) throws IOException {
int ptr = 0;
in = new BufferedInputStream(in);
StringBuffer buffer = new StringBuffer();
while ((ptr = in.read()) != -1) {
buffer.append((char) ptr);
}
return buffer.toString();
}
/**
* 转换主方法
*/
public boolean conver() {
if (swfFile.exists()) {
System.out.println("****swf转换器开始工作,该文件已经转换为swf****");
return true;
}
if (environment == 1) {
System.out.println("****swf转换器开始工作,当前设置运行环境windows****");
} else {
System.out.println("****swf转换器开始工作,当前设置运行环境linux****");
}
try {
doc2pdf();
pdf2swf();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
if (swfFile.exists()) {
return true;
} else {
return false;
}
}
/**
* 返回文件路径
*
* @param s
*/
public String getswfPath() {
if (swfFile.exists()) {
String tempString = swfFile.getPath();
tempString = tempString.replaceAll("\\\\", "/");
return tempString;
} else {
return "";
}
}
/**
* 设置输出路径
*/
public void setOutputPath(String outputPath) {
this.outputPath = outputPath;
if (!outputPath.equals("")) {
String realName = fileName.substring(fileName.lastIndexOf("/"),
fileName.lastIndexOf("."));
if (outputPath.charAt(outputPath.length()) == '/') {
swfFile = new File(outputPath + realName + ".swf");
} else {
swfFile = new File(outputPath + realName + ".swf");
}
}
}
public static void main(String s[]) {
DocConverter d = new DocConverter("D:/test/test.docx");
d.conver();
}
}