Search in sources :

Example 6 with HSLFPictureData

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

the class HSLFExtractor method handleSlideEmbeddedPictures.

private void handleSlideEmbeddedPictures(HSLFSlideShow slideshow, XHTMLContentHandler xhtml) throws TikaException, SAXException, IOException {
    for (HSLFPictureData pic : slideshow.getPictureData()) {
        String mediaType;
        switch(pic.getType()) {
            case EMF:
                mediaType = "image/emf";
                break;
            case WMF:
                mediaType = "image/wmf";
                break;
            case DIB:
                mediaType = "image/bmp";
                break;
            default:
                mediaType = pic.getContentType();
                break;
        }
        byte[] data = null;
        try {
            data = pic.getData();
        } catch (Exception e) {
            EmbeddedDocumentUtil.recordEmbeddedStreamException(e, parentMetadata);
            continue;
        }
        try (TikaInputStream picIs = TikaInputStream.get(data)) {
            handleEmbeddedResource(picIs, null, null, mediaType, xhtml, false);
        }
    }
}
Also used : TikaInputStream(org.apache.tika.io.TikaInputStream) HSLFPictureData(org.apache.poi.hslf.usermodel.HSLFPictureData) TikaException(org.apache.tika.exception.TikaException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 7 with HSLFPictureData

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

the class ImageExtractor method main.

public static void main(String[] args) throws IOException {
    if (args.length < 1) {
        System.err.println("Usage:");
        System.err.println("\tImageExtractor <file>");
        return;
    }
    HSLFSlideShow ppt = new HSLFSlideShow(new HSLFSlideShowImpl(args[0]));
    //extract all pictures contained in the presentation
    int i = 0;
    for (HSLFPictureData pict : ppt.getPictureData()) {
        // picture data
        byte[] data = pict.getData();
        PictureType type = pict.getType();
        FileOutputStream out = new FileOutputStream("pict_" + i++ + type.extension);
        out.write(data);
        out.close();
    }
    ppt.close();
}
Also used : PictureType(org.apache.poi.sl.usermodel.PictureData.PictureType) FileOutputStream(java.io.FileOutputStream) HSLFPictureData(org.apache.poi.hslf.usermodel.HSLFPictureData) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlideShowImpl(org.apache.poi.hslf.usermodel.HSLFSlideShowImpl)

Example 8 with HSLFPictureData

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

the class TestDocumentEncryption method cryptoAPIDecryption.

@Test
public void cryptoAPIDecryption() throws Exception {
    // taken from a msdn blog:
    // http://blogs.msdn.com/b/openspecification/archive/2009/05/08/dominic-salemno.aspx
    Biff8EncryptionKey.setCurrentUserPassword("crypto");
    NPOIFSFileSystem fs = new NPOIFSFileSystem(slTests.getFile("cryptoapi-proc2356.ppt"));
    HSLFSlideShowImpl hss = new HSLFSlideShowImpl(fs);
    HSLFSlideShow ss = new HSLFSlideShow(hss);
    HSLFSlide slide = ss.getSlides().get(0);
    String rawText = HSLFTextParagraph.getRawText(slide.getTextParagraphs().get(0));
    assertEquals("Dominic Salemno", rawText);
    String[][] picCmp = { { "0", "nKsDTKqxTCR8LFkVVWlP9GSTvZ0=" }, { "95163", "SuNOR+9V1UVYZIoeD65l3VTaLoc=" }, { "100864", "Ql3IGrr4bNq07ZTp5iPg7b+pva8=" }, { "714114", "8pdst9NjBGSfWezSZE8+aVhIRe0=" }, { "723752", "go6xqW7lvkCtlOO5tYLiMfb4oxw=" }, { "770128", "gZUM8YqRNL5kGNfyyYvEEernvCc=" }, { "957958", "CNU2iiqUFAnk3TDXsXV1ihH9eRM=" } };
    MessageDigest md = CryptoFunctions.getMessageDigest(HashAlgorithm.sha1);
    List<HSLFPictureData> pd = hss.getPictureData();
    int i = 0;
    for (HSLFPictureData p : pd) {
        byte[] hash = md.digest(p.getData());
        assertEquals(Integer.parseInt(picCmp[i][0]), p.getOffset());
        assertEquals(picCmp[i][1], Base64.encodeBase64String(hash));
        i++;
    }
    DocumentEncryptionAtom dea = hss.getDocumentEncryptionAtom();
    POIFSFileSystem fs2 = ((CryptoAPIDecryptor) dea.getEncryptionInfo().getDecryptor()).getSummaryEntries(fs.getRoot(), "EncryptedSummary");
    PropertySet ps = PropertySetFactory.create(fs2.getRoot(), SummaryInformation.DEFAULT_STREAM_NAME);
    assertTrue(ps.isSummaryInformation());
    assertEquals("RC4 CryptoAPI Encryption", ps.getProperties()[1].getValue());
    ps = PropertySetFactory.create(fs2.getRoot(), DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    assertTrue(ps.isDocumentSummaryInformation());
    assertEquals("On-screen Show (4:3)", ps.getProperties()[1].getValue());
    ss.close();
    fs.close();
    fs2.close();
}
Also used : HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlideShowImpl(org.apache.poi.hslf.usermodel.HSLFSlideShowImpl) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) PropertySet(org.apache.poi.hpsf.PropertySet) MessageDigest(java.security.MessageDigest) HSLFPictureData(org.apache.poi.hslf.usermodel.HSLFPictureData) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) CryptoAPIDecryptor(org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIDecryptor) Test(org.junit.Test)

