Extract Text and Images from a PowerPoint Document in Java
Tags: java, powerpoint, image
Published: 2019-10-29 08:12:27

---
title: Extract Text and Images from a PowerPoint Document in Java
published: true
description: This article shows you how to extract text and images from a PowerPoint in java.
tags: java,powerpoint,image
---

When you find a PowerPoint document with beautiful illustrations or background pictures, you may want to save the pictures to your own computer; or the text content in the document is exactly what you want to reference to other documents, you can save the text content to the text file first. In this article, we’ll introduce how to extract text and images from a PowerPoint document by using [Free Spire.Presentation for Java](https://www.e-iceblue.com/Introduce/free-presentation-for-java.html).

Here is a screent of the sample PowerPoint file:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/0n21rjhhtiwizboq0pt8.png)

##Extract Text from an Entire Document
```java
import com.spire.presentation.IAutoShape;
import com.spire.presentation.ISlide;
import com.spire.presentation.ParagraphEx;
import com.spire.presentation.Presentation;

import java.io.FileWriter;

public class ExtractText {

    public static void main(String[] args) throws Exception{

        //create a Presentation instance
        Presentation ppt = new Presentation();

        //load the PowerPoint document
        ppt.loadFromFile("C:\\Users\\Administrator\\Desktop\\ Triceratops Herds.pptx");

        //create a StringBuilder object
        StringBuilder buffer = new StringBuilder();

        //loop through the slides and extract text
        for (Object slide : ppt.getSlides()) {
            for (Object shape : ((ISlide) slide).getShapes()) {
                if (shape instanceof IAutoShape) {
                    buffer.append("\r\n");
                    for (Object tp : ((IAutoShape) shape).getTextFrame().getParagraphs()) {
                        buffer.append(((ParagraphEx) tp).getText());
                        buffer.append("\r\n");
                    }
                }
            }
        }

        //write to text file
        FileWriter writer = new FileWriter("ExtractedFiles/Text.txt");
        writer.write(buffer.toString());
        writer.flush();
        writer.close();
    }
}
```
Output:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/478sfqk46bn19phzrk4i.png)

##Extract Images from an Entire Document 
```java
import com.spire.presentation.Presentation;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class ExtractImages {

    public static void main(String[] args) throws Exception {

        //create a Presentation instance
        Presentation ppt = new Presentation();

        //load a PowerPoint document
        ppt.loadFromFile("C:\\Users\\Administrator\\Desktop\\ Triceratops Herds.pptx");

        //loop through the images embedded in the document
        for (int i = 0; i < ppt.getImages().getCount(); i++) {

            //save each image to a local folder
            BufferedImage image = ppt.getImages().get(i).getImage();
            ImageIO.write(image, "PNG",  new File(String.format("ExtractedFiles/" + "Image-%1$s.png", i)));
        }
    }
}
```
Output:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/u80dend6d3d58eki2szl.png)