java实现在线预览---poi操作ppt转html及03、07版本兼容问题
来源:互联网 发布:淘宝什么数据库 编辑:程序博客网 时间:2024/05/21 15:41
poi操作ppt转html
由于ppt都是由一页一页图片组成,所以ppt要先转成图片在放入html页面
/** * ppt03转html * filename:要读取的文件所在文件夹 * filepath:文件名 * htmlname:生成html名称 * path:html存放路径 * imgpath:图片存放路径 * */ public static void doPPTtoImage(HttpServletRequest request,String filepath,String sourceid) throws TransformerException { String htmlname="exportPpt"+sourceid+".html"; String path =request.getSession().getServletContext().getRealPath("/view/ppt"); fileExists(path); String imgpath =request.getSession().getServletContext().getRealPath("/view/ppt/images"); fileExists(imgpath); String filename=request.getSession().getServletContext().getRealPath("/vod/mp4"); // 读入PPT文件 File file = new File(filename+"/"+filepath); boolean isppt = checkFile(file); if (!isppt) { System.out.println("The image you specify don't exit!"); } try { FileInputStream is = new FileInputStream(file); SlideShow ppt = new SlideShow(is); is.close(); Dimension pgsize = ppt.getPageSize(); org.apache.poi.hslf.model.Slide[] slide = ppt.getSlides(); FileOutputStream out =null; String imghtml=""; for (int i = 0; i < slide.length; i++) { System.out.print("第" + i + "页。"); TextRun[] truns = slide[i].getTextRuns(); for ( int k=0;k<truns.length;k++){ RichTextRun[] rtruns = truns[k].getRichTextRuns(); for(int l=0;l<rtruns.length;l++){ rtruns[l].setFontIndex(1); rtruns[l].setFontName("宋体"); } } BufferedImage img = new BufferedImage(pgsize.width,pgsize.height, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = img.createGraphics(); graphics.setPaint(Color.BLUE); graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height)); slide[i].draw(graphics); // 这里设置图片的存放路径和图片的格式(jpeg,png,bmp等等),注意生成文件路径 out= new FileOutputStream(imgpath+"/" +(i + 1) + ".jpeg"); javax.imageio.ImageIO.write(img, "jpeg", out); //图片在html加载路径 String imgs="images/"+(i + 1) + ".jpeg"; imghtml+="<img src=\'"+imgs+"\' style=\'width:1200px;height:830px;vertical-align:text-bottom;\'><br><br><br><br>"; } DOMSource domSource = new DOMSource(); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.METHOD, "html"); serializer.transform(domSource, streamResult); out.close(); String ppthtml="<html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>"+imghtml+"</body></html>"; FileUtils.writeStringToFile(new File(path, htmlname), ppthtml, "utf-8"); } catch (FileNotFoundException e) { System.out.println(e); // System.out.println("Can't find the image!"); } catch (IOException e) { } } // function 检查文件是否为PPT public static boolean checkFile(File file) { boolean isppt = false; String filename = file.getName(); String suffixname = null; if (filename != null && filename.indexOf(".") != -1) { suffixname = filename.substring(filename.indexOf(".")); if (suffixname.equals(".ppt")||suffixname.equals(".pptx")) { isppt = true; } return isppt; } else { return isppt; } } /** * ppt07转html * filename:要读取的文件所在文件夹 * filepath:文件名 * htmlname:生成html名称 * path:html存放路径 * imgpath:图片存放路径 * */ public static void pptTohtml07(HttpServletRequest request,String filepath,String sourceid) throws IOException, TransformerException{ String htmlname="exportPpt"+sourceid+".html"; String path=request.getSession().getServletContext().getRealPath("/view/ppt"); fileExists(path); String imgpath =request.getSession().getServletContext().getRealPath("/view/ppt/images"); fileExists(imgpath); String filename=request.getSession().getServletContext().getRealPath("/vod/mp4"); File file = new File(filename+"/"+filepath); boolean isppt = checkFile(file); if (!isppt) { System.out.println("The image you specify don't exit!"); } FileInputStream is = new FileInputStream(file); XMLSlideShow ppt = new XMLSlideShow(is); is.close(); Dimension pgsize = ppt.getPageSize(); System.out.println(pgsize.width+"--"+pgsize.height); XSLFSlide[] pptPageXSLFSLiseList=ppt.getSlides(); FileOutputStream out=null; String imghtml=""; for (int i = 0; i < pptPageXSLFSLiseList.length; i++) { try { for(XSLFShape shape : pptPageXSLFSLiseList[i].getShapes()){ if(shape instanceof XSLFTextShape) { XSLFTextShape tsh = (XSLFTextShape)shape; for(XSLFTextParagraph p : tsh){ for(XSLFTextRun r : p){ r.setFontFamily("宋体"); } } } } BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = img.createGraphics(); // clear the drawing area graphics.setPaint(Color.white); graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height)); // render pptPageXSLFSLiseList[i].draw(graphics); // String Imgname = imgpath +"/"+ (i+1) + ".jpg"; out = new FileOutputStream(Imgname); javax.imageio.ImageIO.write(img, "jpg", out); //图片在html加载路径 String imgs="images/"+(i + 1) + ".jpg"; imghtml+="<img src=\'"+imgs+"\' style=\'width:1200px;height:830px;vertical-align:text-bottom;\'><br><br><br><br>"; } catch (Exception e) { System.out.println(e); System.out.println("第"+i+"张ppt转换出错"); } } System.out.println("7success"); DOMSource domSource = new DOMSource(); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.METHOD, "html"); serializer.transform(domSource, streamResult); out.close(); String ppthtml="<html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>"+imghtml+"</body></html>"; FileUtils.writeStringToFile(new File(path, htmlname), ppthtml, "utf-8"); }
今天比较忙,就不贴结果图了。。。
阅读全文
0 0
- java实现在线预览---poi操作ppt转html及03、07版本兼容问题
- java实现在线预览----poi操作word转html及03、07版本兼容问题
- java实现在线预览----poi操作excel转html及03、07版本兼容问题
- java实现在线预览--poi实现word、excel、ppt转html
- poi PPT 在线预览
- java 使用poi将PPT转为图片,在线预览PPT
- apache poi操作office文档----java在线预览txt、word、ppt、execel,pdf代码
- .net 实现Office文件预览,word文件在线预览、excel文件在线预览、ppt文件在线预览,excel转html,office格式转换,
- java poi- 实现 word Excel pdf ppt 转 HTML
- java poi ppt操作示例
- Java 利用POI操作PPT
- POI 附件在线预览
- asp.net实现word、excel、ppt、pdf在线预览
- Java实现在线预览office
- java文档在线预览实现
- Java实现Office在线预览
- POI操作EXCEL03前后版本不兼容问题解决
- Office在线预览及PDF在线预览的实现方式
- iOS 转让APP,变更开发商名字 —— HERO博客
- Android多线程
- 802.11无线网络学习(三):802.11 MAC基础
- Android之MappedByteBuffer缓冲用法
- 5-3 A-B (20分)
- java实现在线预览---poi操作ppt转html及03、07版本兼容问题
- MAC安装Securecrt破解
- css画图案
- SimPHP框架介绍
- app接口安全性设计浅析
- 第一章
- windows dos操作命令
- Android rgb 转bitmap
- spmi_get_resource