当前位置: 代码迷 >> 综合 >> java实现在线预览doc,txt,ppt,xls等文档,用到了libreOffice,openOffice,SWFTools,JODConverter,flexPaper
  详细解决方案

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

热度:96   发布时间:2023-12-23 06:34:34.0
涉及到的图片请看附件。libreOffice转换的代码:/** 有个jar在maven上导不下来,只能手动添加本地的<!-- jodconverter-core --><!-- https://mvnrepository.com/artifact/org.artofsolving.jodconverter/jodconverter-core --><dependency><groupId>org.artofsolving.jodconverter</groupId><artifactId>jodconverter-core</artifactId><version>3.0-beta-4</version><scope>system</scope><systemPath>${project.basedir}/src/main/resources/lib/jodconverter-core-3.0-beta-4.jar</systemPath></dependency><dependency><groupId>commons-cli</groupId><artifactId>commons-cli</artifactId><version>1.1</version></dependency><dependency><groupId>org.openoffice</groupId><artifactId>juh</artifactId><version>3.2.1</version></dependency><dependency><groupId>org.openoffice</groupId><artifactId>unoil</artifactId><version>3.2.1</version></dependency><!-- jodconverter-core -->*/
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";
// 此类在jodconverter中3版本中存在,在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.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/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文件。
--------------------- 
作者:dongdong_919 
来源:CSDN 
原文:https://blog.csdn.net/dongdong_919/article/details/44959237 
版权声明:本文为博主原创文章,转载请附上博文链接!

 

  相关解决方案