Java Examples for org.apache.poi.xslf.usermodel.XSLFShape

The following java examples will help you to understand the usage of org.apache.poi.xslf.usermodel.XSLFShape. These source code samples are taken from different open source projects.

Example 1
Project: Aspose_for_Apache_POI-master  File: ApacheGetShapesFromSlides.java View source code
public static void main(String[] args) throws Exception {
    String dataPath = "src/featurescomparison/workingwithpresentation/getshapesfromslides/data/";
    XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(dataPath + "AutoShapes.pptx"));
    //get slides 
    XSLFSlide[] slide = ppt.getSlides();
    for (int i = 0; i < slide.length; i++) {
        XSLFShape[] sh = slide[i].getShapes();
        for (int j = 0; j < sh.length; j++) {
            //name of the shape
            String name = sh[j].getShapeName();
            //shapes's anchor which defines the position of this shape in the slide
            java.awt.geom.Rectangle2D anchor = sh[j].getAnchor();
            if (sh[j] instanceof XSLFConnectorShape) {
                XSLFConnectorShape line = (XSLFConnectorShape) sh[j];
                System.out.println("Connector Shape.");
            //work with Line
            } else if (sh[j] instanceof XSLFTextShape) {
                XSLFTextShape shape = (XSLFTextShape) sh[j];
                System.out.println("Text Shape.");
            //work with a shape that can hold text
            } else if (sh[j] instanceof XSLFPictureShape) {
                XSLFPictureShape shape = (XSLFPictureShape) sh[j];
                System.out.println("Picture Shape.");
            //work with Picture
            }
        }
    }
    System.out.println("Done...");
}
Example 2
Project: poi-master  File: XSLFPowerPointExtractor.java View source code
private static void extractText(XSLFShapeContainer data, boolean skipPlaceholders, StringBuilder text) {
    for (XSLFShape s : data) {
        if (s instanceof XSLFShapeContainer) {
            extractText((XSLFShapeContainer) s, skipPlaceholders, text);
        } else if (s instanceof XSLFTextShape) {
            XSLFTextShape ts = (XSLFTextShape) s;
            // Skip non-customised placeholder text
            if (!(skipPlaceholders && ts.isPlaceholder())) {
                text.append(ts.getText());
                text.append("\n");
            }
        } else if (s instanceof XSLFTable) {
            XSLFTable ts = (XSLFTable) s;
            // Skip non-customised placeholder text
            for (XSLFTableRow r : ts) {
                for (XSLFTableCell c : r) {
                    text.append(c.getText());
                    text.append("\t");
                }
                text.append("\n");
            }
        }
    }
}
Example 3
Project: OCRaptor-master  File: XSLFPowerPointExtractorDecorator.java View source code
private void extractContent(XSLFShape[] shapes, boolean skipPlaceholders, XHTMLContentHandler xhtml, String slideDesc) throws SAXException {
    for (XSLFShape sh : shapes) {
        if (sh instanceof XSLFTextShape) {
            XSLFTextShape txt = (XSLFTextShape) sh;
            Placeholder ph = txt.getTextType();
            if (skipPlaceholders && ph != null) {
                continue;
            }
            xhtml.element("p", txt.getText());
        } else if (sh instanceof XSLFGroupShape) {
            // recurse into groups of shapes
            XSLFGroupShape group = (XSLFGroupShape) sh;
            extractContent(group.getShapes(), skipPlaceholders, xhtml, slideDesc);
        } else if (sh instanceof XSLFTable) {
            XSLFTable tbl = (XSLFTable) sh;
            for (XSLFTableRow row : tbl) {
                List<XSLFTableCell> cells = row.getCells();
                extractContent(cells.toArray(new XSLFTableCell[cells.size()]), skipPlaceholders, xhtml, slideDesc);
            }
        } else if (sh instanceof XSLFGraphicFrame) {
            XSLFGraphicFrame frame = (XSLFGraphicFrame) sh;
            XmlObject[] sp = frame.getXmlObject().selectPath("declare namespace p='http://schemas.openxmlformats.org/presentationml/2006/main' .//*/p:oleObj");
            if (sp != null) {
                for (XmlObject emb : sp) {
                    XmlObject relIDAtt = emb.selectAttribute(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "id"));
                    if (relIDAtt != null) {
                        String relID = relIDAtt.getDomNode().getNodeValue();
                        if (slideDesc != null) {
                            relID = slideDesc + relID;
                        }
                        AttributesImpl attributes = new AttributesImpl();
                        attributes.addAttribute("", "class", "class", "CDATA", "embedded");
                        attributes.addAttribute("", "id", "id", "CDATA", relID);
                        xhtml.startElement("div", attributes);
                        xhtml.endElement("div");
                    }
                }
            }
        } else if (sh instanceof XSLFPictureShape) {
            if (!skipPlaceholders && (sh.getXmlObject() instanceof CTPicture)) {
                CTPicture ctPic = ((CTPicture) sh.getXmlObject());
                if (ctPic.getBlipFill() != null && ctPic.getBlipFill().getBlip() != null) {
                    String relID = ctPic.getBlipFill().getBlip().getEmbed();
                    if (relID != null) {
                        if (slideDesc != null) {
                            relID = slideDesc + relID;
                        }
                        AttributesImpl attributes = new AttributesImpl();
                        attributes.addAttribute("", "class", "class", "CDATA", "embedded");
                        attributes.addAttribute("", "id", "id", "CDATA", relID);
                        xhtml.startElement("div", attributes);
                        xhtml.endElement("div");
                    }
                }
            }
        }
    }
}
Example 4
Project: CodeUtils-master  File: TempUtils.java View source code
public static void replacePPT() {
    Map<String, String> replaceMap = new HashMap<String, String>();
    // replaceMap.put("实战名称", "微博实战:Android 视频播放");
    // replaceMap.put("知识点1", "我页面的实现");
    replaceMap.put("我页面的实现", "知识点1");
    replaceMap.put("个人中心页面的基本实现", "知识点2");
    replaceMap.put("个人中心页面菜单栏相关效果", "知识点3");
    replaceMap.put("个人中心页面背景图变化效果", "");
    try {
        // 获取ppt文件
        FileInputStream is = new FileInputStream("temp" + File.separator + "office" + File.separator + "ppt2007.pptx");
        XMLSlideShow ppt = new XMLSlideShow(is);
        is.close();
        // 获取幻灯片
        for (XSLFSlide slide : ppt.getSlides()) {
            // 获取每一张幻灯片中的shape
            for (XSLFShape shape : slide.getShapes()) {
                // position on the canvas
                Rectangle2D anchor = shape.getAnchor();
                if (shape instanceof XSLFTextShape) {
                    XSLFTextShape txShape = (XSLFTextShape) shape;
                    // 获取其中的文字
                    String text = txShape.getText();
                    // 替换文字内容
                    text = replaceAllMap(text, replaceMap);
                    txShape.setText(text);
                    if (text.contains("{picture}")) {
                        // // 替换图片
                        // byte[] pictureData = IOUtils.toByteArray(
                        // new FileInputStream("E:\\33.png"));
                        // int idx = ppt.addPicture(pictureData,
                        // XSLFPictureData.PICTURE_TYPE_PNG);
                        // XSLFPictureShape pic = slide.createPicture(idx);
                        // // 设置XSLFPictureShape的位置信息
                        // pic.setAnchor(anchor);
                        // // 移除XSLFTextShape
                        // slide.removeShape(txShape);
                        System.out.println("替换图片");
                    }
                } else if (shape instanceof XSLFGroupShape) {
                    System.out.println("替换group");
                    for (XSLFShape sunshape : ((XSLFGroupShape) shape).getShapes()) {
                        XSLFTextShape txSunShape = (XSLFTextShape) sunshape;
                        // 获取其中的文字
                        String text = txSunShape.getText();
                        // 替换文字内容
                        text = replaceAllMap(text, replaceMap);
                        txSunShape.setText(text);
                    // if (text.contains("{picture}")) {
                    // // 替换图片
                    // byte[] pictureData = IOUtils
                    // .toByteArray(new FileInputStream(
                    // "E:\\33.png"));
                    // int idx = ppt.addPicture(pictureData,
                    // XSLFPictureData.PICTURE_TYPE_PNG);
                    // XSLFPictureShape pic = slide.createPicture(idx);
                    // slide.removeShape(txSunShape);
                    // pic.setAnchor(anchor);
                    // }
                    }
                } else if (shape instanceof XSLFPictureShape) {
                    System.out.println("替换picture");
                    XSLFPictureShape pShape = (XSLFPictureShape) shape;
                    XSLFPictureData pData = pShape.getPictureData();
                    System.out.println(pData.getFileName());
                } else {
                    System.out.println("Process me: " + shape.getClass());
                }
            }
        }
        File file = new File("temp" + File.separator + "office" + File.separator + "ppt2007plus_new.pptx");
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream out = new FileOutputStream(file);
        ppt.write(out);
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Example 5
Project: Aspose_Slides_Java-master  File: ApacheGetShapesFromSlides.java View source code
public static void main(String[] args) throws Exception {
    // The path to the documents directory.
    String dataDir = Utils.getDataDir(ApacheGetShapesFromSlides.class);
    XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(dataDir + "AutoShapes.pptx"));
    //get slides 
    XSLFSlide[] slide = ppt.getSlides();
    for (int i = 0; i < slide.length; i++) {
        XSLFShape[] sh = slide[i].getShapes();
        for (int j = 0; j < sh.length; j++) {
            //name of the shape
            String name = sh[j].getShapeName();
            //shapes's anchor which defines the position of this shape in the slide
            java.awt.geom.Rectangle2D anchor = sh[j].getAnchor();
            if (sh[j] instanceof XSLFConnectorShape) {
                XSLFConnectorShape line = (XSLFConnectorShape) sh[j];
                System.out.println("Connector Shape.");
            //work with Line
            } else if (sh[j] instanceof XSLFTextShape) {
                XSLFTextShape shape = (XSLFTextShape) sh[j];
                System.out.println("Text Shape.");
            //work with a shape that can hold text
            } else if (sh[j] instanceof XSLFPictureShape) {
                XSLFPictureShape shape = (XSLFPictureShape) sh[j];
                System.out.println("Picture Shape.");
            //work with Picture
            }
        }
    }
    System.out.println("Done...");
}