关键类(PPTUtil):
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package ren.kun.meetingshow;
import com.spire.presentation.Presentation;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
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 java.util.List;
import javax.imageio.ImageIO;
import org.apache.commons.io.IOUtils;
import org.apache.poi.hslf.usermodel.HSLFShape;
import org.apache.poi.hslf.usermodel.HSLFSlide;
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.springframework.util.CollectionUtils;
/**
*
* @author 65340
*/
public class PPTUtil {
public static void PPTtoImage(String filePath, String fileName) throws Exception {
File f = new File(filePath + fileName);
if (f.exists()) {
if (f.getName().endsWith(".pptx") || f.getName().endsWith(".PPTX")) {
ppt2Img(filePath, fileName);
} else {
System.out.println("无法解析ppt文件,请使用pptx文件");
}
} else {
System.out.println("文件不存在");
}
}
//pptx里的页面转图片的关键函数
public static void ppt2Img(String filePath, String fileName) throws Exception {
String fileUrl = filePath + fileName;
String outputFile = filePath + "output";
Presentation ppt = new Presentation();
ppt.loadFromFile(fileUrl);
for (int i = 0; i < ppt.getSlides().getCount(); i++) {
BufferedImage image = ppt.getSlides().get(i).saveAsImage();
String fn = outputFile + "//" + String.format("ToImage-%1$s.jpg", i);
new File(fn).createNewFile();
ImageIO.write(image, "jpg", new File(fn));
}
}
//下面的函数无法成功执行(是pptx转Image的一个方法,但行不通)
public static void pptx2Image(String filePath, String fileName) throws Exception {
// 获取系统可用字体
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = e.getAvailableFontFamilyNames();
List availFonts = CollectionUtils.arrayToList(fontNames);
FileInputStream is = new FileInputStream(new File(filePath + fileName));
XMLSlideShow ppt = new XMLSlideShow(is);
Dimension pgsize = ppt.getPageSize();
int pageSize = ppt.getSlides().size();
for (int i = 0; i < pageSize; i++) {
//防止中文乱码
for (XSLFShape shape : ppt.getSlides().get(i).getShapes()) {
if (shape instanceof XSLFTextShape) {
XSLFTextShape tsh = (XSLFTextShape) shape;
for (XSLFTextParagraph p : tsh) {
for (XSLFTextRun r : p) {
String fontFamily = r.getFontFamily();
//log.info(">>>>>原始ppt字体:{}", fontFamily);
fontFamily = "宋体";
//log.info(">>>>>ppt字体修改为:{}", fontFamily);
r.setFontFamily(fontFamily);
// if (!availFonts.contains(fontFamily)) {
// fontFamily = "宋体";
// log.info(">>>>>ppt字体修改为:{}", fontFamily);
// r.setFontFamily(fontFamily);
// }
}
}
}
}
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 pptname = fileName.substring(0, fileName.lastIndexOf("."));
String newimgPath = filePath + "image/" + pptname + "/";
File imgPath = new File(newimgPath);
if (!imgPath.exists()) {//图片目录不存在则创建
imgPath.mkdirs();
}
String file = newimgPath + (i + 1) + ".png";
FileOutputStream out = new FileOutputStream(file);
javax.imageio.ImageIO.write(img, "png", out);
IOUtils.closeQuietly(out);
}
}
//下面的函数无法成功执行(是pptx转Image的一个方法,但行不通)
public static void ppt2Image(String filePath, String fileName) throws IOException {
// 获取系统可用字体
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = e.getAvailableFontFamilyNames();
List availFonts = CollectionUtils.arrayToList(fontNames);
HSLFSlideShow ppt = new HSLFSlideShow(new HSLFSlideShowImpl(filePath + fileName));
Dimension pgsize = ppt.getPageSize();
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) {
String fontFamily = r.getFontFamily();
//log.info(">>>>>原始ppt字体:{}", fontFamily);
fontFamily = "宋体";
//log.info(">>>>>ppt字体修改为:{}", fontFamily);
r.setFontFamily(fontFamily);
// if (!availFonts.contains(fontFamily)) {
// fontFamily = "宋体";
// log.info(">>>>>ppt字体修改为:{}", fontFamily);
// r.setFontFamily(fontFamily);
// }
}
}
}
}
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
HSLFSlide s = ppt.getSlides().get(i);
s.draw(graphics);
// save the output
String pptname = fileName.substring(0, fileName.lastIndexOf("."));
String newimgPath = filePath + "image/" + pptname + "/";
File imgPath = new File(newimgPath);
if (!imgPath.exists()) {//图片目录不存在则创建
imgPath.mkdirs();
}
String file = newimgPath + (i + 1) + ".png";
FileOutputStream out = new FileOutputStream(file);
javax.imageio.ImageIO.write(img, "png", out);
IOUtils.closeQuietly(out);
//resizeImage(filename, filename, width, height);
}
}
public static int countFileNum(String filePath, String endStr){
int counts = 0;
File dirFile = new File(filePath);
if (dirFile.isDirectory()) {
File[] files1 = dirFile.listFiles();
for (int i = 0; i < files1.length; i++) {
if (files1[i].getName().endsWith(endStr)) {
counts++;
}
}
}
return counts;
}
}
对该类(PPTUtil)的总结:
该类里面函数的最重要功能,就是把pptx里面的页面转换成图片