首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web开发 >

最近实现了双平台下仿豆丁网百度文库阅读doc等文档的例子,分享给大家

2012-03-09 
最近实现了双平台下仿豆丁网百度文库阅读doc等文档的例子,分享给大家 - Web 开发 / 其他http://blog.csdn.

最近实现了双平台下仿豆丁网百度文库阅读doc等文档的例子,分享给大家 - Web 开发 / 其他
http://blog.csdn.net/aimsam/article/details/6600983

上张效果图


Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用openoffice转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
 
此过程涉及软件(实测)
openoffice3.2(window) 
swftools-2011-01-23-1815.exe(window)
openoffice3.1.1(linux)
swftools-2011-01-23-1815.tar.gz(linux)
jar包jodconverter-2.2.2(里面lib目录下含多个要用到的jar文件)
flexpaper1.4.0 (直接复制过来,项目上运行成功,本地运行还没成功)
FlexPaper_1.4.5_flash(网上下载的最新版本没试过, 二选一即可)
附上自己写的java转换Demo DocConverter.java
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

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;


/**
 * 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{
if(docFile.exists()) {
if(!pdfFile.exists()) {
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);  
converter.convert(docFile, pdfFile);  
// close the connection  
connection.disconnect(); 
System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath() + "****");
} catch (java.net.ConnectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("****swf转换器异常,openoffice服务未启动!****");
throw e;
} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
e.printStackTrace();
System.out.println("****swf转换器异常,读取转换文件失败****");
throw e;
} catch (Exception e){
e.printStackTrace();
throw e;
}
} else {
System.out.println("****已经转换为pdf,不需要再进行转化****");
}
} else {
System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");
}
}

/**
* 转换成 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:/TOOLS/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:/data/FTP/midlongforecast/midweatherforecast/20010202.xml");
d.conver();
}
}


以上即是所有用到的转换需要用到的文件

windows安装过程省略,linux下安装swftools命令
解压到目录后
./configure 
make 
make install

无论在哪个平台安装完以上两个软件后
需要在将openoffice以服务方式启动
Window 启动方式
cd C:\Program Files\OpenOffice.org 3\program 
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" –nofirststartwizard
Linux启动
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &


以上是实现两个平台下的转换,但是会有openoffice解析不了的字体乱码问题,或者显示字体变形等
更高级的web文档应用可以参考QQ邮件,邮件实现了在线预览excel doc pdf等格式文件并可以用鼠标点击选取复制,期待高手解决.


[解决办法]
两次转换不会影响性能吗?
------解决方案--------------------


能发个可执行文件吗,一直想找个这样的软降,如果有一定的文档管理功能就更好了,哈哈哈哈

热点排行