pom文件添加
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.12</version>
</dependency>
ppt转图片工具类
public class PPTtoImageUtils {
private static final Logger logger = LoggerFactory.getLogger(PPTtoImageUtils.class);
//支持pptx
public static List<String> pptxToImage(MultipartFile file, String bucket) {
String originalFileName = file.getOriginalFilename();
String rootPath = bucket + "/" + System.currentTimeMillis();
List<String> urlList = Lists.newArrayList();
try {
InputStream is = file.getInputStream();
XMLSlideShow ppt = new XMLSlideShow(is);
is.close();
Dimension pgsize = ppt.getPageSize();
logger.info("pptx to image file [name:{}] [width:{}] [height:{}]", originalFileName, pgsize.width, pgsize.height);
for (int i = 0; i < ppt.getSlides().size(); 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) {
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 filename = (i + 1) + ".png";
// 创建输出流
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// 将图像输出到输出流中。
ImageIO.write(img, "png", bos);
MultipartFile multipartFile = new MockMultipartFile(filename, filename, "image/png", bos.toByteArray());
//上传到oss
String url = UploadUtils.simpleUpload(multipartFile, rootPath);
urlList.add(url);
}
} catch (Exception e) {
logger.error("pptx to image error [name:{}][e:{}] ", originalFileName,e);
throw new BusinessException(ErrorCode.PPT_TO_IMAGE_ERROR);
}
logger.info("pptx to image success [name:{}] ", originalFileName);
return urlList;
}
//支持ppt
public static List<String> pptToImage(MultipartFile file, String bucket) {
String originalFileName = file.getOriginalFilename();
String rootPath = bucket + "/" + System.currentTimeMillis();
List<String> urlList = Lists.newArrayList();
try {
InputStream is = file.getInputStream();
HSLFSlideShow ppt = new HSLFSlideShow(new HSLFSlideShowImpl(is));
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) {
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 filename = (i + 1) + ".png";
// 创建输出流
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// 将图像输出到输出流中。
ImageIO.write(img, "png", bos);
MultipartFile multipartFile = new MockMultipartFile(filename, filename, "image/png", bos.toByteArray());
//上传到oss
String url = UploadUtils.simpleUpload(multipartFile, rootPath);
urlList.add(url);
}
logger.info("ppt to image success [name:{}] ", originalFileName);
return urlList;
} catch (Exception e) {
logger.error("ppt to image error [name:{}] [e:{}]", originalFileName,e);
throw new BusinessException(ErrorCode.PPT_TO_IMAGE_ERROR);
}
}
|