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");      }

今天比较忙,就不贴结果图了。。。