#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
This commit is contained in:
Andreas Beeker 2015-11-29 00:51:04 +00:00
parent 8c1517eee4
commit 0c7e8e0c47
4 changed files with 54 additions and 8 deletions

View File

@ -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<XSLFShape,XSLFTextParagraph> {
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);
}

View File

@ -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;

View 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);
}
}

View File

@ -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();
}
}