Example 9 with HSLFPictureData

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

the class TestShapes method shapeGroup.

/**
     * Test adding shapes to <code>ShapeGroup</code>
     */
@Test
public void shapeGroup() throws IOException {
    HSLFSlideShow ss = new HSLFSlideShow();
    HSLFSlide slide = ss.createSlide();
    Dimension pgsize = ss.getPageSize();
    HSLFGroupShape group = new HSLFGroupShape();
    group.setAnchor(new Rectangle2D.Double(0, 0, pgsize.getWidth(), pgsize.getHeight()));
    slide.addShape(group);
    HSLFPictureData data = ss.addPicture(_slTests.readFile("clock.jpg"), PictureType.JPEG);
    HSLFPictureShape pict = new HSLFPictureShape(data, group);
    pict.setAnchor(new Rectangle2D.Double(0, 0, 200, 200));
    group.addShape(pict);
    HSLFLine line = new HSLFLine(group);
    line.setAnchor(new Rectangle2D.Double(300, 300, 500, 0));
    group.addShape(line);
    //serialize and read again.
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ss.write(out);
    out.close();
    ss.close();
    ByteArrayInputStream is = new ByteArrayInputStream(out.toByteArray());
    ss = new HSLFSlideShow(is);
    is.close();
    slide = ss.getSlides().get(0);
    List<HSLFShape> shape = slide.getShapes();
    assertEquals(1, shape.size());
    assertTrue(shape.get(0) instanceof HSLFGroupShape);
    group = (HSLFGroupShape) shape.get(0);
    List<HSLFShape> grshape = group.getShapes();
    assertEquals(2, grshape.size());
    assertTrue(grshape.get(0) instanceof HSLFPictureShape);
    assertTrue(grshape.get(1) instanceof HSLFLine);
    pict = (HSLFPictureShape) grshape.get(0);
    assertEquals(new Rectangle2D.Double(0, 0, 200, 200), pict.getAnchor());
    line = (HSLFLine) grshape.get(1);
    assertEquals(new Rectangle2D.Double(300, 300, 500, 0), line.getAnchor());
    ss.close();
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) Dimension(java.awt.Dimension) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFGroupShape(org.apache.poi.hslf.usermodel.HSLFGroupShape) HSLFLine(org.apache.poi.hslf.usermodel.HSLFLine) HSLFPictureShape(org.apache.poi.hslf.usermodel.HSLFPictureShape) HSLFShape(org.apache.poi.hslf.usermodel.HSLFShape) ByteArrayInputStream(java.io.ByteArrayInputStream) HSLFPictureData(org.apache.poi.hslf.usermodel.HSLFPictureData) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) Test(org.junit.Test)

Example 10 with HSLFPictureData

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

the class TestOleEmbedding method testEmbedding.

@Test
public void testEmbedding() throws IOException {
    HSLFSlideShow ppt = new HSLFSlideShow();
    File pict = POIDataSamples.getSlideShowInstance().getFile("clock.jpg");
    HSLFPictureData pictData = ppt.addPicture(pict, PictureType.JPEG);
    InputStream is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("Employee.xls");
    POIFSFileSystem poiData1 = new POIFSFileSystem(is);
    is.close();
    int oleObjectId1 = ppt.addEmbed(poiData1);
    HSLFSlide slide1 = ppt.createSlide();
    OLEShape oleShape1 = new OLEShape(pictData);
    oleShape1.setObjectID(oleObjectId1);
    slide1.addShape(oleShape1);
    oleShape1.setAnchor(new Rectangle2D.Double(100, 100, 100, 100));
    // add second slide with different order in object creation
    HSLFSlide slide2 = ppt.createSlide();
    OLEShape oleShape2 = new OLEShape(pictData);
    is = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SimpleWithImages.xls");
    POIFSFileSystem poiData2 = new POIFSFileSystem(is);
    is.close();
    int oleObjectId2 = ppt.addEmbed(poiData2);
    oleShape2.setObjectID(oleObjectId2);
    slide2.addShape(oleShape2);
    oleShape2.setAnchor(new Rectangle2D.Double(100, 100, 100, 100));
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ppt.write(bos);
    ppt = new HSLFSlideShow(new ByteArrayInputStream(bos.toByteArray()));
    OLEShape comp = (OLEShape) ppt.getSlides().get(0).getShapes().get(0);
    byte[] compData = IOUtils.toByteArray(comp.getObjectData().getData());
    bos.reset();
    poiData1.writeFilesystem(bos);
    byte[] expData = bos.toByteArray();
    assertArrayEquals(expData, compData);
    poiData1.close();
    poiData2.close();
    ppt.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Rectangle2D(java.awt.geom.Rectangle2D) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) HSLFPictureData(org.apache.poi.hslf.usermodel.HSLFPictureData) File(java.io.File) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) 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