Apache POI converts PPT to image instance code

  • 2020-12-20 03:38:34
  • OfStack

This article mainly shares related content about Apache POI's conversion of PPT into images, and briefly introduces Apache POI, the specific content is as follows.

1. Introduction to Apache POI

Apache POI is a free and open source cross-platform Java API written with Java. Apache POI provides API to Java program to read and write Microsoft Office format files.


 Check out the official documentation  Apache POI website 

Apache POI operates PPT documents in two ways:

The file format of ES31en-ES32en corresponding to Powerpoint '97(-2007), with the suffix.ppt
The file format of PowerPoint 2007 OOXML corresponding to ES36en-ES37en has the suffix.pptx

2, JAR package

POI jar packages required to operate office:


    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

Introduction of maven method:

The maven approach only needs to introduce two, as they depend 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

POI-HSLF handles PPT documents ending with the.ppt suffix.


/**
   * ppt2003  Document transformation   The suffix is called .ppt
   * @param pptFile ppt file 
   * @param imgFile  The directory where the image will be saved (not the file) 
   * @return
   */
public static Boolean doPPT2003toImage(File pptFile,File imgFile,List<String> list) {
	try {
		FileInputStream is = new FileInputStream(pptFile);
		SlideShow ppt = new SlideShow(is);
		// Shut down in time   The input stream 
		is.close();
		Dimension pgsize = ppt.getPageSize();
		Slide[] slide = ppt.getSlides();
		for (int i = 0; i < slide.length; i++) {
			log.info(" The first " + 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++) {
					//  Original font index   and   The font name 
					int index = rtruns[l].getFontIndex();
					String name = rtruns[l].getFontName();
					log.info(" Original font index   and   The font name : "+index+" - "+name);
					//  To reset   Font index   and   The name of the font   Is to prevent the generation of the image disorderly code problem 
					rtruns[l].setFontIndex(1);
					rtruns[l].setFontName(" Song typeface ");
				}
			}
			// Generate images based on slide size 
			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);
			//  Where to save the picture 
			String absolutePath = imgFile.getAbsolutePath()+"/"+ (i + 1) + ".jpeg";
			File jpegFile = new File(absolutePath);
			//  Image path storage 
			list.add((i + 1) + ".jpeg");
			//  If the image exists, it is no longer generated 
			if (jpegFile.exists()) {
				continue;
			}
			//  Here to set the image storage path and image format (jpeg,png,bmp , etc. ), Note the build file path 
			FileOutputStream out = new FileOutputStream(jpegFile);
			ImageIO.write(img, "jpeg", out);
			out.close();
		}
		log.error("PPT Convert to picture   Success! ");
		return true;
	}
	catch (Exception e) {
		log.error("PPT Convert to picture   An exception has occurred! ", e);
	}
	return false;
}

4. POI-XSLF

POI-XSLF handles PPT files ending with the.pptx suffix.


/**
   * ppt2007 Document transformation   The suffix for .pptx
   * @param pptFile PPT file 
   * @param imgFile  The path directory to save the image (not the file) 
   * @param list  Store file name  list
   * @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 slides 
		XSLFSlide[] slides = xmlSlideShow.getSlides();
		for (int i = 0 ; i < slides.length ; i++) {
			//  Solve the messy code 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(" Song typeface ");
						}
					}
				}
			}
			// Generate images based on slide size 
			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 core code 
			slides[i].draw(graphics);
			// The path where the image will be stored 
			String absolutePath = imgFile.getAbsolutePath()+"/"+ (i + 1) + ".jpeg";
			File jpegFile = new File(absolutePath);
			//  Image path storage 
			list.add((i + 1) + ".jpeg");
			// If the image exists, it is no longer generated 
			if (jpegFile.exists()) {
				continue;
			}
			//  Here to set the image storage path and image format (jpeg,png,bmp , etc. ), Note the build file path 
			FileOutputStream out = new FileOutputStream(jpegFile);
			//  Write to the picture 
			ImageIO.write(img, "jpeg", out);
			out.close();
		}
		log.error("PPT Convert to picture   Success! ");
		return true;
	}
	catch (Exception e) {
		log.error("PPT Convert to picture   An exception has occurred! ", 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 indicates that there is no corresponding use. You should use the second method to convert PPT.

Sometimes the core conversion is a problem because the POI doesn't do it very well and the images are sometimes distorted.


//  The core code 
slides[i].draw(graphics);

conclusion

That's it for this article on Apache POI to convert PPT to image instance code, I hope you can help. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: