Search in sources :

Example 1 with HSLFPictureData

use of org.apache.poi.hslf.usermodel.HSLFPictureData in project poi by apache.

the class DataExtraction method main.

public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        usage();
        return;
    }
    FileInputStream is = new FileInputStream(args[0]);
    HSLFSlideShow ppt = new HSLFSlideShow(is);
    is.close();
    //extract all sound files embedded in this presentation
    HSLFSoundData[] sound = ppt.getSoundData();
    for (int i = 0; i < sound.length; i++) {
        //*.wav
        String type = sound[i].getSoundType();
        //typically file name
        String name = sound[i].getSoundName();
        //raw bytes
        byte[] data = sound[i].getData();
        //save the sound  on disk
        FileOutputStream out = new FileOutputStream(name + type);
        out.write(data);
        out.close();
    }
    int oleIdx = -1, picIdx = -1;
    for (HSLFSlide slide : ppt.getSlides()) {
        //extract embedded OLE documents
        for (HSLFShape shape : slide.getShapes()) {
            if (shape instanceof OLEShape) {
                oleIdx++;
                OLEShape ole = (OLEShape) shape;
                HSLFObjectData data = ole.getObjectData();
                String name = ole.getInstanceName();
                if ("Worksheet".equals(name)) {
                    //read xls
                    @SuppressWarnings({ "unused", "resource" }) HSSFWorkbook wb = new HSSFWorkbook(data.getData());
                } else if ("Document".equals(name)) {
                    HWPFDocument doc = new HWPFDocument(data.getData());
                    //read the word document
                    Range r = doc.getRange();
                    for (int k = 0; k < r.numParagraphs(); k++) {
                        Paragraph p = r.getParagraph(k);
                        System.out.println(p.text());
                    }
                    //save on disk
                    FileOutputStream out = new FileOutputStream(name + "-(" + (oleIdx) + ").doc");
                    doc.write(out);
                    out.close();
                    doc.close();
                } else {
                    FileOutputStream out = new FileOutputStream(ole.getProgID() + "-" + (oleIdx + 1) + ".dat");
                    InputStream dis = data.getData();
                    byte[] chunk = new byte[2048];
                    int count;
                    while ((count = dis.read(chunk)) >= 0) {
                        out.write(chunk, 0, count);
                    }
                    is.close();
                    out.close();
                }
            } else //Pictures
            if (shape instanceof HSLFPictureShape) {
                picIdx++;
                HSLFPictureShape p = (HSLFPictureShape) shape;
                HSLFPictureData data = p.getPictureData();
                String ext = data.getType().extension;
                FileOutputStream out = new FileOutputStream("pict-" + picIdx + ext);
                out.write(data.getData());
                out.close();
            }
        }
    }
    ppt.close();
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HSLFObjectData(org.apache.poi.hslf.usermodel.HSLFObjectData) Range(org.apache.poi.hwpf.usermodel.Range) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) FileInputStream(java.io.FileInputStream) OLEShape(org.apache.poi.hslf.model.OLEShape) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Paragraph(org.apache.poi.hwpf.usermodel.Paragraph) HWPFDocument(org.apache.poi.hwpf.HWPFDocument) HSLFShape(org.apache.poi.hslf.usermodel.HSLFShape) HSLFPictureShape(org.apache.poi.hslf.usermodel.HSLFPictureShape) FileOutputStream(java.io.FileOutputStream) HSLFSoundData(org.apache.poi.hslf.usermodel.HSLFSoundData) HSLFPictureData(org.apache.poi.hslf.usermodel.HSLFPictureData) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide)

Example 2 with HSLFPictureData

use of org.apache.poi.hslf.usermodel.HSLFPictureData in project poi by apache.

the class TestBackground method backgroundPicture.

/**
     * Create a ppt with various fill effects
     */
@Test
public void backgroundPicture() throws IOException {
    HSLFSlideShow ppt1 = new HSLFSlideShow();
    HSLFSlide slide;
    HSLFFill fill;
    HSLFShape shape;
    HSLFPictureData data;
    //slide 1
    slide = ppt1.createSlide();
    slide.setFollowMasterBackground(false);
    fill = slide.getBackground().getFill();
    data = ppt1.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
    fill.setFillType(HSLFFill.FILL_PICTURE);
    fill.setPictureData(data);
    shape = new HSLFAutoShape(ShapeType.RECT);
    shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
    fill = shape.getFill();
    fill.setFillType(HSLFFill.FILL_SOLID);
    slide.addShape(shape);
    //slide 2
    slide = ppt1.createSlide();
    slide.setFollowMasterBackground(false);
    fill = slide.getBackground().getFill();
    data = ppt1.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
    fill.setFillType(HSLFFill.FILL_PATTERN);
    fill.setPictureData(data);
    fill.setBackgroundColor(Color.green);
    fill.setForegroundColor(Color.red);
    shape = new HSLFAutoShape(ShapeType.RECT);
    shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
    fill = shape.getFill();
    fill.setFillType(HSLFFill.FILL_BACKGROUND);
    slide.addShape(shape);
    //slide 3
    slide = ppt1.createSlide();
    slide.setFollowMasterBackground(false);
    fill = slide.getBackground().getFill();
    data = ppt1.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
    fill.setFillType(HSLFFill.FILL_TEXTURE);
    fill.setPictureData(data);
    shape = new HSLFAutoShape(ShapeType.RECT);
    shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
    fill = shape.getFill();
    fill.setFillType(HSLFFill.FILL_PICTURE);
    data = ppt1.addPicture(_slTests.readFile("clock.jpg"), PictureType.JPEG);
    fill.setPictureData(data);
    slide.addShape(shape);
    // slide 4
    slide = ppt1.createSlide();
    slide.setFollowMasterBackground(false);
    fill = slide.getBackground().getFill();
    fill.setFillType(HSLFFill.FILL_SHADE_CENTER);
    fill.setBackgroundColor(Color.white);
    fill.setForegroundColor(Color.darkGray);
    shape = new HSLFAutoShape(ShapeType.RECT);
    shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
    fill = shape.getFill();
    fill.setFillType(HSLFFill.FILL_SHADE);
    fill.setBackgroundColor(Color.red);
    fill.setForegroundColor(Color.green);
    slide.addShape(shape);
    //serialize and read again
    HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
    List<HSLFSlide> slides = ppt2.getSlides();
    fill = slides.get(0).getBackground().getFill();
    assertEquals(HSLFFill.FILL_PICTURE, fill.getFillType());
    assertEquals(3, getFillPictureRefCount(slides.get(0).getBackground(), fill));
    shape = slides.get(0).getShapes().get(0);
    assertEquals(HSLFFill.FILL_SOLID, shape.getFill().getFillType());
    fill = slides.get(1).getBackground().getFill();
    assertEquals(HSLFFill.FILL_PATTERN, fill.getFillType());
    shape = slides.get(1).getShapes().get(0);
    assertEquals(HSLFFill.FILL_BACKGROUND, shape.getFill().getFillType());
    fill = slides.get(2).getBackground().getFill();
    assertEquals(HSLFFill.FILL_TEXTURE, fill.getFillType());
    assertEquals(3, getFillPictureRefCount(slides.get(2).getBackground(), fill));
    shape = slides.get(2).getShapes().get(0);
    assertEquals(HSLFFill.FILL_PICTURE, shape.getFill().getFillType());
    assertEquals(1, getFillPictureRefCount(shape, fill));
    fill = slides.get(3).getBackground().getFill();
    assertEquals(HSLFFill.FILL_SHADE_CENTER, fill.getFillType());
    shape = slides.get(3).getShapes().get(0);
    assertEquals(HSLFFill.FILL_SHADE, shape.getFill().getFillType());
    ppt2.close();
    ppt1.close();
}
Also used : HSLFShape(org.apache.poi.hslf.usermodel.HSLFShape) HSLFAutoShape(org.apache.poi.hslf.usermodel.HSLFAutoShape) HSLFPictureData(org.apache.poi.hslf.usermodel.HSLFPictureData) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) HSLFFill(org.apache.poi.hslf.usermodel.HSLFFill) Test(org.junit.Test)

Example 3 with HSLFPictureData

use of org.apache.poi.hslf.usermodel.HSLFPictureData in project poi by apache.

the class TestMovieShape method testCreate.

@Test
public void testCreate() throws Exception {
    HSLFSlideShow ppt = new HSLFSlideShow();
    HSLFSlide slide = ppt.createSlide();
    String path = "/test-movie.mpg";
    int movieIdx = ppt.addMovie(path, MovieShape.MOVIE_MPEG);
    HSLFPictureData thumbnailData = ppt.addPicture(_slTests.readFile("tomcat.png"), PictureType.PNG);
    MovieShape shape = new MovieShape(movieIdx, thumbnailData);
    shape.setAnchor(new Rectangle2D.Double(300, 225, 120, 90));
    slide.addShape(shape);
    assertEquals(path, shape.getPath());
    assertTrue(shape.isAutoPlay());
    shape.setAutoPlay(false);
    assertFalse(shape.isAutoPlay());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ppt.write(out);
    ppt = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
    slide = ppt.getSlides().get(0);
    shape = (MovieShape) slide.getShapes().get(0);
    assertEquals(path, shape.getPath());
    assertFalse(shape.isAutoPlay());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Rectangle2D(java.awt.geom.Rectangle2D) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HSLFPictureData(org.apache.poi.hslf.usermodel.HSLFPictureData) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) Test(org.junit.Test)

Example 4 with HSLFPictureData

use of org.apache.poi.hslf.usermodel.HSLFPictureData in project poi by apache.

the class TestOleEmbedding method testOleEmbedding2003.

/**
     * Tests support for OLE objects.
     *
     * @throws Exception if an error occurs.
     */
@Test
public void testOleEmbedding2003() throws IOException {
    HSLFSlideShowImpl slideShow = new HSLFSlideShowImpl(_slTests.openResourceAsStream("ole2-embedding-2003.ppt"));
    // Placeholder EMFs for clients that don't support the OLE components.
    List<HSLFPictureData> pictures = slideShow.getPictureData();
    assertEquals("Should be two pictures", 2, pictures.size());
    long[] checkSums = { 0xD37A4204l, 0x26A62F68l, 0x82853169l, 0xE0E45D2Bl };
    int checkId = 0;
    // check for checksum to be uptodate
    for (HSLFPictureData pd : pictures) {
        long checkEMF = IOUtils.calculateChecksum(pd.getData());
        assertEquals(checkSums[checkId++], checkEMF);
    }
    // Actual embedded objects.
    HSLFObjectData[] objects = slideShow.getEmbeddedObjects();
    assertEquals("Should be two objects", 2, objects.length);
    for (HSLFObjectData od : objects) {
        long checkEMF = IOUtils.calculateChecksum(od.getData());
        assertEquals(checkSums[checkId++], checkEMF);
    }
    slideShow.close();
}
Also used : HSLFPictureData(org.apache.poi.hslf.usermodel.HSLFPictureData) HSLFObjectData(org.apache.poi.hslf.usermodel.HSLFObjectData) HSLFSlideShowImpl(org.apache.poi.hslf.usermodel.HSLFSlideShowImpl) Test(org.junit.Test)

Example 5 with HSLFPictureData

use of org.apache.poi.hslf.usermodel.HSLFPictureData in project poi by apache.

the class TestDocumentEncryption method cryptoAPIChangeKeySize.

@Test
public void cryptoAPIChangeKeySize() throws Exception {
    String pptFile = "cryptoapi-proc2356.ppt";
    Biff8EncryptionKey.setCurrentUserPassword("crypto");
    NPOIFSFileSystem fs = new NPOIFSFileSystem(slTests.getFile(pptFile), true);
    HSLFSlideShowImpl hss = new HSLFSlideShowImpl(fs);
    // need to cache data (i.e. read all data) before changing the key size
    List<HSLFPictureData> picsExpected = hss.getPictureData();
    hss.getDocumentSummaryInformation();
    EncryptionInfo ei = hss.getDocumentEncryptionAtom().getEncryptionInfo();
    ((CryptoAPIEncryptionHeader) ei.getHeader()).setKeySize(0x78);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    hss.write(bos);
    hss.close();
    fs.close();
    fs = new NPOIFSFileSystem(new ByteArrayInputStream(bos.toByteArray()));
    hss = new HSLFSlideShowImpl(fs);
    List<HSLFPictureData> picsActual = hss.getPictureData();
    assertEquals(picsExpected.size(), picsActual.size());
    for (int i = 0; i < picsExpected.size(); i++) {
        assertArrayEquals(picsExpected.get(i).getRawData(), picsActual.get(i).getRawData());
    }
    hss.close();
    fs.close();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) CryptoAPIEncryptionHeader(org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionHeader) ByteArrayInputStream(java.io.ByteArrayInputStream) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HSLFPictureData(org.apache.poi.hslf.usermodel.HSLFPictureData) HSLFSlideShowImpl(org.apache.poi.hslf.usermodel.HSLFSlideShowImpl) Test(org.junit.Test)

Aggregations

HSLFPictureData (org.apache.poi.hslf.usermodel.HSLFPictureData)10 HSLFSlideShow (org.apache.poi.hslf.usermodel.HSLFSlideShow)7 Test (org.junit.Test)7 HSLFSlide (org.apache.poi.hslf.usermodel.HSLFSlide)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 HSLFSlideShowImpl (org.apache.poi.hslf.usermodel.HSLFSlideShowImpl)4 Rectangle2D (java.awt.geom.Rectangle2D)3 HSLFShape (org.apache.poi.hslf.usermodel.HSLFShape)3 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 HSLFObjectData (org.apache.poi.hslf.usermodel.HSLFObjectData)2 HSLFPictureShape (org.apache.poi.hslf.usermodel.HSLFPictureShape)2 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)2 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)2 Dimension (java.awt.Dimension)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 MessageDigest (java.security.MessageDigest)1