上一篇文章里面使用了openOffice+swfTools+FlexPaper将上传文档转pdf转swf实现文档的预览,因为swf中无法提取上传word文档的目录,这次试用了openOffice+pdf2html的方式,将word转为pdf再转html,从html中来提取目录的信息实现目录和文档的预览。
jsp中上传文件,采用form表单提交,在web.xml中配置,后台采用servlet来处理请求。在将上传的word文件的路径传递给office2pdf方法后,在该方法中使用openOffice插件将word文件转化为pdf文件。注意:在执行项目前将openOfficce服务启动,启动方法:用cmd命令进入到openOffice安装路径下,执行命令:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard,执行后的效果如图:
在office2pdf方法执行完毕,返回一个生成的pdf的路径,作为pdf转html的入参。具体转化方法参考:
public String pdf2html(String pdfFilePath) throws InterruptedException{
if(!pdfFilePath.equals("")||pdfFilePath!=null){
String filePath = pdfFilePath.substring(0,pdfFilePath.lastIndexOf("\\"));
File htmlFile = new File(filePath+".html