Example usage for org.apache.poi.xslf.usermodel XMLSlideShow addPicture

List of usage examples for org.apache.poi.xslf.usermodel XMLSlideShow addPicture

Introduction

In this page you can find the example usage for org.apache.poi.xslf.usermodel XMLSlideShow addPicture.

Prototype

@Override
public XSLFPictureData addPicture(File pict, PictureType format) throws IOException 

Source Link

Document

Adds a picture to the presentation.

Usage

From source file:AddAudioToPptx.java

License:Apache License

public static void main(String[] args) throws Exception {
    URL video = new URL("http://archive.org/download/test-mpeg/test-mpeg.mpg");
    // URL video = new URL("file:test-mpeg.mpg");

    XMLSlideShow pptx = new XMLSlideShow();

    // add video file
    String videoFileName = "lego_edsheeran.mp3";
    PackagePartName partName = PackagingURIHelper.createPartName("/ppt/media/" + videoFileName);
    PackagePart part = pptx.getPackage().createPart(partName, "audio/mpeg");
    OutputStream partOs = part.getOutputStream();
    //InputStream fis = video.openStream();
    FileInputStream fis = new FileInputStream(videoFileName);
    byte buf[] = new byte[1024];
    for (int readBytes; (readBytes = fis.read(buf)) != -1; partOs.write(buf, 0, readBytes))
        ;//from   ww  w. j a  va  2 s .  c o m
    fis.close();
    partOs.close();

    XSLFSlide slide1 = pptx.createSlide();

    byte[] picture = IOUtils.toByteArray(new FileInputStream("audio.png"));

    //adding the image to the presentation
    XSLFPictureData idx = pptx.addPicture(picture, XSLFPictureData.PictureType.PNG);

    //creating a slide with given picture on it
    XSLFPictureShape pv1 = slide1.createPicture(idx);

    //XSLFPictureShape pv1 = // addPreview(pptx, slide1, part, 5, 50, 80);
    addAudio(pptx, slide1, part, pv1, 5);
    addTimingInfo(slide1, pv1);
    //XSLFPictureShape pv2 = addPreview(pptx, slide1, part, 9, 50, 250);
    //addVideo(pptx, slide1, part, pv2, 9);
    //addTimingInfo(slide1, pv2);

    FileOutputStream fos = new FileOutputStream("pptx-with-audio.pptx");
    pptx.write(fos);
    fos.close();

    pptx.close();
}

From source file:AddAudioToPptx.java

License:Apache License

static XSLFPictureShape addPreview(XMLSlideShow pptx, XSLFSlide slide1, PackagePart videoPart, double seconds,
        int x, int y) throws IOException {
    // get preview after 5 sec.
    IContainer ic = IContainer.make();// w w  w .j  a v a 2 s  .c om
    InputOutputStreamHandler iosh = new InputOutputStreamHandler(videoPart.getInputStream());
    if (ic.open(iosh, IContainer.Type.READ, null) < 0)
        return null;

    IMediaReader mediaReader = ToolFactory.makeReader(ic);

    // stipulate that we want BufferedImages created in BGR 24bit color space
    mediaReader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR);

    ImageSnapListener isl = new ImageSnapListener(seconds);
    mediaReader.addListener(isl);

    // read out the contents of the media file and
    // dispatch events to the attached listener
    while (!isl.hasFired && mediaReader.readPacket() == null)
        ;

    mediaReader.close();
    ic.close();

    // add snapshot
    BufferedImage image1 = isl.image;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ImageIO.write(image1, "jpeg", bos);
    XSLFPictureData snap = pptx.addPicture(bos.toByteArray(), PictureType.JPEG);
    XSLFPictureShape pic1 = slide1.createPicture(snap);
    pic1.setAnchor(new Rectangle(x, y, image1.getWidth(), image1.getHeight()));
    return pic1;
}

From source file:com.hp.autonomy.frontend.reports.powerpoint.PowerPointServiceImpl.java

License:MIT License

/**
 * Utility function to add image data to a PowerPoint presentation.
 * @param imageSource the image source./*from w w w. j a v  a 2 s .c  om*/
 * @param ppt the presentation to add to.
 * @param imageId the image identifier, typically a URI.
 * @return the picture data.
 */
private static XSLFPictureData addPictureData(final ImageSource imageSource, final XMLSlideShow ppt,
        final String imageId) {
    final ImageData imageData = imageSource.getImageData(imageId);
    return ppt.addPicture(imageData.getData(), imageData.getType());
}

From source file:org.maptalks.poi.animation.TestXSLFAnimationGroup.java

License:Apache License

