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("

");

sb.append("

");

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("

");

sb.append("

");

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

}*/

}

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

  1. java将office文档,word,ppt,pdf文档转换成swf文件在线预览

    java将office文档pdf文档转换成swf文件在线预览 第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux ...

  2. 使用libreoffice将office文档(word、ppt、excel)转pdf,实现在线预览

    项目需要实现局域网预览office文档的功能,之前做的在线项目,都是将文档上传到cdn,利用cdn自带的转码功能,把文档转换为pdf,然后再用pdf.js实现在线预览. 因为是局域网,没有办法上传到c ...

  3. PPT在线预览 转换为图片实现方案 Apache POI 实现时踩坑:含嵌入文件ppt转换报错 ArrayStoreException

    前言 背景:最近项目需要实现PPT预览功能,以及项目APP上浏览ppt,初步方案是ppt转为图片. 实现 1.ppt转为pdf,然后pdf转为图片 该种实现,先将ppt转为pdf文件,实现方式有很多, ...

  4. word、excel、ppt 办公文件 在线预览

    如果想要免费的,可以用 openoffice,实现原理就是: 通过第三方工具openoffice,将word.excel.ppt.txt等文件转换为pdf文件流:当然如果装了Adobe Reader ...

  5. 快速实现word、excel、ppt、txt等办公文件在线预览功能(Java版)

    点击关注公众号,实用技术文章及时了解 来源:blog.csdn.net/weixin_40986713/ article/details/109527294 java实现办公文件在线预览功能是一个大家 ...

  6. office文档 在线预览 (doc、ppt、xls)

    office 在线预览  调用微软的api,将office文档转换为html,然后实现预览. 参考我们的实现方式:http://demo.kalcaddle.com/?user/loginSubmit ...

  7. Java 实现word、excel、ppt、txt等办公文件在线预览功能!

    大家好,我是宝哥! 如何用 Java 实现word.excel.ppt.txt等办公文件在线预览功能?本文告诉你答案! java 实现办公文件在线预览功能是一个大家在工作中也许会遇到的需求,网上些公司 ...

  8. java 使用poi将PPT转为图片,在线预览PPT

    在线预览PPT,我的思路为java 使用poi将PPT转为图片,图片存储到指定的文件夹下面,然后返回生成的图片名称集合,将图片路径遍历到前台的html标签上,用HTML前台模仿画一个PPT框架,操作图 ...

  9. 手把手教你用 Java 实现word、excel、ppt、txt等办公文件在线预览功能!

    如何用 Java 实现word.excel.ppt.txt等办公文件在线预览功能?本文告诉你答案! java 实现办公文件在线预览功能是一个大家在工作中也许会遇到的需求,网上些公司专门提供这样的服务, ...

  10. Java通过openOffice实现word,excel,ppt转成pdf实现在线预览

    Java通过openOffice实现word,excel,ppt转成pdf实现在线预览 一.OpenOffice 1.1 下载地址 1.2 JodConverter 1.3 新建实体类PDFDemo ...

最新文章

  1. linux c 延迟函数 sleep usleep 使用区别
  2. Citus中的分片策略:Append Distribution追加分配
  3. (一)Builder(建造者)模式
  4. [Spring5]IOC容器_底层原理
  5. docker 使用技巧
  6. powerdesigner辅助导入导出excel文件
  7. [BZOJ4817]树点涂色
  8. Mac typora自定义编辑界面的配置
  9. C++ set简介及简单应用
  10. jquery 多个class操作
  11. 【起航计划 027】2015 起航计划 Android APIDemo的魔鬼步伐 26 App-Preferences-Preferences from XML 偏好设置界面...
  12. 黑马程序员Python教程的代码--植物大战僵尸游戏代码
  13. 《认知盈余》核心摘要——“人们实际上很喜欢创造并分享”: 参与是一种行为
  14. 21.通用型1602液晶显示屏操作方法
  15. VMware中的虚拟机开启VT,支持KVM
  16. 记一次安装 ubuntu 18.04 双系统 (双硬盘)
  17. [NIPS2017]Attention is all you need
  18. 圆和圆柱体计算(继承)Python
  19. Contention
  20. Flink实战之实时风控规则引擎

热门文章

  1. HCNA-Telnet
  2. flume+kafka+storm整合02---问题
  3. 应试教育堵死了孩子们犯错的道路
  4. 【MYSQL】DQL
  5. java自动违例设计,java违例
  6. 看这一篇就够了:写简历、面试、谈薪酬的技巧和防坑指南
  7. HTTPS的工作原理
  8. Debian 9.x 系统安装 Proxmox VE (笔记)
  9. cmd设置总是置顶_windows 下如何让一个窗口置顶?
  10. 史密斯数(C++实现)