本文介绍了使用Apache POI XSLF在指定位置的单张幻灯片中附加3张图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Apache POI XSLF在单张幻灯片中粘贴3张图片。但是,我只能在幻灯片中添加一张图片。此外,我找不到任何方法来指定图片的大小和方向。

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.

尝试以下代码

    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.

推荐答案

你需要将Anchor设置为你的图片

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

这篇关于使用Apache POI XSLF在指定位置的单张幻灯片中附加3张图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 23:17