目前发现两种方法:
1、使用openoffice将office转换为pdf文档,使用swfTools将pdf文档转换为swf文件,通过flexpaper插件展示;
2、webOffice插件,直接展示。
在这里先说下第一种:
第一 office--->pdf
下载 openoffice 地址: https://share.weiyun.com/e78f8f6518e3a5534044b210102daebd 密码: sM0YCz
然后在cmd环境下进入安装目录的program目录,输入打开openoffice的命令:
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
输入完成之后在任务管理器可以看见soffice.bin的进程在任务管理器,这一服务就启动成功。当然在代码中转换office2pdf我们还需要一些架包。
下载jodconverter-2.2.2架包,然后复制到lib目录下,引入架包就可以了。这个架包有如下包:
如果报错 The type com.sun.star.lang.XEventListener cannot be resolved. It is indirectly referenced from required异常:
解决办法:到官方网站下载jodconverter-2.2.2.zip,然后把lib文件夹下得所有jar包都拷进去,就可以了。
packagecom.util;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.net.ConnectException;importcom.artofsolving.jodconverter.DocumentConverter;importcom.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;importcom.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;importcom.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;importcom.winchannel.pluginclient.util.PropertiersUtil;/***@authorxx
* @time xx*/
public classConvertPdf {/***@paramfilePath 上传文件所在文件夹的绝对路径
*@paramfileName 文件名称
*@return生成pdf路径\文件名*/
publicString beginConvert(String filePath, String fileName,String fastName) {final String DOC = ".doc";final String DOCX = ".docx";final String XLS = ".xls";final String XLSX = ".xlsx";final String PDF = ".pdf";final String PPT=".ppt";final String PPTX=".pptx";final String TXT=".txt";
String fileExt= "";if (null != fileName && fileName.indexOf(".") > 0) {int index = fileName.lastIndexOf(".");
fileExt=fileName.substring(index).toLowerCase();
}
String inputFile= filePath + File.separator +fileName;
String outputFile= "";int i = 0;//如果是office文档,先转为pdf文件
if (fileExt.equals(DOC) || fileExt.equals(DOCX) ||fileExt.equals(XLS)|| fileExt.equals(XLSX)||fileExt.equals(PPT)||fileExt.equals(PPTX)){
outputFile= filePath + File.separator + fastName +PDF;
System.out.println("====源文件!===="+inputFile);
System.out.println("====转换后文件!===="+outputFile);
i=office2PDF(inputFile, outputFile);
}if(fileExt.equals(TXT)||fileExt.equals(".java")){
File input=newFile(inputFile);
ChangeFileCode changeFileCode= newChangeFileCode();
String fileCode=changeFileCode.getFileEnCode(inputFile);if(fileCode!=null && !"".equals(fileCode)) {
changeFileCode.setFileIn(input.getPath(), fileCode);//如果文件编码为ANSI用GBK来读,如果是UTF-8用UTF-8来读
changeFileCode.setFileOut(input.getPath(), "UTF-8");//UTF-8则文件编码为UTF-8, 如果为GBK,编码为ANSI
changeFileCode.start();
}
inputFile=filePath + File.separator + fastName + ".odt";
input.renameTo(newFile(inputFile));
outputFile=filePath + File.separator + fastName +PDF;
System.out.println("====源文件!===="+inputFile);
System.out.println("====转换后文件!===="+outputFile);
i=office2PDF(inputFile, outputFile);
}if(fileExt.equals(PDF)){//如果是pdf不需要转换 复制改名
outputFile = filePath + File.separator + fastName +PDF;
i=copy(new File(inputFile),newFile(outputFile));
}if(i==0){returnoutputFile;
}else{return "0";
}
}/*** 复制文件*/
private intcopy(File fromFile, File toFile){try{if (!fromFile.exists()){
System.out.println("====来源文件为空!====");
}if (!toFile.exists()){
System.out.println("====创建新文件!====");
toFile.createNewFile();
}
FileInputStream fis= newFileInputStream(fromFile);
System.out.println("fromFile :" +fromFile.getAbsolutePath());
FileOutputStream fos= newFileOutputStream(toFile);
System.out.println("toFile :" +toFile.getAbsolutePath());int len = 0;byte[] buf = new byte[1024];while((len = fis.read(buf)) != -1){
fos.write(buf,0,len);
}
fis.close();
fos.close();
}catch(Exception e) {
System.out.println("====文件复制失败====");return -1;
}return 0;
}/*** office文档转pdf文件
*@paramsourceFile office文档绝对路径
*@paramdestFile pdf文件绝对路径
*@return
*/
public intoffice2PDF(String sourceFile, String destFile) {
String OpenOffice_HOME= PropertiersUtil.getValue("base", "oo_home");
String host_Str=PropertiersUtil.getValue("base", "oo_host");
String port_Str= PropertiersUtil.getValue("base", "oo_port");
OpenOfficeConnection connection=null;
Process pro=null;try{
File inputFile= newFile(sourceFile);if (!inputFile.exists()) {
System.out.println("====找不到源文件===="+sourceFile);return -1; //找不到源文件
}//如果目标路径不存在, 则新建该路径
File outputFile = newFile(destFile);if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}//windows//启动OpenOffice的服务
String command =OpenOffice_HOME+ "\\program\\soffice.exe -headless -accept=\"socket,host="
+ host_Str + ",port=" + port_Str + ";urp;\"";//linux
/*String command = openOffice_HOME
+ "/program/soffice --headless --accept=\"socket,host="
+ host_Str + ",port=" + port_Str + ";urp;\" --nofirststartwizard &";*/System.out.println("====command===="+command);
pro=Runtime.getRuntime().exec(command);//连接openoffice服务
connection = newSocketOpenOfficeConnection(host_Str, Integer.parseInt(port_Str));
connection.connect();//转换
DocumentConverter converter = newOpenOfficeDocumentConverter(connection);try{
converter.convert(inputFile, outputFile);
}catch(Exception e) {if(connection!=null){
connection.disconnect();
}if(pro!=null){
pro.destroy();
}
System.out.println("====转换异常!====");
}//关闭连接和服务
connection.disconnect();
pro.destroy();return 0;
}catch(FileNotFoundException e) {
System.out.println("====文件未找到====");return -1;
}catch(ConnectException e) {
System.out.println("====OpenOffice服务监听异常!====");
}catch(IOException e) {
System.out.println("====IO异常!====");
}return 1;
}public static void main(String[] args) throwsIOException {
ConvertPdf c=newConvertPdf();
String filePath="E:\\";
String fileName="ddd.docx";
System.out.println(">>>"+c.beginConvert(filePath, fileName,"1"));
}
}
第二 pdf--->swf
下载 swftools 地址: https://share.weiyun.com/653b5ad04d9132347c3a72af9e5da6d1 密码: 77NIZe
packagecom.util;importjava.io.BufferedInputStream;importjava.io.IOException;importjava.io.InputStream;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;/*** 将pdf文件转成swf格式*/
public classConvertSwf {private final Logger log = LoggerFactory.getLogger(ConvertSwf.class);/*** 将pdf文件转换成swf文件
*@paramsourceFile pdf文件绝对路径
*@paramoutFile swf文件绝对路径
*@paramtoolFile 转换工具绝对路径*/
publicString convertPdf2Swf(String sourceFile, String outFile) {//linux//String TOOL = "pdf2swf";//windows
String TOOL = "D:\\Program Files (x86)\\SWFTools\\pdf2swf";
String command= TOOL +" "+ sourceFile + " -o " +outFile+ " -s flashversion=9 "+ " -p 0-100"+" -s languagedir=/opt/xpdf-chinese-simplified/ ";try{
Process process=Runtime.getRuntime().exec(command);
log.info("===="+loadStream(process.getInputStream())+"====");
log.info("===="+loadStream(process.getErrorStream())+"====");
log.info("====转换成功swf====");
}catch(Exception e) {
e.printStackTrace();
}return "";
}static String loadStream(InputStream in) throwsIOException{int ptr = 0;
in= newBufferedInputStream(in);
StringBuffer buffer= newStringBuffer();while ((ptr=in.read())!= -1){
buffer.append((char)ptr);
}returnbuffer.toString();
}public static voidmain(String[] args) {
ConvertSwf c= newConvertSwf();
String filePath="E:\\";
String fileName="1.pdf";
c.convertPdf2Swf(filePath+fileName,filePath+"12.swf");
}
}
第三 展示swf
下载 flexpaper 地址:https://share.weiyun.com/298067f3edba681973d89a9487ce78a0 密码:NXwFPi
使用这个插件需要有三个js文件。分别是:jquery.js、flexpaper_flash.js、flexpaper_flash_debug.js。插件的名字是FlexPaperViewer.swf。
html,body{height:100%;
}body{margin:0;padding:0;overflow:auto;
}#flashContent{display:none;
}
在线文档预览varfp=newFlexPaperViewer('FlexPaperViewer','viewerPlaceHolder',
{
config:{
SwfFile:escape('http://localhost:8080/TestBaiDu/12.swf'),
Scale:1.2,
ZoomTransition:'easeOut',
ZoomTime:0.5,
ZoomInterval:0.2,
FitPageOnLoad:false,
FitWidthOnload:false,
FullScreenAsMaxWindow:false,
ProgressiveLoading:false,
MinZoomSize:0.2,
MaxZoomSize:5,
SearchMatchAll:false,
InitViewMode:'SinglePage',
RenderingOrder :'flash',
ViewModeToolsVisible:true,
ZoomToolsVisible:true,
NavToolsVisible:true,
CursorToolsVisible:true,
SearchToolsVisible:true,
localeChain:'en_US'}
});