This article mainly shares related content about Apache POI converting PPT into pictures. It briefly introduces Apache POI, and the specific content is as follows.
1. Introduction to Apache POI
Apache POI is a free and open source cross-platform Java API written in Java. Apache POI provides the function of Java programs to read and write Microsoft Office format archives.
You can view the official document Apache POI official website
There are two ways to operate PPT documents with Apache POI:
1. The file format suffix of Powerpoint '97(-2007) corresponding to POI-HSLF is .ppt
2. The file format suffix of PowerPoint 2007 OOXML corresponding to POI-XSLF is .pptx
2. JAR package
POI operation of office requires jar package:
poi-3.12.jar poi-ooxml-3.12.jar poi-ooxml-schemas-3.12.jar poi-scratchpad-3.12.jar xmlbeans-2.6.0.jar
Maven method introduced:
The maven method only needs to introduce two, because they rely on several others
<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>
3. POI-HSLF method
POI-HSLF method handles documents ending with the .ppt suffix of PPT.
/** * The conversion suffix of the ppt2003 document is .ppt * @param pptFile ppt file* @param imgFile The directory to which the image will be saved (not a file) * @return */public static Boolean doPPT2003toImage(File pptFile,File imgFile,List<String> list) {try {FileInputStream is = new FileInputStream(pptFile);SlideShow ppt = new SlideShow(is);//Close the input stream in time is.close();Dimension pgsize = ppt.getPageSize();Slide[] slide = ppt.getSlides();for (int i = 0; i < slide.length; i++) {log.info("Th" + i + "Page.");TextRun[] truns = slide[i].getTextRuns();for (int k = 0; k < truns.length; k++) {RichTextRun[] rtruns = truns[k].getRichTextRuns();for (int l = 0; l < rtruns.length; l++) {// The original font index and font name int index = rtruns[l].getFontIndex();String name = rtruns[l].getFontName();log.info("Original font index and font name: "+index+" - "+name);// Reset the font index and font name to prevent the generated image garbled rtruns[l].setFontIndex(1);rtruns[l].setFontName("宋体");}}// Generate the image according to the size of the slide BufferedImage 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[i].draw(graphics);// The image is saved by String absolutePath = imgFile.getAbsolutePath()+"/"+ (i + 1) + ".jpeg";File jpegFile = new File(absolutePath);// The image path is stored in list.add((i + 1) + ".jpeg");// If the image exists, no if is generated (jpegFile.exists()) {continue;}// Here is the storage path of the image and the format of the image (jpeg, png, bmp, etc.), pay attention to the generation file path FileOutputStream out = new FileOutputStream(jpegFile);ImageIO.write(img, "jpeg", out);out.close();}log.error("PPT converted to image successfully!");return true;}catch (Exception e) {log.error("Exception occurred when converting to image!", e);}return false;}4. POI-XSLF method
POI-XSLF method handles documents ending with the .pptx suffix of PPT files.
/** * The conversion suffix of the ppt2007 document is .pptx * @param pptFile PPT file * @param imgFile The path directory to which the image will be saved (not a file) * @param list The list that stores the file name * @return */public static Boolean doPPT2007toImage(File pptFile,File imgFile,List<String> list) {FileInputStream is = null ;try {is = new FileInputStream(pptFile);XMLSlideShow xmlSlideShow = new XMLSlideShow(is);is.close();// Get the size Dimension pgsize = xmlSlideShow.getPageSize();// Get the slide XSLFSlide[] slides = xmlSlideShow.getSlides();for (int i = 0 ; i < slides.length ; i++) {// Solve the garbled problem XSLFShape[] shapes = slides[i].getShapes();for (XSLFShape shape : shapes) {if (shape instanceof XSLFTextShape) {XSLFTextShape sh = (XSLFTextShape) shape;List<XSLFTextParagraph> textParagraphs = sh.getTextParagraphs();for (XSLFTextParagraph xslfTextParagraph : textParagraphs) {List<XSLFTextRun> textRuns = xslfTextParagraph.getTextRuns();for (XSLFTextRun xslfTextRun: textRuns) {xslfTextRun.setFontFamily("安");}}}}//Generate image according to the size of the slide BufferedImage 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));// The most core code slides[i].draw(graphics);// The path to which the image will be stored String absolutePath = imgFile.getAbsolutePath()+"/"+ (i + 1) + ".jpeg";File jpegFile = new File(absolutePath);// The image path is stored in list.add((i + 1) + ".jpeg");// If the image exists, if (jpegFile.exists()) {continue;}// Here is the image storage path and the image format (jpeg, png, bmp, etc.), pay attention to the generation file path FileOutputStream out = new FileOutputStream(jpegFile);// Write to the image to ImageIO.write(img, "jpeg", out);out.close();}log.error("PPT converted to image successfully!");return true;}catch (Exception e) {log.error("Exception occurred when the conversion to image was converted to image!", e);}return false;}5. Possible errors
org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
The above error occurs, which means that it is not used in correspondence, and the second method should be used to convert PPT.
Sometimes problems occur when core conversions are easily changed because the POI is not done well and the pictures are sometimes easily distorted.
// The most core code slides[i].draw(graphics);
Summarize
The above is all about Apache POI converting PPT into image instance code. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!