001    /*
002     * JBoss, Home of Professional Open Source.
003     * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004     * as indicated by the @author tags. See the copyright.txt file in the
005     * distribution for a full listing of individual contributors.
006     *
007     * This is free software; you can redistribute it and/or modify it
008     * under the terms of the GNU Lesser General Public License as
009     * published by the Free Software Foundation; either version 2.1 of
010     * the License, or (at your option) any later version.
011     *
012     * This software is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015     * Lesser General Public License for more details.
016     *
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this software; if not, write to the Free
019     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021     */
022    package org.jboss.dna.sequencer.msoffice.powerpoint;
023    
024    import java.util.List;
025    import java.io.*;
026    import java.awt.image.BufferedImage;
027    import java.awt.*;
028    import java.awt.geom.Rectangle2D;
029    import java.util.ArrayList;
030    import org.apache.poi.hslf.usermodel.SlideShow;
031    import org.apache.poi.hslf.model.Slide;
032    import org.apache.poi.hslf.model.TextRun;
033    
034    /**
035     * Utility for extracting metadata from PowerPoint files
036     * 
037     * @author Michael Trezzi
038     */
039    public class PowerPointMetadataReader {
040    
041        public static List<SlideMetadata> instance( InputStream stream ) throws IOException {
042            SlideShow slideshow = new SlideShow(stream);
043            Slide[] slides = slideshow.getSlides();
044    
045            List<SlideMetadata> slidesMetadata = new ArrayList<SlideMetadata>();
046    
047            for (Slide slide : slides) {
048                SlideMetadata slideMetadata = new SlideMetadata();
049                // process title
050                slideMetadata.setTitle(slide.getTitle());
051    
052                // process notes
053                for (TextRun textRun : slide.getNotesSheet().getTextRuns()) {
054                    if (slideMetadata.getNotes() == null) {
055                        slideMetadata.setNotes("");
056                    }
057                    slideMetadata.setNotes(slideMetadata.getNotes() + textRun.getText());
058                }
059                // process text
060                for (TextRun textRun : slide.getTextRuns()) {
061                    if (!textRun.getText().equals(slideMetadata.getTitle()) && textRun.getText() != null) {
062                        if (slideMetadata.getText() == null) {
063                            slideMetadata.setText("");
064                        }
065                        slideMetadata.setText(slideMetadata.getText() + textRun.getText());
066                    }
067                }
068    
069                // process thumbnail
070                Dimension pgsize = slideshow.getPageSize();
071    
072                BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
073                Graphics2D graphics = img.createGraphics();
074                // clear the drawing area
075                graphics.setPaint(Color.white);
076                graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
077    
078                // render
079                slide.draw(graphics);
080    
081                ByteArrayOutputStream out = new ByteArrayOutputStream();
082                javax.imageio.ImageIO.write(img, "png", out);
083                slideMetadata.setThumbnail(out.toByteArray());
084    
085                slidesMetadata.add(slideMetadata);
086    
087            }
088    
089            return slidesMetadata;
090        }
091    
092    }