]> source.dussan.org Git - poi.git/commitdiff
#58663 - Pictures cannot be removed from a slide
authorAndreas Beeker <kiwiwings@apache.org>
Sun, 29 Nov 2015 00:51:04 +0000 (00:51 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Sun, 29 Nov 2015 00:51:04 +0000 (00:51 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1717018 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFGroupShape.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java
src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java

index b3aba60ddb084090e72178c1e121d2a226edc8b8..93ca5ed0e6775aeda8b2892853f716b587f4fa12 100644 (file)
@@ -43,6 +43,7 @@ import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
 import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector;\r
 import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;\r
 import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShapeNonVisual;\r
+import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;\r
 import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;\r
 \r
 /**\r
@@ -173,6 +174,13 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
             grpSp.getGrpSpList().remove(obj);\r
         } else if (obj instanceof CTConnector){\r
             grpSp.getCxnSpList().remove(obj);\r
+        } else if (obj instanceof CTPicture) {\r
+            XSLFPictureShape ps = (XSLFPictureShape)xShape;\r
+            XSLFSheet sh = getSheet();\r
+            if (sh != null) {\r
+                sh.removePictureRelation(ps);\r
+            }\r
+            grpSp.getPicList().remove(obj);\r
         } else {\r
             throw new IllegalArgumentException("Unsupported shape: " + xShape);\r
         }\r
index 44de46e1edffbdb005d2b313852ceba7dd8df3b1..48c8babddf99ccf35ac6f3ec0a2e760095d8a579 100644 (file)
@@ -144,16 +144,18 @@ public class XSLFPictureShape extends XSLFSimpleShape
         return null;\r
     }\r
 \r
-    private CTBlip getBlip(){\r
+    protected CTBlip getBlip(){\r
         CTPicture ct = (CTPicture)getXmlObject();\r
         return ct.getBlipFill().getBlip();\r
     }\r
-    private String getBlipLink(){\r
+    \r
+    protected String getBlipLink(){\r
         String link = getBlip().getLink();\r
         if (link.isEmpty()) return null;\r
         return link;\r
     }\r
-    private String getBlipId(){\r
+    \r
+    protected String getBlipId(){\r
         String id = getBlip().getEmbed();\r
         if (id.isEmpty()) return null;\r
         return id;\r
index f999c6c4b60c97c8224a11c6154d3611b9bf67e0..d8d9cd3b601999ae442525f9ba6561d5edb11b3a 100644 (file)
@@ -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<XSLFShape,XSLFTextParagraph> {
             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<XSLFShape,XSLFTextParagraph> {
         }
         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
index b5acb85291d67629e62533d3c0d9424aac729084..2d7402f714d1a5b3d4e3dd4fc860e907ffcd7e5d 100644 (file)
@@ -20,20 +20,24 @@ import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;\r
 import static org.junit.Assert.assertFalse;\r
 import static org.junit.Assert.assertNotNull;\r
+import static org.junit.Assert.assertTrue;\r
 \r
+import java.io.ByteArrayInputStream;\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
 import java.util.HashMap;\r
 import java.util.List;\r
 import java.util.Map;\r
 \r
-import org.apache.poi.xslf.XSLFTestDataSamples;\r
+import org.apache.poi.POIDataSamples;\r
 import org.apache.poi.sl.usermodel.PictureData.PictureType;\r
+import org.apache.poi.xslf.XSLFTestDataSamples;\r
 import org.junit.Test;\r
 import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;\r
 \r
-/**\r
- * @author Yegor Kozlov\r
- */\r
 public class TestXSLFPictureShape {\r
+    private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();\r
 \r
     @Test\r
     public void testCreate() throws Exception {\r
@@ -170,4 +174,23 @@ public class TestXSLFPictureShape {
         ppt1.close();\r
         ppt2.close();\r
     }\r
+    \r
+    @Test\r
+    public void bug58663() throws IOException {\r
+        InputStream is = _slTests.openResourceAsStream("shapes.pptx");\r
+        XMLSlideShow ppt = new XMLSlideShow(is);\r
+        is.close();\r
+        \r
+        XSLFSlide slide = ppt.getSlides().get(0);\r
+        XSLFPictureShape ps = (XSLFPictureShape)slide.getShapes().get(3);\r
+        slide.removeShape(ps);\r
+        \r
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();\r
+        ppt.write(bos);\r
+        ppt.close();\r
+        \r
+        XMLSlideShow ppt2 = new XMLSlideShow(new ByteArrayInputStream(bos.toByteArray()));\r
+        assertTrue(ppt2.getPictureData().isEmpty());\r
+        ppt2.close();\r
+    }\r
 }
\ No newline at end of file