分享

java实现在线预览doc,txt,ppt,xls等文档,用到了libreOffice,openOffice,SWFTools,JODConverter,flexPaper

 WindySky 2016-09-20

涉及到的图片请看附件。

libreOffice转换的代码:

public String doDocToFdpLibre() {

// File inputFile = new File("d:/1.txt");
// File inputFile = new File("d:/ppt.ppt");
// File inputFile = new File("d:/pptx.pptx");
// File inputFile = new File("d:/doc.doc");
// File inputFile = new File("d:/docx.docx");
// File inputFile = new File("d:/xls.xls");
File inputFile = new File("d:/jpg.jpg");
// File inputFile = new File("d:/gif.gif");
System.out.println("libreOffice
开始转换..............................");
Long startTime = new Date().getTime();
// 
// txt:
使用libreOffice来转换pdf,转换成功,但是中文有乱码!!!!!
// doc:
这是office中的doc文档,可以转换成功,并且中文没有乱码
//docx:
这是office中的docx文档,可以转换成功,并且中文没有乱码
//ppt
pptx:转换成功。
//xls:
转换成功,没有中文乱码
//xlsx
:转换成功
//jpg
png:成功
String libreOfficePath = "D:/Program Files (x86)/LibreOffice 4";
//
此类在jodconverter3版本中存在,在2.2.2版本中不存在
DefaultOfficeManagerConfiguration configuration = newDefaultOfficeManagerConfiguration();
// libreOffice
的安装目录
configuration.setOfficeHome(new File(libreOfficePath));
//
端口号
configuration.setPortNumber(8100);
// configuration.setTaskExecutionTimeout(1000 * 60 * 5L);//
//
设置任务执行超时为5分钟
// configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);//
//
设置任务队列超时为24小时
OfficeManager officeManager = configuration.buildOfficeManager();
officeManager.start();
System.out.println("...start.....");
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
File outputFile = new File("d:/jpgLibre.pdf");
converter.convert(inputFile, outputFile);
// converter.convert(inputFile, stw, outputFile, pdf); 
//
转换结束
System.out.println("
转换结束。。。。。");
String pdfPath = outputFile.getPath();
long endTime = new Date().getTime();
long time = endTime - startTime;
System.out.println("libreOffice
转换所用时间为:"+time);
return pdfPath;
}

 

openOffice转换的代码:

public String doDocToFdpOpen() {
// File inputFile = new File("d://unicode.txt");
// File inputFile = new File("d:/pptx.pptx");
// File inputFile = new File("d:/ppt.ppt");
// File inputFile = new File("d:/docx.docx");
// File inputFile = new File("d:/doc.doc");
// File inputFile = new File("d:/xlsx.xlsx");
// File inputFile = new File("d:/jpg.jpg");
File inputFile = new File("d:/gif.gif");
System.out
.println("openOffice
开始转换....................................");
// txt:
使用openoffice来转换pdf,转换成功,但有乱码!!!!!
//
原因是编码格式不统一,如果源文件是utf-8的文件的话,就不会有乱码
//doc
docx,xls,xlsx:转换成功,没有乱码
//!!!!!jpg,png,gif
无法转换
//ppt.pptx
可以转换

long startTime = new Date().getTime();
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
} catch (ConnectException e) {
e.printStackTrace();
}
DocumentConverter converter = new OpenOfficeDocumentConverter(
connection);
File outputFile = new File("d:/gifOpen.pdf");
converter.convert(inputFile, outputFile);
connection.disconnect();
String pdfPath = outputFile.getPath();
long endTime = new Date().getTime();
long time = endTime - startTime;
System.out.println("openOffice
所用时间为:"+time);
return pdfPath;
}

 

pdf格式转为swf格式的代码:

public void doPdfToSwf(String pdfPath){
Runtime r = Runtime.getRuntime(); 
System.out.println("pdfPath:"+pdfPath);
String swfPath = pdfPath.substring(0,pdfPath.lastIndexOf('.'));
System.out.println("D:/Program Files (x86)/SWFTools/pdf2swf.exe -t"+pdfPath+" -s flashversion=9 -o "+swfPath+".swf");
try {
Process p = r.exec("D:/Program Files (x86)/SWFTools/pdf2swf.exe -t"+pdfPath+" -s flashversion=9 -o "+swfPath+".swf");

} catch (IOException e) {
e.printStackTrace();

}

 

新建web项目

flexpaper中的js文件拷到项目如图所示的位置,然后创建html文件,代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www./1999/xhtml" lang="en"xml:lang="en"> 
<head> 
<title>FlexPaper</title> 
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> 
<style type="text/css" media="screen"> 
html, body { height:100%; }
body { margin:0; padding:0; overflow:auto; } 
#flashContent { display:none; }
</style> 

<script type="text/javascript"src="js/flexpaper_flash.js"></script>
</head> 
<body> 
<div style="position:absolute;left:10px;top:10px;">
<a id="viewerPlaceHolder"style="width:680px;height:480px;display:block"></a>

<script type="text/javascript"> 
document.write(escape("d://ppt.swf"))
var fp = new FlexPaperViewer( 
'FlexPaperViewer',
'viewerPlaceHolder', { config : {
//
注意地址的写法
SwfFile : escape('d://docLibre.swf'),
Scale : 0.8, //
缩放比例
ZoomTransition : 'easeOut',//
缩放样式
ZoomTime : 0.5,//
变换缩放比例需要的时间
ZoomInterval : 0.2,//
缩放比例之间的间隔
FitPageOnLoad : true,//
初始化时自适应页面
FitWidthOnLoad : true,//
内容自适应页面宽度
FullScreenAsMaxWindow : false,//
可以最大化
ProgressiveLoading : false,//
值为true的时候为逐步加载
MinZoomSize : 0.2,//
最小缩放比例
MaxZoomSize : 5,//
最大缩放比例
SearchMatchAll : false,//
搜索的时候,目标高显示亮,测试后没有效果
InitViewMode : 'Portrait',//
启动模式
PrintPaperAsBitmap : false,

ViewModeToolsVisible : true,//
工具栏上是否显示样式选择框(就是显示缩略图或分页显示的工具)
ZoomToolsVisible : true,//
工具栏上是否显示缩放工具,包括全屏
NavToolsVisible : true,//
工具栏上是否显示导航工具(也就是页码工具)
CursorToolsVisible : true,//
工具栏上是否显示光标工具
SearchToolsVisible : true,//
工具栏上是否显示搜索

// localeChain: 'en_US'//
语言
localeChain: 'zh_CN'
}});
</script>
</div>
</body> 
</html>

注意

填写好生成的swf文件的地址

运行html文件

如果出现如图所示的加载状态,请点击这个链接:http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html#119065

选择左边第三项,然后将刚才新建html文件的地址加到右下方的位置,选择始终允许。

然后再次运行html文件,将会在线正确显示swf文件。


 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多