ppt转html java_poi ppt转换为html,实现在线预览

该博客介绍了如何使用Apache POI库将PPT(PPTX)转换为HTML,通过将每张幻灯片导出为图片并插入到HTML中,实现了PPT的在线预览功能。
摘要由CSDN通过智能技术生成

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

+%20");

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

+%20");

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

}*/

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值