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 c011c5db98..5c839f40fe 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 @@ -279,15 +279,11 @@ public class XSLFPictureShape extends XSLFSimpleShape */ public String getName() { String name = null; - XmlObject xmlObject = getXmlObject(); - if (xmlObject instanceof CTPicture) { - CTPicture ctPicture = (CTPicture)xmlObject; - CTPictureNonVisual nvPicPr = ctPicture.getNvPicPr(); - if (nvPicPr != null) { - CTNonVisualDrawingProps cnvdProps = nvPicPr.getCNvPr(); - if (cnvdProps != null) { - name = cnvdProps.getName(); - } + CTPictureNonVisual nvPicPr = getCTPictureNonVisual(); + if (nvPicPr != null) { + CTNonVisualDrawingProps cnvdProps = nvPicPr.getCNvPr(); + if (cnvdProps != null) { + name = cnvdProps.getName(); } } return name; @@ -412,17 +408,17 @@ public class XSLFPictureShape extends XSLFSimpleShape } String relId = getSheet().importBlip(blipId, p.getSheet()); - - CTPicture ct = (CTPicture)getXmlObject(); CTBlip blip = getBlipFill().getBlip(); blip.setEmbed(relId); - CTApplicationNonVisualDrawingProps nvPr = ct.getNvPicPr().getNvPr(); - if(nvPr.isSetCustDataLst()) { + CTPictureNonVisual nvPicPr = getCTPictureNonVisual(); + CTApplicationNonVisualDrawingProps nvPr = nvPicPr == null ? null : nvPicPr.getNvPr(); + + if(nvPr != null && nvPr.isSetCustDataLst()) { // discard any custom tags associated with the picture being copied nvPr.unsetCustDataLst(); } - if(blip.isSetExtLst()) { + if (blip.isSetExtLst()) { // TODO: check for SVG copying CTOfficeArtExtensionList extLst = blip.getExtLst(); for(CTOfficeArtExtension ext : extLst.getExtArray()){ @@ -438,4 +434,45 @@ public class XSLFPictureShape extends XSLFSimpleShape } } } + + /** + * @return boolean; true if the picture is a video + * @since POI 5.2.0 + */ + public boolean isVideoFile() { + CTPictureNonVisual nvPicPr = getCTPictureNonVisual(); + if (nvPicPr != null) { + CTApplicationNonVisualDrawingProps nvPr = nvPicPr.getNvPr(); + if (nvPr != null) { + return nvPr.isSetVideoFile(); + } + } + return false; + } + + /** + * @return the link ID for the video file + * @since POI 5.2.0 + */ + public String getVideoFileLink() { + if (isVideoFile()) { + CTPictureNonVisual nvPicPr = getCTPictureNonVisual(); + if (nvPicPr != null) { + CTApplicationNonVisualDrawingProps nvPr = nvPicPr.getNvPr(); + if (nvPr != null && nvPr.getVideoFile() != null) { + return nvPr.getVideoFile().getLink(); + } + } + } + return null; + } + + private CTPictureNonVisual getCTPictureNonVisual() { + XmlObject xmlObject = getXmlObject(); + if (xmlObject instanceof CTPicture) { + CTPicture ctPicture = (CTPicture) xmlObject; + return ctPicture.getNvPicPr(); + } + return null; + } } \ No newline at end of file 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 b6cd26b9ee..65d18e9043 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 @@ -339,4 +339,24 @@ class TestXSLFPictureShape { } } } + + @Test + void testIsSetVideoFile() throws IOException { + try (XMLSlideShow ppt = openSampleDocument("EmbeddedVideo.pptx")) { + XSLFSlide slide = ppt.getSlides().get(0); + XSLFPictureShape ps = (XSLFPictureShape) slide.getShapes().get(0); + + assertTrue(ps.isVideoFile()); + } + } + + @Test + void testGetVideoLink() throws IOException { + try (XMLSlideShow ppt = openSampleDocument("EmbeddedVideo.pptx")) { + XSLFSlide slide = ppt.getSlides().get(0); + XSLFPictureShape ps = (XSLFPictureShape) slide.getShapes().get(0); + + assertEquals(ps.getVideoFileLink(), "rId2"); + } + } } \ No newline at end of file diff --git a/test-data/slideshow/EmbeddedVideo.pptx b/test-data/slideshow/EmbeddedVideo.pptx new file mode 100644 index 0000000000..f7954228a8 Binary files /dev/null and b/test-data/slideshow/EmbeddedVideo.pptx differ