Attach 3 images in single slide at specified posit

2019-02-28 10:58发布

I need to paste 3 pictures in single slide using Apache POI XSLF. However I could able to add only one picture in a slide. Also I could not find any ways to specify the size and orientation the picture should be.

Tried the following code

    XMLSlideShow ppt = new XMLSlideShow();
    XSLFSlide slide = ppt.createSlide();
    XSLFGroupShape group1 = slide.createGroup();
    byte buf[] = new byte[1024];

    for (int i = 1; i <= 2; i++) {
        byte[] pictureData = IOUtils.toByteArray(new FileInputStream(
                "C:\\Users\\Ashok\\Pictures\\" + i + ".png"));
        int elementIndex = ppt.addPicture(pictureData,
                XSLFPictureData.PICTURE_TYPE_PNG);
        XSLFPictureShape picture = slide.createPicture(elementIndex);
        List<XSLFPictureData> allPictures = ppt.getAllPictures();
        System.out.println(allPictures.size());
    }
    FileOutputStream fos = new FileOutputStream("C:\\test2.pptx");
    ppt.write(fos);
    fos.flush();
    fos.close();

The above code contains only the last image.

1条回答
beautiful°
2楼-- · 2019-02-28 11:15

You nead to set Anchor to your pictures

for (int i = 1; i <= 2; i++) {
    byte[] pictureData = IOUtils.toByteArray(new FileInputStream(
            "C:\\Users\\Ashok\\Pictures\\" + i + ".png"));
    int elementIndex = ppt.addPicture(pictureData,
            XSLFPictureData.PICTURE_TYPE_PNG);
    XSLFPictureShape picture = slide.createPicture(elementIndex);

    // Set picture position and size
    picture.setAnchor(new Rectangle(positionX, positionY, width, height));

    List<XSLFPictureData> allPictures = ppt.getAllPictures();
    System.out.println(allPictures.size());
}
查看更多
登录 后发表回答