Converting the Slides of a PPT into Images using Java - GeeksforGeeks

15 Jul, 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 
Download the zip file from the official site.Extract the external jar files from the zip file.Add the external jar files. For that select Java Build Path -> Configure Build Path -> Libraries -> Class -> Add External Jars...Select the jar file from the required folderClick Apply and close.
Download the zip file from the official site.
Extract the external jar files from the zip file.
Add the external jar files. For that select Java Build Path -> Configure Build Path -> Libraries -> Class -> Add External Jars...
Select the jar file from the required folder
Click Apply and close.
Imports from java.awt
java.awt.Color: This class has the colors needed to change the appearance of objects in the interface.java.awt.Dimension It contains the height and width of a component.java.awt.Graphics2D It provides provides control over geometry, coordinate transformations, color management, and text layout.java.awt.geom.Rectangle2D It describes a rectangle defined by a location (x,y) and dimension (w x h) .java.awt.image.BufferedImage It is used to handle and manipulate the image data.
java.awt.Color: This class has the colors needed to change the appearance of objects in the interface.
java.awt.Dimension It contains the height and width of a component.
java.awt.Graphics2D It provides provides control over geometry, coordinate transformations, color management, and text layout.
java.awt.geom.Rectangle2D It describes a rectangle defined by a location (x,y) and dimension (w x h) .
java.awt.image.BufferedImage It is used to handle and manipulate the image data.
Imports from java.io 
java.io.File: It contains several methods for working with the path name, deleting and renaming files,etc.java.io.FileInputStream: It is used to read data from a file.java.io.FileOutputStream: It is used for writing data to a File.java.io.IOException: It is an exception which is used in the code to throw a failure in Input and Output operations
java.io.File: It contains several methods for working with the path name, deleting and renaming files,etc.
java.io.FileInputStream: It is used to read data from a file.
java.io.FileOutputStream: It is used for writing data to a File.
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 
org.apache.poi.xslf.usermodel.XMLSlideShow: It is used to create objects to read or write a slideshow.org.apache.poi.xslf.usermodel.XSLFSlide: It is used to create and manage a slide in a presentation.
org.apache.poi.xslf.usermodel.XMLSlideShow: It is used to create objects to read or write a slideshow.
org.apache.poi.xslf.usermodel.XSLFSlide: It is used to create and manage a slide in a presentation.
Implementation:
Java
// Converting the slides of a PPT into Images using Javaimport 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:
After Execution of the program:
 
 
anikakapoor
Picked
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Functional Interfaces in Java
Stream In Java
Constructors in Java
Different ways of Reading a text file in Java
Exceptions in Java
Convert a String to Character array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?