@Test
public void testAnimationGroup() throws Exception {
    XMLSlideShow pptx = new XMLSlideShow();
    XSLFSlide slide = pptx.createSlide();
    Dimension pageSize = pptx.getPageSize();
    //        Rectangle2D rectangle = new Rectangle2D.Double(0,0,pageSize.getWidth(),pageSize.getHeight());
    Rectangle rectangle = new java.awt.Rectangle(0, 0, (int) pageSize.getWidth(), (int) pageSize.getHeight());
    List<XSLFAnimationType> animationTypes = new ArrayList<XSLFAnimationType>();
    XSLFAnimation animation = new XSLFAnimation();
    String[] directions = new String[5];
    directions[0] = MoveDirection.BOTTOM;
    directions[1] = MoveDirection.LEFT;//from  ww  w.j  a va 2  s. co m
    directions[2] = MoveDirection.TOP;
    directions[3] = MoveDirection.RIGHT;
    directions[4] = MoveDirection.BOTTOM;
    String pathStr = this.getClass().getResource("/images").getPath();
    String nodeType = "clickEffect";
    for (int i = 0; i < 3; i++) {
        String fileName = pathStr + "/" + (i + 1) + ".png";
        File downloadeFile = new File(fileName);
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(downloadeFile));
        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
        int size = 0;
        byte[] temp = new byte[1024];
        while ((size = in.read(temp)) != -1) {
            out.write(temp, 0, size);
        }
        in.close();
        byte[] content = out.toByteArray();
        XSLFPictureData pictureData = pptx.addPicture(content,
                org.apache.poi.sl.usermodel.PictureData.PictureType.PNG);
        XSLFPictureShape picShape = slide.createPicture(pictureData);
        picShape.setAnchor(rectangle);
        picShape.setLineWidth(0);

        if (i > 1) {
            nodeType = "afterEffect";
            XSLFAnimationType animationType = new FlyIn(picShape, directions[i], nodeType);
            animationTypes.get(1).addChild(animationType);
        } else {
            XSLFAnimationType animationType = new FlyIn(picShape, directions[i], nodeType);
            animationTypes.add(animationType);
        }
    }
    animation.addAnimationToSlide(slide, animationTypes);
    FileOutputStream out = new FileOutputStream("d:\\group.pptx");
    //        FileOutputStream out = new FileOutputStream(pathStr+"/group.pptx");
    pptx.write(out);
    out.close();
}

From source file:org.maptalks.poi.animation.TestXSLFAnimationShape.java

License:Apache License

@Test
public void testAnimationSlide() throws Exception {
    XMLSlideShow pptx = new XMLSlideShow();
    XSLFSlide slide = pptx.createSlide();
    Dimension pageSize = pptx.getPageSize();
    //        Rectangle2D rectangle = new Rectangle2D.Double(0,0,pageSize.getWidth(),pageSize.getHeight());
    Rectangle rectangle = new java.awt.Rectangle(0, 0, (int) pageSize.getWidth(), (int) pageSize.getHeight());
    List<XSLFAnimationType> animationTypes = new ArrayList<XSLFAnimationType>();
    XSLFAnimation animation = new XSLFAnimation();
    String[] directions = new String[5];
    directions[0] = MoveDirection.BOTTOM;
    directions[1] = MoveDirection.LEFT;//from  w  w  w. jav  a 2  s.  c  om
    directions[2] = MoveDirection.TOP;
    directions[3] = MoveDirection.RIGHT;
    directions[4] = MoveDirection.BOTTOM;
    String pathStr = this.getClass().getResource("/images").getPath();
    for (int i = 0; i < 3; i++) {
        String fileName = pathStr + "/" + (i + 1) + ".png";
        File downloadeFile = new File(fileName);
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(downloadeFile));
        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
        int size = 0;
        byte[] temp = new byte[1024];
        while ((size = in.read(temp)) != -1) {
            out.write(temp, 0, size);
        }
        in.close();
        byte[] content = out.toByteArray();
        XSLFPictureData pictureData = pptx.addPicture(content,
                org.apache.poi.sl.usermodel.PictureData.PictureType.PNG);
        XSLFPictureShape picShape = slide.createPicture(pictureData);
        picShape.setAnchor(rectangle);
        picShape.setLineWidth(0);
        XSLFAnimationType animationType = new FlyIn(picShape, directions[i]);
        animationTypes.add(animationType);

        XSLFAnimationType animationOutType = new FlyOut(picShape, directions[i]);
        animationTypes.add(animationOutType);
    }
    slide = animation.addAnimationToSlide(slide, animationTypes);
    FileOutputStream out = new FileOutputStream("d:\\temp.pptx");
    //        FileOutputStream out = new FileOutputStream(pathStr+"/temp.pptx");
    pptx.write(out);
    out.close();
}

From source file:org.maptalks.poi.animation.TestZoomIn.java

License:Apache License

