1

我需要使用 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();

上面的代码只包含最后一张图片。

4

1 回答 1

3

您需要为您的图片设置锚点

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());
}
于 2016-05-12T15:16:10.063 回答