| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- package com.steerinfo.dil.util;
- import org.apache.poi.hslf.usermodel.*;
- import org.apache.poi.xslf.usermodel.*;
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.geom.Rectangle2D;
- import java.awt.image.BufferedImage;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Base64;
- /**
- *
- * @author ruand http://blog.csdn.net/emoven/article/details/52683215
- */
- public class POIPptToHtml {
- private final static String PPT = "ppt";
- private final static String PPTX = "pptx";
- public static String pptToHtml(InputStream is, String type) {
- String htmlStr = "预览失败";
- try {
- if (PPT.equals(type)) {
- htmlStr = toImage2003(is);
- } else if (PPTX.equals(type)) {
- htmlStr = toImage2007(is);
- } else {
- htmlStr = "the file is not a ppt";
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return htmlStr;
- }
- public static String toImage2007(InputStream is) throws Exception {
- String htmlStr = "预览失败";
- XMLSlideShow ppt = new XMLSlideShow(is);
- is.close();
- Dimension pgsize = ppt.getPageSize();
- System.out.println(pgsize.width + "--" + pgsize.height);
- 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);
- ByteArrayOutputStream stream = new ByteArrayOutputStream();
- ImageIO.write(img, "png", stream);
- String imgStr = Base64.getEncoder().encodeToString(stream.toByteArray());
- // save the output
- sb.append("<br>");
- sb.append("<img src=" + "\"data:image/png;base64," + imgStr + "\"" + "/>");
- stream.close();
- } catch (Exception e) {
- System.out.println("第" + i + "张ppt转换出错");
- }
- }
- System.out.println("success");
- htmlStr = sb.toString();
- return htmlStr;
- }
- public static String toImage2003(InputStream is) {
- String htmlStr = "预览失败";
- try {
- HSLFSlideShow ppt = new HSLFSlideShow(is);
- Dimension pgsize = ppt.getPageSize();
- StringBuffer sb = new StringBuffer();
- 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);
- ByteArrayOutputStream stream = new ByteArrayOutputStream();
- //String imageDir = targetDir + "/" + pptFileName + "/";
- //FileUtils.createDir(imageDir);// create image dir
- //String imagePath = imageDir + pptFileName + "-" + (i + 1) + ".png";
- ImageIO.write(img, "png", stream);
- String imgStr = Base64.getEncoder().encodeToString(stream.toByteArray());
- sb.append("<br>");
- sb.append("<img src=" + "\"data:image/png;base64," + imgStr + "\"" + "/>");
- stream.close();
- }
- System.out.println("success");
- htmlStr = sb.toString();
- } catch (Exception e) {
- }
- return htmlStr;
- }
- /***
- * 功能 :调整图片大小
- *
- * @param srcImgPath
- * 原图片路径
- * @param distImgPath
- * 转换大小后图片路径
- * @param width
- * 转换后图片宽度
- * @param height
- * 转换后图片高度
- */
- 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));
- }
- }
|