在线预览ppt doc

打造自己在在线文档浏览

Apache POI

对在线文档浏览的误会,刚开始我选择了POI项目
Apache Poi下载地址:http://poi.apache.org/download.html
我使用的版本是稳定版poi-bin-3.14.zip

ApachePoi组件

  • POIFS(较差混淆技术实现文件系统):此组件是所有其他POI元件的基本因素。它被用来明确地读取不同的文件。
  • HSSF(可怕的电子表格格式):它被用来读取和写入MS-Excel文件的xls格式。
  • XSSF(XML格式):它是用于MS-Excel中XLSX文件格式。
  • HPSF (可怕的属性设置格式):它用来提取MS-Office文件属性设置。
  • HWPF(可怕的字处理格式) :它是用来读取和写入MS-Word的文档扩展名的文件。
  • XWPF(XML字处理格式):它是用来读取和写入MS-Word的docx扩展名的文件。
  • HSLF(可怕的幻灯片版式格式):它是用于读取、创建和编辑PowerPoint演示文稿。
  • HDGF(可怕的图表格式):它包含类和方法为MS-Visio的二进制文件。
  • HPBF(可怕的出版商格式):它被用来读取和写人MS-Publisher文件。

Open Office

做浏览,其实是需要另一个OpenOffice的项目。
步骤:

  • 先将doc、ppt、xls转换成pdf
  • 再用js调用pdf在线查看
  • 主要借助的工具openoffice、jodconvert、viewjs

安装openoffice

http://www.openoffice.org/zh-cn/download/

JODConvert简介

JODConvert,是一个Java的OpenDocument文件转换器,可以进行许多文件格式的转换。
它依赖于OpenOffice提供的服务来进行转换,它能将MS Office文档转换为PDF格式。


可以将JODConverter内嵌在Java应用程序里,也可以单独作为命令行由脚本调用,更可以应用为网页程序或者WebService以供网络应用。

1
2
3
4
5
6
7
8
OfficeManager officeManager = new DefaultOfficeManagerConfiguration( ).buildOfficeManager( );
//启动OpenOffice服务
officeManager.start( );
//执行转换
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
converter.convert(new File(“test.odt”), new File(“test.pdf”));
//停止服务
officeManager.stop( );

OfficeManager是一个接口,主要定义了三个方法:

  • public void start() 启动OpenOffice服务
  • public void stop() 停止OpenOffice服务
  • public void execute(OfficeTask task) 执行转换任务
    DefaultOfficeManagerConfiguration是一个实现了OfficeManager接口的实体类,其提供了相关方法配置OpenOffice.org,比如:
    1
    2
    3
    public DefaultOfficeManagerConfiguration setOfficeHome(String officeHome) //设置OpenOffice.org安装目录
    public DefaultOfficeManagerConfiguration setConnectionProtocol(OfficeConnectionProtocol conn) //设置连接协议,确定使用管道通信,还是socket通信
    public DefaultOfficeManagerConfiguration setTemplateProfileDir(File templateProfileDir)//设置临时目录

可以查看JODConvert API手册查看其它配置。
配置完之后,必须要执行方法buildOfficeManager(),实现真正的配置。


OfficeDocumentConverter中主要包含convert方法,该方法实际上调用的是实现OfficeManager接口的类中的execute方法。

算法设计

Mark-Down

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.converter.pdfConverter;

import java.io.File;
import java.io.FileNotFoundException;

import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;

import com.converter.utils.FileUtils;

public class OpenOfficePDFConverter implements PDFConverter{

private static OfficeManager officeManager;
private static String OFFICE_HOME = "D:\\Program Files\\OpenOffice.org 3";
private static int port[] = {8100};



public void convert2PDF(String inputFile, String pdfFile) {

if(inputFile.endsWith(".txt")){
String odtFile = FileUtils.getFilePrefix(inputFile)+".odt";
if(new File(odtFile).exists()){
System.out.println("odt文件已存在!");
inputFile = odtFile;
}else{
try {
FileUtils.copyFile(inputFile,odtFile);
inputFile = odtFile;
} catch (FileNotFoundException e) {
System.out.println("文档不存在!");
e.printStackTrace();
}
}
}

startService();
System.out.println("进行文档转换转换:" + inputFile + " --> " + pdfFile);
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
converter.convert(new File(inputFile),new File(pdfFile));
stopService();
System.out.println();
}


public void convert2PDF(String inputFile) {
String pdfFile = FileUtils.getFilePrefix(inputFile)+".pdf";
convert2PDF(inputFile,pdfFile);

}

public static void startService(){
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
try {
System.out.println("准备启动服务....");
configuration.setOfficeHome(OFFICE_HOME);//设置OpenOffice.org安装目录
configuration.setPortNumbers(port); //设置转换端口,默认为8100
configuration.setTaskExecutionTimeout(1000 * 60 * 5L);//设置任务执行超时为5分钟
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);//设置任务队列超时为24小时

officeManager = configuration.buildOfficeManager();
officeManager.start(); //启动服务
System.out.println("office转换服务启动成功!");
} catch (Exception ce) {
System.out.println("office转换服务启动失败!详细信息:" + ce);
}
}

public static void stopService(){
System.out.println("关闭office转换服务....");
if (officeManager != null) {
officeManager.stop();
}
System.out.println("关闭office转换成功!");
}
}

渲染

下载viewerjs
http://viewerjs.org/releases/viewerjs-0.5.7.zip
解压,将ViewerJS下的所有内容放置项目webapp下,
在同级目录下放置一个pdf(test.pdf),
如项目名是xxx
在线预览该pdf为http://localhost:8080/xxx/test.pdf