将word、txt等文本格式的文件转成pdf格式显示,并且去掉打印和logo等功能。
1 private File srcFile; //源文件 2 private File pdfFile; //转换成pdf的文件 3 private File swfFile; //转成swf文件 4 private String fileType; //文件类型 5 private final String PDF2SWF_PATH="C:\\Program Files\\SWFTools\\pdf2swf.exe";//pdf2swf 安装路径 6 private final String CONVERTFILETYPE = "jpg,jpeg,font,gif,png,wav,zip,jar,sql,exe,mp3"; // 设置文件类型 7 private OfficeManager officeManager; 8 private final String OFFICE_HOME = "C:\\Program Files\\OpenOffice.org 3"; //OpenOffice.org 3 安装路径 9 private int port[] = {8100}; 10 /** 11 * 将doc文件转换为pdf文件 12 */ 13 public void docpdf() throws Exception { 14 try { 15 if (srcFile.exists()) { 16 if (!swfFile.exists()) { 17 startService(); 18 OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager); 19 converter.convert(srcFile,pdfFile); 20 stopService(); 21 } 22 } 23 } catch (Exception e) { 24 System.out.println("pdf转换异常,openoffice服务未启动!"); 25 throw e; 26 } 27 } 28 // pdf->swf 29 public void pdfswf() throws Exception { 30 try { 31 if (!swfFile.exists()) { 32 if (pdfFile.exists()) { 33 String command = PDF2SWF_PATH +" \""+pdfFile+"\" -o "+swfFile+" -T 9 -f"; 34 try { 35 System.out.println("开始转换文档: "+ pdfFile ); 36 Process p = Runtime.getRuntime().exec(command); 37 new DoOutput(p.getInputStream()).start(); 38 new DoOutput(p.getErrorStream()).start(); 39 // p.destroy(); 40 System.out.println("成功转换为SWF文件!"); 41 } catch (IOException e) { 42 e.printStackTrace(); 43 System.out.println("转换文档为swf文件失败!"); 44 } 45 } 46 }else if (swfFile.exists() && pdfFile.exists()) { 47 if (!"pdf".equals(fileType)) { 48 pdfFile.delete(); 49 } 50 } 51 52 } catch (Exception e) { 53 System.out.println("swf文件失败"); 54 throw e; 55 } 56 } 57 /** 58 * 起动openoffice转换服务 59 * 60 */ 61 public void startService(){ 62 DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration(); 63 try { 64 configuration.setOfficeHome(OFFICE_HOME);//设置OpenOffice.org安装目录 65 configuration.setPortNumbers(port); //设置转换端口,默认为8100 66 configuration.setTaskExecutionTimeout(1000 * 60 * 5L);//设置任务执行超时为5分钟 67 configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);//设置任务队列超时为24小时 68 officeManager = configuration.buildOfficeManager(); 69 officeManager.start(); //启动服务 70 } catch (Exception ce) { 71 System.out.println("office转换服务启动失败!详细信息:" + ce); 72 } 73 } 74 /** 75 * 关闭office转换服务 76 * 77 */ 78 public void stopService(){ 79 if (officeManager != null) { 80 officeManager.stop(); 81 } 82 } 83 84 /** 85 * @author yi 86 * 内部类 87 */ 88 private static class DoOutput extends Thread { 89 public InputStream is; 90 //构造方法 91 public DoOutput(InputStream is) { 92 this.is = is; 93 } 94 public void run() { 95 BufferedReader br = new BufferedReader(new InputStreamReader(this.is)); 96 String str = null; 97 try { 98 //这里并没有对流的内容进行处理,只是读了一遍 99 while ((str = br.readLine()) != null); 100 101 } catch (IOException e) { 102 e.printStackTrace(); 103 } finally 104 { 105 if (br != null) { 106 try { 107 br.close(); 108 } catch (IOException e) { 109 e.printStackTrace(); 110 } 111 } 112 } 113 } 114 }
页面上:
1 <script type="text/javascript"> 2 var fp = new FlexPaperViewer('<%=contextPath%>/FlexPaper/FlexPaperViewer', 'viewerPlaceHolder', { 3 config : { 4 SwfFile: escape('<%=contextPath%>/FlexPaper/swf/<%=session.getAttribute("fileName")%>'), 5 Scale : 0.6, 6 ZoomTransition : 'easeOut', 7 ZoomTime : 0.5, 8 ZoomInterval : 0.2, 9 FitPageOnLoad : true, 10 FitWidthOnLoad : false, 11 FullScreenAsMaxWindow : false, 12 ProgressiveLoading : false, 13 MinZoomSize : 0.2, 14 MaxZoomSize : 5, 15 SearchMatchAll : false, 16 InitViewMode : 'Portrait', 17 PrintPaperAsBitmap : false, 18 PrintEnabled : false, 19 ViewModeToolsVisible : true, 20 ZoomToolsVisible : true, 21 NavToolsVisible : true, 22 CursorToolsVisible : true, 23 SearchToolsVisible : true, 24 25 localeChain: 'zh_CN' 26 } 27 }); 28 </script>
需要安装两个软件:pdf2swf 和 OpenOffice.org 3