Example usage for org.apache.poi.xslf.usermodel XSLFTextShape getTextParagraphs

List of usage examples for org.apache.poi.xslf.usermodel XSLFTextShape getTextParagraphs

Introduction

In this page you can find the example usage for org.apache.poi.xslf.usermodel XSLFTextShape getTextParagraphs.

Prototype

@Override
    public List<XSLFTextParagraph> getTextParagraphs() 

Source Link

Usage

From source file:org.apache.tika.parser.microsoft.ooxml.XSLFPowerPointExtractorDecorator.java

License:Apache License

private void extractContent(List<? extends 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;
            }//from   w  w  w.  j ava 2s. c o m
            for (XSLFTextParagraph p : txt.getTextParagraphs()) {
                xhtml.element("p", p.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) {
            //unlike tables in Word, ppt/x can't have recursive tables...I don't think
            extractTable((XSLFTable) sh, xhtml);
        } 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");
                    }
                }
            }
        }
    }
}

From source file:org.joeffice.presentation.ShapeComponent.java

License:Apache License

private void handleTextShape(XSLFTextShape textShape) {
    final JTextPane textField = new JTextPane();
    textField.setBorder(BorderFactory.createEmptyBorder());
    textField.setOpaque(false);/*from   w  w w .  j av  a  2s . com*/
    textField.getActionMap().remove(DefaultEditorKit.pageDownAction); // used to move to next or previous slide
    textField.getActionMap().remove(DefaultEditorKit.pageUpAction);
    List<XSLFTextParagraph> paragraphs = textShape.getTextParagraphs();
    boolean newLine = false;
    for (XSLFTextParagraph paragraph : paragraphs) {
        applyAlignment(paragraph, textField);
        List<XSLFTextRun> textParts = paragraph.getTextRuns();
        for (XSLFTextRun textPart : textParts) {
            try {
                String text = textPart.getText();
                AttributeSet attributes = null;
                String simpleBullet = "";
                try {
                    attributes = getFontAttributes(textPart);
                    simpleBullet = getBullet(paragraph);
                } catch (Exception ex) {
                    // ignore
                }
                if (!text.isEmpty()) {
                    text = simpleBullet + text;
                }
                if (newLine) {
                    text = "\r\n" + text;
                }
                int documentLength = textField.getDocument().getLength();
                textField.getDocument().insertString(documentLength, text, attributes);
            } catch (BadLocationException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
        newLine = true;
    }

    add(textField);
    if (editable) {
        textField.getDocument().addDocumentListener(this);
        textField.getDocument().addUndoableEditListener(
                (UndoRedo.Manager) getSlideComponent().getSlidesComponent().getUndoRedo());
        textField.addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent e) {
                registerActions(textField);
            }

            @Override
            public void focusLost(FocusEvent e) {
                unregisterActions();
            }
        });
    } else {
        textField.setEditable(false);
    }
}