diff options
author | PJ Fanning <fanningpj@apache.org> | 2021-10-23 09:27:35 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2021-10-23 09:27:35 +0000 |
commit | 802165ecd0cf8ae6590e751ce7d02c13ca3919a9 (patch) | |
tree | b43cc87d3a47bda14b0cd568b93fa9880d871ca5 /poi-ooxml | |
parent | 03fc88b18b0a8ecab6ecaa2c8a877d5ad24a28fb (diff) | |
download | poi-802165ecd0cf8ae6590e751ce7d02c13ca3919a9.tar.gz poi-802165ecd0cf8ae6590e751ce7d02c13ca3919a9.zip |
add XSLFPictureShape setName
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894505 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi-ooxml')
-rw-r--r-- | poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java | 37 | ||||
-rw-r--r-- | poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFPictureShape.java | 48 |
2 files changed, 80 insertions, 5 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java index 9205a74112..c011c5db98 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java @@ -282,11 +282,11 @@ public class XSLFPictureShape extends XSLFSimpleShape XmlObject xmlObject = getXmlObject(); if (xmlObject instanceof CTPicture) { CTPicture ctPicture = (CTPicture)xmlObject; - CTPictureNonVisual nvSpPr = ctPicture.getNvPicPr(); - if (nvSpPr != null) { - CTNonVisualDrawingProps cnv = nvSpPr.getCNvPr(); - if (cnv != null) { - name = cnv.getName(); + CTPictureNonVisual nvPicPr = ctPicture.getNvPicPr(); + if (nvPicPr != null) { + CTNonVisualDrawingProps cnvdProps = nvPicPr.getCNvPr(); + if (cnvdProps != null) { + name = cnvdProps.getName(); } } } @@ -294,6 +294,33 @@ public class XSLFPictureShape extends XSLFSimpleShape } /** + * @param name picture name + * @return returns true if the name was set + * @since POI 5.1.0 + */ + public boolean setName(String name) { + XmlObject xmlObject = getXmlObject(); + if (xmlObject instanceof CTPicture) { + CTPicture ctPicture = (CTPicture)xmlObject; + CTPictureNonVisual nvPicPr = ctPicture.getNvPicPr(); + if (nvPicPr == null) { + nvPicPr = ctPicture.addNewNvPicPr(); + } + if (nvPicPr != null) { + CTNonVisualDrawingProps cnvdProps = nvPicPr.getCNvPr(); + if (cnvdProps == null) { + cnvdProps = nvPicPr.addNewCNvPr(); + } + if (cnvdProps != null) { + cnvdProps.setName(name); + return true; + } + } + } + return false; + } + + /** * @return SVG image data -- can return null if no SVG image is found */ public XSLFPictureData getSvgImage() { 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 79918590ef..b6cd26b9ee 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 @@ -95,6 +95,54 @@ class TestXSLFPictureShape { } @Test + void testCreateWithSetName() throws Exception { + try (XMLSlideShow ppt1 = new XMLSlideShow()) { + assertEquals(0, ppt1.getPictureData().size()); + byte[] data1 = new byte[100]; + for (int i = 0; i < 100; i++) { + data1[i] = (byte) i; + } + XSLFPictureData pdata1 = ppt1.addPicture(data1, PictureType.JPEG); + assertEquals(0, pdata1.getIndex()); + assertEquals(1, ppt1.getPictureData().size()); + + XSLFSlide slide = ppt1.createSlide(); + XSLFPictureShape shape1 = slide.createPicture(pdata1); + assertNotNull(shape1.getPictureData()); + assertArrayEquals(data1, shape1.getPictureData().getData()); + assertTrue(shape1.setName("Shape1 Picture")); + + byte[] data2 = new byte[200]; + for (int i = 0; i < 200; i++) { + data2[i] = (byte) i; + } + XSLFPictureData pdata2 = ppt1.addPicture(data2, PictureType.PNG); + XSLFPictureShape shape2 = slide.createPicture(pdata2); + assertNotNull(shape2.getPictureData()); + assertEquals(1, pdata2.getIndex()); + assertEquals(2, ppt1.getPictureData().size()); + assertArrayEquals(data2, shape2.getPictureData().getData()); + assertTrue(shape2.setName("Shape2 Picture")); + + try (XMLSlideShow ppt2 = writeOutAndReadBack(ppt1)) { + List<XSLFPictureData> pics = ppt2.getPictureData(); + assertEquals(2, pics.size()); + assertArrayEquals(data1, pics.get(0).getData()); + assertArrayEquals(data2, pics.get(1).getData()); + + List<XSLFShape> shapes = ppt2.getSlides().get(0).getShapes(); + assertEquals(2, shapes.size()); + XSLFPictureShape xlsfShape1 = (XSLFPictureShape) shapes.get(0); + XSLFPictureShape xlsfShape2 = (XSLFPictureShape) shapes.get(1); + assertArrayEquals(data1, xlsfShape1.getPictureData().getData()); + assertArrayEquals(data2, xlsfShape2.getPictureData().getData()); + assertEquals("Shape1 Picture", xlsfShape1.getName()); + assertEquals("Shape2 Picture", xlsfShape2.getName()); + } + } + } + + @Test void testCreateMultiplePictures() throws Exception { try (XMLSlideShow ppt1 = new XMLSlideShow()) { XSLFSlide slide1 = ppt1.createSlide(); |