This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/trunk by this push:
new 545dca7379 XSLF: drop removed images from the picture list in
getPictureData (#1553)
545dca7379 is described below
commit 545dca7379b224554ca49cd1e1fae173a83b74c0
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Nov 9 08:03:18 2026 +0100
XSLF: drop removed images from the picture list in getPictureData (#1553)
Removing a picture shape can remove its image part, but the deck's
picture list kept the entry. addPicture with the same bytes then
returned it, and the new shape linked to a part missing from the
written file. getPictureData now drops entries whose part is no longer
in the package, which covers every removal path, so removeSlide no
longer needs its own check.
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../org/apache/poi/xslf/usermodel/XMLSlideShow.java | 6 +++---
.../poi/xslf/usermodel/TestXSLFPictureShape.java | 21 +++++++++++++++++++++
2 files changed, 24 insertions(+), 3 deletions(-)
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java
b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java
index 00f0d36028..e84d19dd7c 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java
@@ -256,6 +256,9 @@ public class XMLSlideShow extends POIXMLDocument
@Override
public List<XSLFPictureData> getPictureData() {
+ // removing a slide or shape can remove image parts from the package,
and the list
+ // holds its own instances, so drop the entries by part rather than by
instance
+ _pictures.removeIf(p ->
!getPackage().containPart(p.getPackagePart().getPartName()));
if (_pictures.isEmpty()) {
getPackage().getPartsByName(GET_PICTURE_DATA_PATTERN).forEach(part
-> {
XSLFPictureData pd = new XSLFPictureData(part);
@@ -521,9 +524,6 @@ public class XMLSlideShow extends POIXMLDocument
// removing the slide also removes the parts which only it used,
// so the relations above need to be handled first
removeRelation(slide);
- // the picture list holds its own instances, so drop them by part,
- // which also covers images reached through other parts, e.g. SmartArt
- _pictures.removeIf(p ->
!getPackage().containPart(p.getPackagePart().getPartName()));
return slide;
}
diff --git
a/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java
b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java
index 48c9161501..8d9f77ef70 100644
---
a/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java
+++
b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java
@@ -289,6 +289,27 @@ class TestXSLFPictureShape {
}
}
+ @Test
+ void testAddPictureAfterRemoveShape() throws IOException {
+ try (XMLSlideShow ppt = new XMLSlideShow()) {
+ byte[] data = new byte[]{1, 2, 3};
+ XSLFSlide slide = ppt.createSlide();
+ XSLFPictureShape ps = slide.createPicture(ppt.addPicture(data,
PictureType.PNG));
+
+ // removing the only shape using the image also removes the image
part
+ slide.removeShape(ps);
+ assertTrue(ppt.getPictureData().isEmpty());
+
+ // adding the same image again creates a new part instead of
returning the removed one
+ slide.createPicture(ppt.addPicture(data, PictureType.PNG));
+ try (XMLSlideShow ppt2 = writeOutAndReadBack(ppt)) {
+ XSLFPictureShape pic = (XSLFPictureShape)
ppt2.getSlides().get(0).getShapes().get(0);
+ assertArrayEquals(data, pic.getPictureData().getData());
+ assertEquals(1, ppt2.getPictureData().size());
+ }
+ }
+ }
+
@Test
void testTiffImageBug59742() throws Exception {
try (XMLSlideShow slideShow = new XMLSlideShow();
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]