简体   繁体   English

Apache POI XSLF用另一个图像替换图像

[英]Apache POI XSLF replace image with another image

I'm using Apache POI as a way to template out a layout with placeholder shapes I replace with contents I retrieve from a restful call. 我使用Apache POI作为模板来布局占位符形状的布局,而该占位符形状将替换为我从宁静调用中检索的内容。

All of this is working fine, but I can't find a way to replace an image with another image. 所有这些都工作正常,但是我找不到用另一张图片替换一张图片的方法。

I know it is possible to create and add it to a slide using the following code: 我知道可以使用以下代码创建并将其添加到幻灯片中:

int idx = ui.getPpt().addPicture(bytes, XSLFPictureData.PICTURE_TYPE_PNG);
XSLFPictureShape pic = slide.createPicture(idx);

This all works but what I'd like to do is replace an existing image but can't seem to find a way to do it as I can with tables and text objects. 所有这些都可以,但是我想做的是替换现有图像,但是似乎找不到像使用表格和文本对象一样的方法。

Does anyone have any ideas? 有人有什么想法吗?

First read the slide and get the shape which contains the picture. 首先阅读幻灯片并获得包含图片的形状。 The shape will be an instance of XSLFPictureShape . 该形状将是XSLFPictureShape一个实例。 Use the following code: 使用以下代码:

for(XSLFShape shape : slide.getShapes()) {

   XSLFPictureShape image = (XSLFPictureShape) shape;
   image.getPictureData().setData(IOUtils.toByteArray(new  FileInputStream("path_to_image"));

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM