I need to duplicate the same shape in a slide using Apache POI (XSLF) ppt.
I can do the something like this below code ?
static void cloneShape(XMLSlideShow slideShow, int slideNumber, String textBoxId) {
Optional<XSLFShape> textBoxopt = getShapesByName(slideShow, slideNumber, textBoxId).stream().findFirst();
XSLFAutoShape shapeToBeCloned = (XSLFAutoShape) textBoxopt.get();
XSLFShapeContainer slide = slideShow.getSlides().get(slideNumber);
XSLFAutoShape shape1 = slide.createAutoShape(***shapeToBeCloned***);
There is not any clone method for
XSLFShape
s. And even if it would, then there is not any method to add a clonedXSLFShape
to theXSLFSheet
(slide). There is XSSFSheet.addShape(XSLFShape shape) but this does nothing but throwingUnsupportedOperationException
. I love the sense of humor of theapache poi
developers.So if one want copy a shape of a slide, then one only is able using the underlying objects. The class
org.apache.xmlbeans.XmlObject
provides acopy
method which makes a deep copy of the XML. Then that copy needs to be added into the shape tree of the slide. Then the shape tree of the slide needs to be new initialized. After that the high level object of the shape can be got fromXSSFSheet.getShapes()
. Unfortunately most of the needed methods are not public. So reflection needs to be used.Following code shows one way to do this. It simply clones all shapes except group shapes and graphical object frame shapes in each slide of the given
PPTIn.pptx
.