java实现附件预览(openoffice+PDF.js)

来源:互联网 发布:java 线程中断机制 编辑:程序博客网 时间:2024/06/05 19:34

java实现附件预览(openoffice+PDF.js,将office文档,通过openoffice工具转换为PDF文件,使用PDF.js进行前端展示

是对openoffice+swftools+flexpaper的升级版,减少一次swf文件转换,及flexpaper只能预览十页内容,原文:http://blog.csdn.net/z69183787/article/details/17468039

 


 

现支持的转换格式如下:

private static String[] docFileLayouts = {".txt",".doc",".docx",".wps",".xls",".xlsx",".et",".ppt",".pptx",".dps"};//office办公软件格式private static String[] imgFileLayouts = {".jpg",".gif",".jpeg",".png",".bmp"};//图片格式private static String[] pdfFileLayouts = {".pdf"};//pdf格式

如有其它格式需要,可以对转换格式进行扩展,

 

1、 安装Apache_OpenOffice_4.1.2_Win_x86_install_en-US.exe,

下载连接:http://www.openoffice.org/download/index.html

2、 添加环境变量path=D:\Program Files (x86)\OpenOffice 4\program

3、 执行open_office_service.bat脚本,并,查看进程soffice是否运行

4、 Action新增方法

/** * .解析文章内容到PDF * @param request * @param modelMap * @param baseAddition */private void initPDF(ServletWebRequest request,ModelMap modelMap,              BaseAdditionbaseAddition) {       Stringpath = "";       if("".equals(baseAddition.getExpandOne()) || baseAddition.getExpandOne()==null){              path= KAPath + baseAddition.getOperationId()+"/"+baseAddition.getAdditionId()+"/";       }       // 调用转换类DocConverter,并将需要转换的文件传递给该类的构造方法       DocConverterd = new DocConverter(baseAddition);       // 调用conver方法开始转换,先执行doc2pdf()将office文件转换为pdf;再执行pdf2swf()将pdf转换为swf;       d.conver();       // 调用getPdfPath()方法,打印转换后的pdf文件路径       Stringpdfpath = request.getRequest().getContextPath()+path+d.getPdfName();       // 将相对路径放入sessio中保存       modelMap.put("pdfPath", pdfpath);}

4、前端jsp,PDF加载展示

<!--内容--><div class="mim_content"><iframe width="100%"height="700px" src="<%=basePath %>js/pdf/web/viewer.html?file=${pdfPath}"></iframe></div>

注:${pdfPath},

       a.PDF文件路径必须为项目路径

       b.文件名称不能包含中文s

5、切换到演示模式,由于使用iframe引入pdf页,全屏模式无法使用,具体原因可问度娘,这里只给出全屏显示思路。编辑viewer.js:

       a.将全屏按扭显示

/*if (!PDFView.supportsFullscreen) {    document.getElementById('presentationMode').classList.add('hidden');    document.getElementById('secondaryPresentationMode').      classList.add('hidden');  }*/
       b.更改按扭点击事件为自定义处理

request: function presentationModeRequest() {  //引用knowledgeLuceneDetail.jspif(parent.document.IsFullScreen==null || !parent.document.IsFullScreen){parent.launchFullscreen();}else{parent.exitFullscreen();}    return true;  }
       c.函数可以隐藏不相关div,可以全屏显示,这里使用提隐藏div

var docHtml = document.documentElement;var docBody = document.body;var videobox = document.getElementById('content');var mi_main_div = $("#mi_main_div");var module_info_div = $("#module_info_div");var module_info_2_div = $("#module_info_2_div");//進入全屏function launchFullscreen() {var cssText = 'width:100%;height:100%;overflow:hidden;';mi_main_div.hide();module_info_2_div.hide();docHtml.style.cssText = cssText;docBody.style.cssText = cssText;videobox.style.cssText = cssText + ';' + 'margin:0px;padding:0px;';module_info_div.removeClass('module_info');document.IsFullScreen = true;}//退出全屏function exitFullscreen() {mi_main_div.show();module_info_2_div.show();docHtml.style.cssText = "";docBody.style.cssText = "";videobox.style.cssText = "";module_info_div.addClass('module_info');document.IsFullScreen = false;}

js、java代码,有一些垃圾代码,大家请见量,有觉得不爽的地方,可以自己优化。

相关文件PDF.js+源码,请超链:http://download.csdn.net/detail/shnsvyu/9573577

0 0
原创粉丝点击