Attach 3 images in single slide at specified positions using Apache POI XSLF

我怕爱的太早我们不能终老 提交于 2019-12-02 12:56:18

问题


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:


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());
}


来源:https://stackoverflow.com/questions/29745516/attach-3-images-in-single-slide-at-specified-positions-using-apache-poi-xslf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!