From 9e45a9d9fd0fa25759c49f85d3bd9b4b09aa3579 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Sun, 29 Nov 2015 00:51:04 +0000 Subject: [PATCH] #58663 - Pictures cannot be removed from a slide git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1717018 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xslf/usermodel/XSLFGroupShape.java | 8 +++++ .../poi/xslf/usermodel/XSLFPictureShape.java | 8 +++-- .../apache/poi/xslf/usermodel/XSLFSheet.java | 15 ++++++++- .../xslf/usermodel/TestXSLFPictureShape.java | 31 ++++++++++++++++--- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFGroupShape.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFGroupShape.java index b3aba60ddb..93ca5ed0e6 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFGroupShape.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFGroupShape.java @@ -43,6 +43,7 @@ import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D; import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector; import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape; import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShapeNonVisual; +import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture; import org.openxmlformats.schemas.presentationml.x2006.main.CTShape; /** @@ -173,6 +174,13 @@ implements XSLFShapeContainer, GroupShape { grpSp.getGrpSpList().remove(obj); } else if (obj instanceof CTConnector){ grpSp.getCxnSpList().remove(obj); + } else if (obj instanceof CTPicture) { + XSLFPictureShape ps = (XSLFPictureShape)xShape; + XSLFSheet sh = getSheet(); + if (sh != null) { + sh.removePictureRelation(ps); + } + grpSp.getPicList().remove(obj); } else { throw new IllegalArgumentException("Unsupported shape: " + xShape); } diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java index 44de46e1ed..48c8babddf 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java @@ -144,16 +144,18 @@ public class XSLFPictureShape extends XSLFSimpleShape return null; } - private CTBlip getBlip(){ + protected CTBlip getBlip(){ CTPicture ct = (CTPicture)getXmlObject(); return ct.getBlipFill().getBlip(); } - private String getBlipLink(){ + + protected String getBlipLink(){ String link = getBlip().getLink(); if (link.isEmpty()) return null; return link; } - private String getBlipId(){ + + protected String getBlipId(){ String id = getBlip().getEmbed(); if (id.isEmpty()) return null; return id; diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java index f999c6c4b6..d8d9cd3b60 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java @@ -49,7 +49,6 @@ import org.apache.poi.util.IOUtils; import org.apache.poi.util.Internal; import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.XmlOptions; -import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelationshipId; import org.openxmlformats.schemas.presentationml.x2006.main.CTCommonSlideData; import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector; import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame; @@ -283,6 +282,10 @@ implements XSLFShapeContainer, Sheet { spTree.getGrpSpList().remove(obj); } else if (obj instanceof CTConnector){ spTree.getCxnSpList().remove(obj); + } else if (obj instanceof CTPicture) { + XSLFPictureShape ps = (XSLFPictureShape)xShape; + removePictureRelation(ps); + spTree.getPicList().remove(obj); } else { throw new IllegalArgumentException("Unsupported shape: " + xShape); } @@ -586,4 +589,14 @@ implements XSLFShapeContainer, Sheet { } return part; } + + /** + * Helper method for sheet and group shapes + * + * @param pictureShape the picture shapes whose relation is to be removed + */ + void removePictureRelation(XSLFPictureShape pictureShape) { + POIXMLDocumentPart pd = getRelationById(pictureShape.getBlipId()); + removeRelation(pd); + } } \ No newline at end of file diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java index b5acb85291..2d7402f714 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java @@ -20,20 +20,24 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; -import org.apache.poi.xslf.XSLFTestDataSamples; +import org.apache.poi.POIDataSamples; import org.apache.poi.sl.usermodel.PictureData.PictureType; +import org.apache.poi.xslf.XSLFTestDataSamples; import org.junit.Test; import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture; -/** - * @author Yegor Kozlov - */ public class TestXSLFPictureShape { + private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance(); @Test public void testCreate() throws Exception { @@ -170,4 +174,23 @@ public class TestXSLFPictureShape { ppt1.close(); ppt2.close(); } + + @Test + public void bug58663() throws IOException { + InputStream is = _slTests.openResourceAsStream("shapes.pptx"); + XMLSlideShow ppt = new XMLSlideShow(is); + is.close(); + + XSLFSlide slide = ppt.getSlides().get(0); + XSLFPictureShape ps = (XSLFPictureShape)slide.getShapes().get(3); + slide.removeShape(ps); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ppt.write(bos); + ppt.close(); + + XMLSlideShow ppt2 = new XMLSlideShow(new ByteArrayInputStream(bos.toByteArray())); + assertTrue(ppt2.getPictureData().isEmpty()); + ppt2.close(); + } } \ No newline at end of file -- 2.39.5