@Test
public void testAnimationSlide() throws Exception {
    XMLSlideShow pptx = new XMLSlideShow();
    XSLFSlide slide = pptx.createSlide();
    Dimension pageSize = pptx.getPageSize();
    Rectangle rectangle = new java.awt.Rectangle(0, 0, (int) pageSize.getWidth(), (int) pageSize.getHeight());
    //        Rectangle2D rectangle = new Rectangle2D.Double();
    List<XSLFAnimationType> animationTypes = new ArrayList<XSLFAnimationType>();
    XSLFAnimation animation = new XSLFAnimation();
    String[] directions = new String[5];
    directions[0] = MoveDirection.BOTTOM;
    directions[1] = MoveDirection.LEFT;/*from   w w w  .j av a2  s. c  om*/
    directions[2] = MoveDirection.TOP;
    directions[3] = MoveDirection.RIGHT;
    directions[4] = MoveDirection.BOTTOM;
    String pathStr = this.getClass().getResource("/images").getPath();
    for (int i = 0; i < 3; i++) {
        String fileName = pathStr + "/" + (i + 1) + ".png";
        File downloadeFile = new File(fileName);
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(downloadeFile));
        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
        int size = 0;
        byte[] temp = new byte[1024];
        while ((size = in.read(temp)) != -1) {
            out.write(temp, 0, size);
        }
        in.close();
        byte[] content = out.toByteArray();
        XSLFPictureData pictureData = pptx.addPicture(content,
                org.apache.poi.sl.usermodel.PictureData.PictureType.PNG);
        XSLFPictureShape picShape = slide.createPicture(pictureData);
        picShape.setAnchor(rectangle);
        picShape.setLineWidth(0);
        XSLFAnimationType animationType = new ZoomIn(picShape, directions[i]);
        animationTypes.add(animationType);

        XSLFAnimationType animationOutType = new FlyOut(picShape, directions[i]);
        animationTypes.add(animationOutType);
    }
    slide = animation.addAnimationToSlide(slide, animationTypes);
    FileOutputStream out = new FileOutputStream("d:\\zoomin.pptx");
    //        FileOutputStream out = new FileOutputStream(pathStr+"/zoomin.pptx");
    pptx.write(out);
    out.close();
}

From source file:org.maptalks.poi.animation.TestZoomOut.java

License:Apache License

@Test
public void testAnimationSlide() throws Exception {
    XMLSlideShow pptx = new XMLSlideShow();
    XSLFSlide slide = pptx.createSlide();
    Dimension pageSize = pptx.getPageSize();
    //        Rectangle2D rectangle = new Rectangle2D.Double(0,0,pageSize.getWidth(),pageSize.getHeight());
    Rectangle rectangle = new java.awt.Rectangle(0, 0, (int) pageSize.getWidth(), (int) pageSize.getHeight());
    List<XSLFAnimationType> animationTypes = new ArrayList<XSLFAnimationType>();
    XSLFAnimation animation = new XSLFAnimation();
    String[] directions = new String[5];
    directions[0] = MoveDirection.BOTTOM;
    directions[1] = MoveDirection.LEFT;//from w  w  w.j  a  v a 2s .c  o m
    directions[2] = MoveDirection.TOP;
    directions[3] = MoveDirection.RIGHT;
    directions[4] = MoveDirection.BOTTOM;
    String pathStr = this.getClass().getResource("/images").getPath();
    for (int i = 0; i < 3; i++) {
        String fileName = pathStr + "/" + (i + 1) + ".png";
        File downloadeFile = new File(fileName);
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(downloadeFile));
        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
        int size = 0;
        byte[] temp = new byte[1024];
        while ((size = in.read(temp)) != -1) {
            out.write(temp, 0, size);
        }
        in.close();
        byte[] content = out.toByteArray();
        XSLFPictureData pictureData = pptx.addPicture(content,
                org.apache.poi.sl.usermodel.PictureData.PictureType.PNG);
        XSLFPictureShape picShape = slide.createPicture(pictureData);
        picShape.setAnchor(rectangle);
        picShape.setLineWidth(0);
        XSLFAnimationType animationType = new ZoomIn(picShape, directions[i]);
        animationTypes.add(animationType);

        XSLFAnimationType animationOutType = new ZoomOut(picShape, directions[i]);
        animationTypes.add(animationOutType);
    }
    slide = animation.addAnimationToSlide(slide, animationTypes);
    FileOutputStream out = new FileOutputStream("d:\\zoomout.pptx");
    //        FileOutputStream out = new FileOutputStream(pathStr+"/zoomout.pptx");
    pptx.write(out);
    out.close();
}

From source file:poi.xslf.usermodel.Tutorial5.java

License:Apache License

public static void main(String[] args) throws IOException {
    XMLSlideShow ppt = new XMLSlideShow();

    XSLFSlide slide = ppt.createSlide();
    File img = new File(System.getProperty("POI.testdata.path"), "slideshow/clock.jpg");
    byte[] data = IOUtils.toByteArray(new FileInputStream(img));
    int pictureIndex = ppt.addPicture(data, XSLFPictureData.PICTURE_TYPE_PNG);

    XSLFPictureShape shape = slide.createPicture(pictureIndex);

    FileOutputStream out = new FileOutputStream("images.pptx");
    ppt.write(out);//from   w  w w  .  java 2s  .  com
    out.close();
}