Apache POI converts a PPT file into an image instance code,

Source: Internet
Author: User

Apache POI converts a PPT file into an image instance code,

This article describes how Apache POI converts a PPT to an image.

1. Apache POI Overview

Apache POI is a free and open-source cross-platform Java API written in Java. Apache POI provides APIs for Java programs to read and write Microsoft Office files.

You can view the official Apache POI documentation.

You can use either of the following methods to operate a PPT file on Apache POI:

1. POI-HSLF corresponding to the Powerpoint '97 (-2007) file format-suffixed with. ppt
2. File Format for PowerPoint 2007 OOXML corresponding to the POI-XSLF-suffixed with. pptx

2. JAR package

Jar packages required for POI office operations:

    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 Introduction:

You only need to introduce two maven methods, because 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 Mode

POI-HSLF processing PPT documents ending with. ppt suffix.

/*** The conversion Suffix of the ppt2003 document is .ppt * @ param pptFile * @ param imgFile the directory to be saved for the image (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 is timely. close (); Dimension pgsize = ppt. getPageSize (); Slide [] slide = ppt. getSlides (); for (int I = 0; I <slide. length; I ++) {Log.info ("page" + I +. "); 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 ); // re-set the font index and font name to prevent garbled characters in the generated image. rtruns [l]. setFontIndex (1); rtruns [l]. setFontName (" ");} // Generate image 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); // image storage location String absolutePath = imgFile. getAbsolutePath () + "/" + (I + 1) + ". jpeg "; File export File = new File (absolutePath );// Image path storage list. add (I + 1) + ". jpeg "); // if the image exists, if (then file is not generated. exists () {continue;} // set the image storage path and format (jpeg, png, bmp, etc ), note that the file path FileOutputStream out = new FileOutputStream (writable file); ImageIO. write (img, "jpeg", out); out. close ();} log. error ("PPT converted to image successful! "); Return true;} catch (Exception e) {log. error (" an Exception occurred when converting a PPT file to an image! ", E);} return false ;}

4. POI-XSLF

POI-XSLF-based processing of documents whose pptfiles end with a. pptx suffix.

/*** The conversion Suffix of the ppt2007 document is .pptx * @ param pptFile * @ param imgFile the path directory of the image to be saved (not a file) * @ param list: 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 (); // obtain the size of Dimension pgsize = xmlSlideShow. getPageSize (); // get the slide serving FS Lide [] slides = xmlSlideShow. getSlides (); for (int I = 0; I <slides. length; I ++) {// solve the garbled problem XSLFShape [] shapes = slides [I]. getShapes (); for (gradient fshape shape: shapes) {if (shape instanceof gradient ftextshape) {gradient ftextshape sh = (gradient ftextshape) shape; List <symbol ftextparagraph> textParagraphs = sh. getTextParagraphs (); for (export ftextparagraph: textParagraphs) {List <export ftextrun> textRuns = x SlfTextParagraph. getTextRuns (); for (Fig FIG: textRuns) {fig. setFontFamily ("") ;}}// generate an image 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); // set Path to be stored: String absolutePath = imgFile. getAbsolutePath () + "/" + (I + 1) + ". jpeg "; File paths File = new File (absolutePath); // list of image paths. add (I + 1) + ". jpeg "); // if the image exists, if (then file is not generated. exists () {continue;} // set the image storage path and format (jpeg, png, bmp, etc ), note that the file path FileOutputStream out = new FileOutputStream (writable file) is generated; // write it to the image to go to ImageIO. write (img, "jpeg", out); out. close ();} log. error ("PPT converted to image successful! "); Return true;} catch (Exception e) {log. error (" an Exception occurred when converting a PPT file to an 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)

If the above error occurs, it indicates that it is not used properly. You should use the second method to convert the PPT.

Sometimes the core conversion is prone to problems because POI is not doing well and images are sometimes prone to distortion.

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

Summary

The above is all about converting Apache poi ppt into image instance code. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.