使用Apache POI XSLF在指定位置的单张幻灯片中附加3张图像

时间:2015-04-20 10:36:07

标签: java image apache-poi powerpoint xslf

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

尝试以下代码

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

以上代码仅包含最后一张图片。

1 个答案:

答案 0 :(得分:2)

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

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