使用Java从PowerPoint幻灯片中获取背景图片

时间:2011-02-15 15:39:02

标签: java powerpoint apache-poi

我需要使用java从PowerPoint幻灯片中获取背景图像。我知道Apache POI项目。我可以找到从幻灯片中获取文本和形状的材料,但不是实际的背景。有没有人有任何建议?

编辑:我使用建议的链接捏了下面的代码。此代码似乎抓住幻灯片的内容,但不完全是背景。生成的图像为背景白色。

我用这个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++;
        }
    }

}


1 个答案:

答案 0 :(得分:1)

查看此问题的代码: Extracting images from pptx with apache poi

看起来应该是这样的:

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