Skip to content
Related Articles

Related Articles

Converting the Slides of a PPT into Images using Java
  • Difficulty Level : Easy
  • Last Updated : 04 Jan, 2021

To convert PowerPoint slides to images, multiple packages are required like, java.awt because It contains all the classes for creating user interfaces and for painting graphics and images, java.io because It provides a set of input streams and a set of output streams used to read and write data to files or other input and output sources, Apache POI because It is used to create, modify, and display Microsoft Office files using Java programs.

Download Apache POI 

  1. Dowload the zip file from the official site.
  2. Extract the external jar files from the zip file.
  3. Add the external jar files. 
    For that select Java Build Path -> Configure Build Path -> Libraries -> Class -> Add External Jars…
  4. Select the jar file from the required folder
  5. Click Apply and close.

Imports from java.awt

  1. java.awt.Color: This class has the colors needed to change the appearance of objects in the interface.
  2. java.awt.Dimension It contains the height and width of a component.
  3. java.awt.Graphics2D It provides provides control over geometry, coordinate transformations, color management, and text layout.
  4. java.awt.geom.Rectangle2D It describes a rectangle defined by a location (x,y) and dimension (w x h) .
  5. java.awt.image.BufferedImage It is used to handle and manipulate the image data.

Imports from java.io 

  1. java.io.File: It contains several methods for working with the path name, deleting and renaming files,etc.
  2. java.io.FileInputStream: It is used to read data from a file.
  3. java.io.FileOutputStream: It is used for writing data to a File.
  4. java.io.IOException: It is an exception which is used in the code to throw a failure in Input and Output operations

Imports from Apache POI 



  1. org.apache.poi.xslf.usermodel.XMLSlideShow: It is used to create objects to read or write a slideshow.
  2. org.apache.poi.xslf.usermodel.XSLFSlide: It is used create and manage a slide in a presentation.

Implementation:
 

Java




// Converting the slides of a PPT into Images using Java
import java.util.List;
  
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
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.IOException;
  
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
  
public class PPTToImages {
  
    public static void main(String args[])
        throws IOException
    {
  
        // create an empty presentation
        File file = new File("slides.pptx");
        XMLSlideShow ppt
            = new XMLSlideShow(new FileInputStream(file));
  
        // get the dimension and size of the slide
        Dimension pgsize = ppt.getPageSize();
        List<XSLFSlide> slide = ppt.getSlides();
        BufferedImage img = null;
  
        System.out.println(slide.size());
  
        for (int i = 0; i < slide.size(); i++) {
            img = new BufferedImage(
                pgsize.width, pgsize.height,
                BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = img.createGraphics();
  
            // clear area
            graphics.setPaint(Color.white);
            graphics.fill(new Rectangle2D.Float(
                0, 0, pgsize.width, pgsize.height));
  
            // draw the images
            slide.get(i).draw(graphics);
            FileOutputStream out = new FileOutputStream(
                "ppt_image" + i + ".png");
            javax.imageio.ImageIO.write(img, "png", out);
            ppt.write(out);
            out.close();
            System.out.println(i);
        }
        System.out.println("Image successfully created");
    }
}

Before the execution of the program:
 

Slides

After Execution of the program:

 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :