package com; import org.apache.poi.ooxml.POIXMLDocumentPart; import org.apache.poi.xslf.usermodel.*; import java.awt.*; 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.InputStream; public class ImageConvert { public static void converter(String fileName) { // BackRS rs = new BackRS(); File file = new File(fileName); String name = file.getName(); String filePath = file.getParent(); try { FileInputStream in = new FileInputStream(file); XMLSlideShow ppt = new XMLSlideShow(in); Dimension pgsize = ppt.getPageSize(); String saveImagePathName = filePath + File.separator + getFileNameNoEx(name) + "_JPG"; File path = new File(saveImagePathName); if (!path.exists()) { path.mkdir(); } Integer i = 0; BufferedImage img = null; String mp4Name = ""; for (XSLFSlide slide : ppt.getSlides()) { //解决乱码问题 for (XSLFShape shape : slide.getShapes()) { if (shape instanceof XSLFTextShape) { XSLFTextShape tsh = (XSLFTextShape) shape; for (XSLFTextParagraph p : tsh) { for (XSLFTextRun r : p) { r.setFontFamily("宋体"); } } } } //将pptx的每一页保存成图片 img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = img.createGraphics(); graphics.setPaint(Color.white); graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height)); slide.draw(graphics); FileOutputStream out = new FileOutputStream(path + File.separator + (i + 1) + ".JPG"); i++; javax.imageio.ImageIO.write(img, "JPG", out); out.close(); for (POIXMLDocumentPart part : slide.getRelations()) { String contentType = part.getPackagePart().getContentType(); //取pptx中的图片 // if (contentType.equals("image/jpeg")||contentType.equals("image/png")) { // InputStream is = part.getPackagePart().getInputStream(); // FileOutputStream fos = new FileOutputStream(path + File.separator + (i + 1) + ".jpg"); // i++; // byte[] b = new byte[1024]; // int length; // while ((length = is.read(b)) > 0) { // fos.write(b, 0, length); // } // is.close(); // fos.close(); // }else if (contentType.equals("video/mp4")) {//判断类型是否为mp4 if (name.indexOf(String.valueOf(part.getPackagePart().getPartName())) == -1) {//因为相同的MP4会出现两次,所以做此判断 name += "," + part.getPackagePart().getPartName(); } else { continue; } InputStream is = part.getPackagePart().getInputStream(); FileOutputStream fos = new FileOutputStream(path + File.separator + (i + 1) + ".mp4"); i++; byte[] b = new byte[1024]; int length; while ((length = is.read(b)) > 0) { fos.write(b, 0, length); } is.close(); fos.close(); } } } System.out.println("Image successfully converted."); } catch (Exception e) { System.out.println("error:" + e.getMessage()); } } /** * 获取文件名,去除扩展名的 */ public static String getFileNameNoEx(String filename) { if ((filename != null) && (filename.length() > 0)) { int dot = filename.lastIndexOf('.'); if ((dot > -1) && (dot < (filename.length()))) { return filename.substring(0, dot); } } return filename; } public static void main(String[] args) { System.out.println("Hello World!"); // ImageConvert imageConvert=new ImageConvert(); converter("E:\\spring-boot\\SpireDemo\\1.pptx"); System.out.println("successfully!"); } }
pom.xml
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>4.1.2</version> </dependency>