JAVA-WEB(通过POI)实现PPT在线预览

思路:

获取网络PPT 文件 将PPT每一页的幻灯片都转换成单张图片 用流输出到页面展示

1, 首先获取PPT格式 PPT格式无非两种 PPTX,PPT

2,获取PPT幻灯片页数传递到JSP中

3,JSP循环获取每页PPT

代码:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xslf.usermodel.XSLFTextShape;
import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextFont;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide;

import com.yyt.yunbao.common.vo.PptImageResult;

/**

  • @author Xu Yuanyuan

  • @version 1.0

  • @date 2017年4月25日 上午11:11:07

  • @desc PPT转换为图片
    */
    public class PPTToImageUtil {

    public static void main(String[] args) {
    String url = "http://www.xxx.com/xxx.pptx";//支持ppt和pptx
    try {
    ByteArrayInputStream pptInput = Utility.readNetFile(url);
    PptImageResult result = PPTToImageUtil.convert(".pptx", pptInput);
    System.out.println(result.getTotalImage());
    System.out.println(result.getImageByteList().size());
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    public static PptImageResult convert(String suffix, ByteArrayInputStream pptInput) {
    if (pptInput != null) {
    if (suffix.endsWith(".pptx")) {
    return PPTToImageUtil.ppt2007Img(pptInput);
    } else if (suffix.endsWith(".ppt")) {
    return PPTToImageUtil.ppt2003Img(pptInput);
    }
    }
    return null;
    }

    /**

    • 设置PPTX字体

    • @param slide
      */
      private static void setFont(XSLFSlide slide) {
      for (XSLFShape shape : slide.getShapes()) {
      if (shape instanceof XSLFTextShape) {
      XSLFTextShape txtshape = (XSLFTextShape) shape;
      for (XSLFTextParagraph paragraph : txtshape.getTextParagraphs()) {
      List truns = paragraph.getTextRuns();
      for (XSLFTextRun trun : truns) {
      trun.setFontFamily("宋体");
      double currentFontSize = trun.getFontSize();
      if((currentFontSize <=0)||(currentFontSize >=26040)){
      trun.setFontSize(30);
      }
      }
      }
      }

      }
      }

    /**

    • 转换为2007版的PPT到图片流
    • @param bais
    • @return
      */
      public static PptImageResult ppt2007Img(ByteArrayInputStream bais) {
      PptImageResult result = new PptImageResult();
      XMLSlideShow ppt = null;
      try {
      ppt = new XMLSlideShow(bais);
      Dimension pgsize = ppt.getPageSize();
      XSLFSlide[] slides = ppt.getSlides();
      result.setTotalImage(slides.length);
      for (int i = 0; i < slides.length; i++) {
      // 设置字体为宋体,解决中文乱码问题
      CTSlide xmlObject = slides[i].getXmlObject();
      setFont(slides[i]);
      CTGroupShape spTree = xmlObject.getCSld().getSpTree();
      CTShape[] spArray = spTree.getSpArray();
      for (CTShape shape : spArray) {
      CTTextBody txBody = shape.getTxBody();
      if (txBody == null) {
      continue;
      }
      CTTextParagraph[] pArray = txBody.getPArray();
      CTTextFont font = CTTextFont.Factory
      .parse(""
      + " "
      + " " + "
      " + "
      ");
      for (CTTextParagraph textParagraph : pArray) {
      CTRegularTextRun[] textRuns = textParagraph.getRArray();
      for (CTRegularTextRun textRun : textRuns) {
      CTTextCharacterProperties properties = textRun.getRPr();
      properties.setLatin(font);
      }
      }
      }
      BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
      Graphics2D graphics = img.createGraphics();
      graphics.setPaint(Color.white);
      graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
      slides[i].draw(graphics);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ImageIO.write(img, "png", baos);
      baos.flush();
      baos.close();
      result.getImageByteList().add(baos.toByteArray());
      }
      } catch (Exception e) {
      e.printStackTrace();
      } finally {
      if (bais != null) {
      try {
      bais.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      }
      return result;
      }

    /**

    • 设置字体防止乱码
    • @param slide
      */
      private static void set2003Font(Slide slide) {
      TextRun[] truns = slide.getTextRuns();
      if(Utility.isNotEmpty(truns)){
      for (TextRun trun: truns) {
      RichTextRun[] rtruns = trun.getRichTextRuns();
      if(Utility.isNotEmpty(rtruns)){
      for (RichTextRun rtrun : rtruns) {
      rtrun.setFontIndex(1);
      rtrun.setFontName("宋体");
      }
      }
      }
      }
      }

    /**

    • 转换为2003版的PPT到图片流
    • @param bais
    • @return
      */
      public static PptImageResult ppt2003Img(ByteArrayInputStream bais) {
      PptImageResult result = new PptImageResult();
      try {
      SlideShow ppt = new SlideShow(new HSLFSlideShow(bais));
      Dimension pgsize = ppt.getPageSize();
      Slide[] slides = ppt.getSlides();
      result.setTotalImage(slides.length);
      for (int i = 0; i < slides.length; i++) {
      set2003Font(slides[i]);
      BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
      Graphics2D graphics = img.createGraphics();
      graphics.setPaint(Color.white);
      graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
      slides[i].draw(graphics);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ImageIO.write(img, "png", baos);
      baos.flush();
      baos.close();
      result.getImageByteList().add(baos.toByteArray());
      }
      } catch (Exception e) {
      e.printStackTrace();
      } finally {
      if (bais != null) {
      try {
      bais.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      }
      return result;
      }
      }

你可能感兴趣的:(JAVA-WEB(通过POI)实现PPT在线预览)