ppt转换为html的原理就是将ppt转换为图片

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.poi.hslf.usermodel.HSLFShape;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
import org.apache.poi.hslf.usermodel.HSLFTextParagraph;
import org.apache.poi.hslf.usermodel.HSLFTextRun;
import org.apache.poi.hslf.usermodel.HSLFTextShape;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xslf.usermodel.XSLFTextShape;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.maiyue.base.utils.ComUtil;
import com.maiyue.base.utils.FileUtils;

/**
 * ppt转换为html
 * @author chen
 *
 */
public class POIPptToHtmlUtils {
 
 private static Logger logger = LoggerFactory.getLogger(POIPptToHtmlUtils.class);

/**
  *
  * @param sourceFilePath
  * @param targetFolder
  * @param targetFileName
  * @return
  */
 public static String pptToHtml(String sourceFilePath, String targetFolder, String targetFileName) {
  FileUtils.createFileFolder(targetFolder);
  File pptFile = new File(sourceFilePath);
  if (pptFile.exists()) {
   try {
    String type = FileUtils.getFileSuffix(sourceFilePath);
    String targetFilePath = targetFolder + "/"+ targetFileName;
    if ("ppt".equals(type)) {
     String htmlStr = toImage2003(sourceFilePath, targetFolder);
     return FileUtils.writeToFile(htmlStr, targetFilePath, "UTF-8");
    } else if ("pptx".equals(type)) {
     String htmlStr = toImage2007(sourceFilePath, targetFolder);
     return FileUtils.writeToFile(htmlStr, targetFilePath, "UTF-8");
    } else {
     logger.error("ppt转换为html,源文件={}不是ppt文件", sourceFilePath);
     return null;
    }

} catch (Exception e) {
    logger.error("ppt文档转换为html,发生异常,源文件={},", sourceFilePath, e);
    return null;
   }
  } else {
   logger.error("ppt文档转换为html,源文件={}不存在", sourceFilePath);
   return null;
  }
 }

public static String toImage2007(String sourcePath, String targetDir) throws Exception {
  String htmlStr = "";
  FileInputStream is = new FileInputStream(sourcePath);
  XMLSlideShow ppt = new XMLSlideShow(is);
  is.close();
  FileUtils.createDir(targetDir);
  Dimension pgsize = ppt.getPageSize();
  String imageFileName = "ppt" + ComUtil.genUUID(3);
  StringBuffer sb = new StringBuffer();
  for (int i = 0; i < ppt.getSlides().size(); i++) {
   try {
    for (XSLFShape shape : ppt.getSlides().get(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
    ppt.getSlides().get(i).draw(graphics);
    // save the output
    String imageDir = targetDir + "/" + imageFileName + "/";
    FileUtils.createDir(imageDir);// create image dir
    // 相对路径
    String relativeImagePath = imageFileName + "/" + imageFileName + "-" + (i + 1) + ".png";
    // 绝对路径
    String imagePath = imageDir + imageFileName + "-" + (i + 1) + ".png";
    sb.append("<br>");
    sb.append("<img src=" + "\"" + relativeImagePath + "\"" + "/>");
    FileOutputStream out = new FileOutputStream(imagePath);
    javax.imageio.ImageIO.write(img, "png", out);
    out.close();
   } catch (Exception e) {
    logger.error("ppt转换为html,发生异常,源文件={}", sourcePath, e);
    System.out.println("第" + i + "张ppt转换出错");
    return null;
   }
  }
  htmlStr = sb.toString();
  return htmlStr;
 }

public static String toImage2003(String sourcePath, String targetDir) {
  String htmlStr = "";
  try {
   HSLFSlideShow ppt = new HSLFSlideShow(new HSLFSlideShowImpl(sourcePath));
   FileUtils.createDir(targetDir);
   Dimension pgsize = ppt.getPageSize();
   StringBuffer sb = new StringBuffer();
   String imageFileName = ComUtil.genUUID(5);
   for (int i = 0; i < ppt.getSlides().size(); i++) {
    for (HSLFShape shape : ppt.getSlides().get(i).getShapes()) {
     if (shape instanceof HSLFTextShape) {
      HSLFTextShape tsh = (HSLFTextShape) shape;
      for (HSLFTextParagraph p : tsh) {
       for (HSLFTextRun 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
    ppt.getSlides().get(i).draw(graphics);
    String imageDir = targetDir + "/" + imageFileName + "/";
    // create image dir
    FileUtils.createDir(imageDir);
    // 相对路径
    String relativeImagePath = imageFileName + "/" + imageFileName + "-" + (i + 1) + ".png";
    // 绝对路径
    String imagePath = imageDir + imageFileName + "-" + (i + 1) + ".png";
    sb.append("<br>");
    sb.append("<img src=" + "\"" + relativeImagePath + "\"" + "/>");
    FileOutputStream out = new FileOutputStream(imagePath);
    javax.imageio.ImageIO.write(img, "png", out);
    out.close();
   }
   htmlStr = sb.toString();
  } catch (Exception e) {
   logger.error("ppt转换为html,发生异常,源文件={}", sourcePath, e);
   return null;
  }
  
  return htmlStr;
 }

/**
  *
  * @param srcImgPath
  * @param distImgPath
  * @param width
  * @param height
  * @throws IOException
  */
 public static void resizeImage(String srcImgPath, String distImgPath, int width, int height) throws IOException {
  File srcFile = new File(srcImgPath);
  Image srcImg = ImageIO.read(srcFile);
  BufferedImage buffImg = null;
  buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  buffImg.getGraphics().drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
  ImageIO.write(buffImg, "JPEG", new File(distImgPath));
 }
 
 /*public static void main(String[] args) {
  //POIPptToHtmlUtils.pptToHtml("D:/diagnosis/file/temp//ppt2007.pptx", "D:/diagnosis/file/temp/", "test5.html");
  POIPptToHtmlUtils.pptToHtml("D:/diagnosis/file/temp//ppt2003.ppt", "D:/diagnosis/file/temp/", "test6.html");
 }*/

}

poi ppt转换为html,实现在线预览相关推荐

  1. 使用aspose方式使excel,ppt,word进行在线预览。(无水印)

    使用aspose方式使excel,ppt,word进行在线预览.(无水印) 1.首先,页面需要用jquery中window.open();打开一个新页面. window.open(../fileMan ...

  2. html怎么转换到百度,类似百度文库在线预览文档flash版(支持word、excel、ppt、pdf)+在线预览文档html版...

    类似百度文库在线预览文档flash版(支持word.excel.ppt.pdf)+在线预览文档html版 (1).将文档转换为html,只支持支持office文档 (2).将文档转换为flash,实现 ...

  3. OpenOffice在线预览附件

    OpenOffice在线预览附件 Author xiuhong.chen@hand-china.com Date 2018/1/20 Desc 在线预览附件功能 1.下载安装包 官方下载: http: ...

  4. 实现PPT的在线预览(动态,及转PDF)

    实现PPT的在线预览(动态,及转PDF) 公司的新需求,需要在网页上动态预览PPT,此处记录下,防止忘记. 之前在网上找了很多资料,比如:用POI 实现等,这里写下自己的实现方法 1.PPT 转PDF ...

  5. SpringBoot 使用LibreOffice 在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件

    接上一篇:linux环境源码安装unoconv Linux环境_源码安装Unoconv实现文件在线预览doc,doxc,xls,xlsx,ppt,pptx 文件 https://gblfy.blog. ...

  6. SpringBoot 使用unoconv 在线预览 doc,doxc,xls,xlsx,ppt,pptx 文件

    接上一篇:linux环境源码安装unoconv Linux环境_源码安装Unoconv实现文件在线预览doc,doxc,xls,xlsx,ppt,pptx 文件 https://gblfy.blog. ...

  7. office在线预览哪家强?不能播放ppt动画,不能监听翻页?

    前言 众所周知,word.excel.ppt 和 pdf 文件在线预览有很多解决方案,但大多无法播放 ppt 动画,或者功能非常单一.这不,最近产品经理闭关修炼三天,提出了如下需求: 监听文档翻页,根 ...

  8. 实现word在线预览 有php的写法 也有插件似

    1 <?php2 //header("Content-type:text/html;charset=utf-8");3 //word转html 展示4 $lj=$_GET[' ...

  9. 前端实现文件在线预览

    需求: 一个览pdf.word.xls.ppt等文件需要在线预览功能 介绍 使用 XDOC文档预览云服务文档地址 基于HTTP的REST方式调用,只需要传入URL! 支持pdf.docx.xlsx.p ...

  10. office 文档在线预览新姿势之永中云转换

    你知道的越多,你不知道的越多 点赞再看,养成习惯 如果您有疑问或者见解,或者需要毕业设计,大作业指导,购买付费源码等,欢迎指教: 企鹅:869192208 文章目录 前言: 一.接入准备 二.预览 w ...

最新文章

  1. 重磅图书——PHP MySQL开发新圣经
  2. 干货!一文读懂人工智能和机器学习有什么关系
  3. linux系统下对磁盘的,学会在Linux下对硬盘分区
  4. AVFoundation和 GPUImage初探
  5. linux下多路复用模型之Select模型
  6. 一个简单文本处理问题的多种解法
  7. 小程序tabbar这套方案全搞定!
  8. php 获取key的位置,PHP获取当前所在目录位置的方法
  9. 让媳妇瞬间搞懂Spring 多数据源操作(SpringBoot + Durid)
  10. 20000 字干货笔记,一天搞定 MySQL !
  11. WPS配置工具参数 ksomisc.exe
  12. MATLAB工具箱下载地址总汇
  13. knockoutjs 经验总结
  14. 记一次HDD(机械硬盘)突然出故障,然后数据恢复以及更换HDD的过程
  15. 北辰创业笔记:百度引流推广有用吗?百度引流最有效的方法
  16. JVM cpu过高排查
  17. paddle 图标注_安卓|尺寸标注工具,让标注更加方便
  18. 电梯测试震动软件,保证质量电梯振动分析仪
  19. Computer Vision—计算机视觉 (一)
  20. 光盘刻录系列之二刻录光盘的程序步骤

热门文章

  1. Remove.bg api 自动去背景 实例
  2. 人工智能行业市场分析
  3. 库函数memcpy的实现
  4. geany配置python_Geany中怎么配置python?
  5. 虚拟机内kali走主机代理
  6. 百度未命名算法将执行,打击SEO收割
  7. 高速公路 (highway)
  8. 微型计算机中 存储器容量最大的部件是,2012年计算机一级考试B练习题及答案
  9. 如何配置SQL Server数据库远程连接
  10. IBM MQ 创建以及常见问题集锦