利用OpenOffice和FlexPaper创建类百度文库的预览

思路:
1.利用OpenOffice将文档型文件(doc,docx,xls,xlsx)转为化pdf文件;
2.利用SWFTools将pdf文件转为swf;
3.利用FlexPaper播放swf.

一、利用OpenOffice将文档型文件(doc,docx,xls,xlsx)转为化pdf文件
    安装OpenOffice,并启动服务
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" –nofirststartwizard

执行telnet 127.0.0.1 8100,如果出现乱码则表示启动成功.
PS:当启动OpenOffice后,无法监听到8100端口时,请将-accpet里的参数host改为0.0.0.0尝试.
    转化代码:
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
			try {
				connection.connect();
			} catch(Exception e) {
				e.printStackTrace();
				request.setAttribute("errorMsg", "没有安装OpenOffice,不能预览!");
				return;
			}
			DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
			converter.convert(tempFile, outputFile);
			connection.disconnect();

注意:tempFile是以doc,docx,xls或xlsx为文件后缀名的文件,outputFile为以pdf后缀的文件.
    现在,就把pdf文件准备好了。
二、利用SWFTools将pdf文件转为swf
   
public static int convertPDF2SWF(String pdfName, String swfName) throws IOException {
		//创建swf文件
		File dest = new File(swfName);
		//if (!dest.exists()) dest.mkdirs();

		//源文件不存在则返回
		File source = new File(pdfName);
		if(!source.exists())
			return 0;

		//调用pdf2swf命令进行转换
		//			String command = "pdf2swf.exe" + " -o \"" + destPath + "\\" + fileName + "\"  <span style="color: rgb(255, 0, 0);">-s languagedir=D:\\xpdf\\xpdf-chinese-simplified</span> -s flashversion=9 \"" + sourcePath + "\"";
		Map<String, String> envMap = System.getenv();
		Object home = envMap.get("PDF2SWF_HOME");
		String path = null;
		if(home != null) {
			path = home.toString();
		}
		String command = path + File.separator + "pdf2swf.exe " + pdfName + " -o " + swfName + " " + "-T 9 -f";
		Process pro = Runtime.getRuntime().exec(command);

		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(pro.getInputStream()));
		while(bufferedReader.readLine() != null)
			;

		try {
			pro.waitFor();
		} catch(InterruptedException e) {
			e.printStackTrace();
		}

		return pro.exitValue();

	}
    

注意,这儿使用了一个系统变量PDF2SWF_HOME,在windows下,安装好SWFTools后,做如下配置:“我的电脑”-右键“属性”-“高级”-“环境变量”,在“系统变量”点击“新建”,“变量名”为“PDF2SWF_HOME”,变量值为swftools的安装文件夹,如“D:\Program Files (x86)\SWFTools”,重新打开cmd窗口。
    现在,swf文件也准备好了
三、利用FlexPaper播放swf
  
<body>
<a id="viewerPlaceHolder" style="width:920px;height:700px;display:block"></a>
	        
	        <script type="text/javascript"> 
				var fp = new FlexPaperViewer(	
						 '<%=request.getContextPath()%>/swf/FlexPaperViewer',
						 'viewerPlaceHolder', { config : {
						 SwfFile : '<%=request.getContextPath()%>/htmlTemp/${fileName}.swf',
						 Scale : 0.6, 
						 ZoomTransition : 'easeOut',
						 ZoomTime : 0.5,
						 ZoomInterval : 0.2,
						 FitPageOnLoad : true,
						 FitWidthOnLoad : true,
						 ProgressiveLoading : false,
						 MinZoomSize : 0.2,
						 MaxZoomSize : 5,
						 SearchMatchAll : false,
						 InitViewMode : 'SinglePage',
						 
						 ViewModeToolsVisible : true,
						 ZoomToolsVisible : true,
						 NavToolsVisible : true,
						 CursorToolsVisible : true,
						 SearchToolsVisible : true,
  						 PrintEnable:false,	
  						 localeChain: 'en_US'
						 }});
	        </script>
</body>
   


    最后,完成了.

    小结:利用OpenOffice可以将文档型的文件转为了HTML、PDF等格式,但是,在转HTML时,效果不是很理想,主要是格式的问题.如果需要实现类似于163邮箱或QQ邮箱文档预览的功能也是可以的.
    FlexPaper基本版是免费的,但有收费的版本,如果是企业级的应用,请注意.不过做个类似的东西也不难.
    OpenOffice和SWFTOOL都是支持多平台的,无论windows或linux,都适用.

相关包:
    引入jodconverter-2.2.2包,并引用相关其他包。

猜你喜欢

转载自wxfiteye.iteye.com/blog/1688421
今日推荐