user489041
user489041

Reputation: 28304

Get background image from PowerPoint Slide using Java

I need to get the background image from a PowerPoint slide using java. I am aware of the Apache POI project. I can find material for getting text and shapes from the slides, but not the actual background. Does anyone have any suggestions?

EDIT: I have creaked the following code using the suggested link. This code seems to grab the contents of the slide, but not exactly the background. The resulting images are white for the background.

I tried it with this PowerPoint


package PowerPointProcessing;

import Logging.LogRunner;
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.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Background;
import org.apache.poi.hslf.model.Fill;
import org.apache.poi.hslf.model.Shape;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;


/**
 *
 * @author dvargo
 */
public class PPI
{

    Dimension pageSize;
    public Slide[] theSlides;

    public PPI(String powerPointFilePath)
    {
        SlideShow ppt = null;
        //open the presentation
        try
        {
            ppt = new SlideShow(new HSLFSlideShow(powerPointFilePath));
        }
        catch(Exception e)
        {
            LogRunner.getLogger().severe("Could not open the powerpoint presentation");
            return;

        }


        //get all the slides
        theSlides = ppt.getSlides();
        //see how many slides there are
        int numberOfSlides = theSlides.length;
        pageSize = ppt.getPageSize();


    }

    public BufferedImage getBackground(Slide theSlide)
    {
        Background background;
        background = theSlide.getBackground();
        Fill f = background.getFill();
        Color color = f.getForegroundColor();
        Shape[] theShapes = theSlide.getShapes();

        BufferedImage img = new BufferedImage(pageSize.width, pageSize.height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = img.createGraphics();
        graphics.setPaint(color);
        graphics.fill(new Rectangle2D.Float(0, 0, pageSize.width, pageSize.height));
        theSlide.draw(graphics);
        return img;
    }

    public static void main(String[] args) {
        PPI ppi = new PPI("C:\\Documents and Settings\\dvargo\\Desktop\\Cludder\\a.ppt");
        int count= 0;
        for (Slide currSlide : ppi.theSlides)
        {
            BufferedImage img = ppi.getBackground(currSlide);
            try
            {
                ImageIO.write(img, "jpeg", new File("C:\\Documents and Settings\\dvargo\\Desktop\\ppt\\" + count + ".jpeg"));
            }
            catch (IOException ex)
            {
                Logger.getLogger(PPI.class.getName()).log(Level.SEVERE, null, ex);
            }
            count++;
        }
    }

}


Upvotes: 2

Views: 3363

Answers (1)

jzd
jzd

Reputation: 23629

Looking at the code from this question: Extracting images from pptx with apache poi

Looks like it should be something like:

Background background = slides[current].getBackground(); 
Fill f = background.getFill(); 
Color color = f.getForegroundColor(); 

Upvotes: 1

Related Questions