Example usage for org.apache.poi.xslf.usermodel XSLFSlide getShapes

List of usage examples for org.apache.poi.xslf.usermodel XSLFSlide getShapes

Introduction

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

Prototype

@Override
public List<XSLFShape> getShapes() 

Source Link

Document

Returns an array containing all of the shapes in this sheet

Usage

From source file:com.github.codeurjc.slidesconverter.PowerPointToHTML.java

License:Apache License

private void generateSlidesContent(XMLSlideShow pptx, HSLFSlideShow ppt) throws IOException {

    out.println("<h1>Slides</h1>");

    for (int numSlide = 0; numSlide < pptx.getSlides().size(); numSlide++) {

        System.out.println("Processing slide " + numSlide);

        XSLFSlide slideX = pptx.getSlides().get(numSlide);
        HSLFSlide slide = ppt.getSlides().get(numSlide);

        Section section = getSection(numSlide);

        if (section != null && section.startSlide == numSlide) {

            if (section.level == 0) {

                out.println("<h2>" + escape(section.sectionNum + ". " + section.name) + "</h2>");

            } else {

                out.println(//  w  ww.j  a  va2 s  .com
                        "<h3>" + escape(section.sectionNum + "." + section.subsectionNum + ". " + section.name)
                                + "</h3>");
            }
        }

        generateSlideImage(slide);

        out.println("<h4>Slide (" + (numSlide + 1) + ")</h4>");

        out.println("<img src=" + getImageFileName(slideX.getSlideNumber()) + "><br>");

        for (XSLFShape shape : slideX.getShapes()) {

            if (shape instanceof XSLFTextShape) {

                XSLFTextShape tsh = (XSLFTextShape) shape;

                boolean first = true;
                boolean code = false;

                boolean subtitle = false;

                for (XSLFTextParagraph p : tsh) {

                    String indent = "";

                    int indentLevel = p.getIndentLevel();
                    if (subtitle) {
                        indentLevel++;
                    }

                    if (indentLevel > 0) {
                        for (int j = 0; j < indentLevel; j++) {
                            indent += "> ";
                        }
                    }

                    StringBuilder sb = new StringBuilder();
                    for (XSLFTextRun r : p) {
                        sb.append(r.getRawText());
                    }

                    String text = sb.toString();

                    // Avoid section or subsection name to appear in slide
                    // contents
                    if ((section != null && (text.equals(section.name)
                            || (section.parentSection != null && section.parentSection.name.equals(text))))
                            || text.trim().isEmpty()) {
                        continue;
                    }

                    if (first) {

                        code = p.getDefaultFontFamily().startsWith("Courier");

                        if (code) {
                            out.println("<pre style='border:1px;border-style: solid;'>");
                        }
                    }

                    if (!code) {
                        out.println("<p>");
                    }

                    out(indent + text);

                    if (!code) {
                        out.println("</p>");
                    }

                    first = false;
                    if (p.getTextAlign() == TextAlign.CENTER) {
                        subtitle = true;
                    }
                }

                if (code) {
                    out.println("</pre>");
                }

            } else if (shape instanceof XSLFPictureShape) {

                XSLFPictureShape pShape = (XSLFPictureShape) shape;

                out.println("<p>Imagen: " + pShape.getShapeName() + "</p>");
            }
        }

        out.println("<hr>");
    }

    out.println("</body></html>");
}

From source file:com.github.codeurjc.slidesconverter.PowerPointToHTML.java

License:Apache License

private String calculateSubtitle(XSLFSlide slideX, HSLFSlide slide) {

    if (isContentSlide(slide.getSlideNumber())) {
        return null;
    }//from ww  w .jav  a2 s  . c  om

    for (XSLFShape shape : slideX.getShapes()) {

        if (shape instanceof XSLFTextShape) {

            XSLFTextShape tsh = (XSLFTextShape) shape;

            Rectangle2D figure = getRelativeFigure(tsh);

            if (figure.getY() < 0.1) {
                continue;
            }

            for (XSLFTextParagraph p : tsh) {
                for (XSLFTextRun r : p) {
                    return r.getRawText();
                }
            }

            return null;
        }
    }

    return null;
}

From source file:com.github.codeurjc.slidesconverter.PowerPointToHTML.java

License:Apache License

private String calculateTitle(XSLFSlide slideX, HSLFSlide slide) {

    String title = slide.getTitle();

    if (title != null) {
        return title;
    }/* w  w w . j  a  va 2 s. c om*/

    title = slideX.getTitle();

    if (title != null) {
        return title;
    }

    boolean titleSlide = ArrayUtils.contains(titleSlides, slideX.getSlideNumber());

    for (XSLFShape shape : slideX.getShapes()) {

        if (shape instanceof XSLFTextShape) {

            XSLFTextShape tsh = (XSLFTextShape) shape;

            Rectangle2D figure = getRelativeFigure(tsh);

            if (titleSlide || figure.getY() < 0.1) {

                StringBuilder titleSB = new StringBuilder();

                for (XSLFTextParagraph p : tsh) {
                    for (XSLFTextRun r : p) {
                        titleSB.append(r.getRawText());
                    }
                }

                title = titleSB.toString();

                if (!title.trim().isEmpty()) {
                    return title;
                }
            }
        }
    }

    return null;
}

From source file:com.hp.autonomy.frontend.reports.powerpoint.PowerPointServiceImpl.java

License:MIT License

private static void transferSizedTextboxes(final XMLSlideShow ppt, final XSLFSlide slide,
        final XSLFSlide sizingSlide) {
    // Clone all text boxes to the original slide afterward, and remove the sizing slide
    for (XSLFShape shape : sizingSlide.getShapes()) {
        if (shape instanceof XSLFTextBox) {
            final XSLFTextBox src = (XSLFTextBox) shape;
            final XSLFTextBox textBox = slide.createTextBox();
            textBox.setAnchor(src.getAnchor());
            textBox.clearText();/*from   w w  w  .  j  a va 2s.co  m*/
            src.forEach(
                    srcPara -> textBox.addNewTextParagraph().getXmlObject().set(srcPara.getXmlObject().copy()));
        }
    }

    ppt.removeSlide(ppt.getSlides().indexOf(sizingSlide));
}

From source file:com.hp.autonomy.frontend.reports.powerpoint.SlideShowTemplate.java

License:MIT License

/**
 * Given an existing slide, search its relations to find a chart object.
 * @param slide a slide from the template.
 * @param error if we can't find a slide, this error message will be returned as the exception.
 * @return a pair containing the chart.xml data and the graphical object which represented it on the slide.
 * @throws TemplateLoadException if we can't find a chart object.
 *//*from  ww  w . j ava  2  s  .  c om*/
private ImmutablePair<XSLFChart, CTGraphicalObjectFrame> getChart(final XSLFSlide slide, final String error)
        throws TemplateLoadException {
    for (POIXMLDocumentPart.RelationPart part : slide.getRelationParts()) {
        if (part.getDocumentPart() instanceof XSLFChart) {
            final String relId = part.getRelationship().getId();

            for (XSLFShape shape : slide.getShapes()) {
                if (shape instanceof XSLFGraphicFrame) {
                    final CTGraphicalObjectFrame frameXML = (CTGraphicalObjectFrame) shape.getXmlObject();
                    final XmlObject[] children = frameXML.getGraphic().getGraphicData()
                            .selectChildren(new QName(XSSFRelation.NS_CHART, "chart"));

                    for (final XmlObject child : children) {
                        final String imageRel = child.getDomNode().getAttributes()
                                .getNamedItemNS(RELATION_NAMESPACE, "id").getNodeValue();

                        if (relId.equals(imageRel)) {
                            return new ImmutablePair<>(part.getDocumentPart(), frameXML);
                        }
                    }
                }
            }
        }
    }

    throw new TemplateLoadException(error);
}

From source file:easyoffice.powerpoint.PPTMaker.java

private static void processSlides(XMLSlideShow ppt) {
    ArrayList<Slide> replacementData = generateSlides();
    XSLFSlide templateSlide = ppt.getSlides().get(TEMPLATE_SLIDE_INDEX);
    boolean somethingReplaced = false;

    for (Slide slide : replacementData) {
        XSLFSlide copiedSlide = copySlide(ppt, templateSlide);
        List<XSLFShape> slideShapes = copiedSlide.getShapes();

        for (XSLFShape slideShape : slideShapes) {
            if (slideShape instanceof XSLFTextShape) {
                XSLFTextShape textShape = (XSLFTextShape) slideShape;
                for (SlideText slideText : slide.replacementData) {
                    if (textShape.getText().contains(String.format(STRING_FORMAT, slideText.key))) {
                        replaceContent(textShape, slideText);
                        somethingReplaced = true;
                    }/*from   ww  w. jav a 2  s . c om*/
                }
            }
        }
    }

    if (!somethingReplaced) {
        System.out.println(Strings.NOTHING_REPLACED(TEMPLATE_SLIDE_INDEX));
        System.exit(0);
    }
}

From source file:mj.ocraptor.extraction.tika.parser.microsoft.ooxml.XSLFPowerPointExtractorDecorator.java

License:Apache License

/**
 * @see org.apache.poi.xslf.extractor.XSLFPowerPointExtractor#getText()
 *//*from ww w . java 2s  .  c o  m*/
protected void buildXHTML(XHTMLContentHandler xhtml) throws SAXException, IOException {
    XMLSlideShow slideShow = (XMLSlideShow) extractor.getDocument();

    XSLFSlide[] slides = slideShow.getSlides();
    for (XSLFSlide slide : slides) {
        String slideDesc;

        if (slide.getPackagePart() != null && slide.getPackagePart().getPartName() != null) {
            slideDesc = getJustFileName(slide.getPackagePart().getPartName().toString());
            slideDesc += "_";
        } else {
            slideDesc = null;
        }

        // slide
        extractContent(slide.getShapes(), false, xhtml, slideDesc);

        // slide layout which is the master sheet for this slide
        XSLFSheet slideLayout = slide.getMasterSheet();
        extractContent(slideLayout.getShapes(), true, xhtml, null);

        // slide master which is the master sheet for all text layouts
        XSLFSheet slideMaster = slideLayout.getMasterSheet();
        extractContent(slideMaster.getShapes(), true, xhtml, null);

        // notes (if present)
        XSLFSheet slideNotes = slide.getNotes();
        if (slideNotes != null) {
            extractContent(slideNotes.getShapes(), false, xhtml, slideDesc);
            // master sheet for this notes
            XSLFSheet notesMaster = slideNotes.getMasterSheet();
            extractContent(notesMaster.getShapes(), true, xhtml, null);
        }

        // comments (if present)
        XSLFComments comments = slide.getComments();
        if (comments != null) {
            for (CTComment comment : comments.getCTCommentsList().getCmList()) {
                xhtml.element("p", comment.getText());
            }
        }
    }

    if (Config.inst().getProp(ConfigBool.ENABLE_IMAGE_OCR)) {
        TikaImageHelper helper = new TikaImageHelper(metadata);
        try {
            List<XSLFPictureData> pictures = slideShow.getAllPictures();
            for (XSLFPictureData picture : pictures) {
                ByteArrayInputStream imageData = new ByteArrayInputStream(picture.getData());
                BufferedImage image = ImageIO.read(imageData);
                helper.addImage(image);
                helper.addTextToHandler(xhtml);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (extractor != null) {
                extractor.close();
            }
            if (helper != null) {
                helper.close();
            }
        }
    }
}

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

License:Apache License

/**
 * @see org.apache.poi.xslf.extractor.XSLFPowerPointExtractor#getText()
 *//*www.j  a  v a2 s.c om*/
protected void buildXHTML(XHTMLContentHandler xhtml) throws SAXException, IOException {
    XMLSlideShow slideShow = (XMLSlideShow) extractor.getDocument();
    XSLFCommentAuthors commentAuthors = slideShow.getCommentAuthors();

    List<XSLFSlide> slides = slideShow.getSlides();
    for (XSLFSlide slide : slides) {
        String slideDesc;
        if (slide.getPackagePart() != null && slide.getPackagePart().getPartName() != null) {
            slideDesc = getJustFileName(slide.getPackagePart().getPartName().toString());
            slideDesc += "_";
        } else {
            slideDesc = null;
        }

        // slide content
        xhtml.startElement("div", "class", "slide-content");
        extractContent(slide.getShapes(), false, xhtml, slideDesc);
        xhtml.endElement("div");

        // slide layout which is the master sheet for this slide
        xhtml.startElement("div", "class", "slide-master-content");
        XSLFSlideLayout slideLayout = slide.getMasterSheet();
        extractContent(slideLayout.getShapes(), true, xhtml, null);
        xhtml.endElement("div");

        // slide master which is the master sheet for all text layouts
        XSLFSheet slideMaster = slideLayout.getMasterSheet();
        extractContent(slideMaster.getShapes(), true, xhtml, null);

        // notes (if present)
        XSLFNotes slideNotes = slide.getNotes();
        if (slideNotes != null) {
            xhtml.startElement("div", "class", "slide-notes");

            extractContent(slideNotes.getShapes(), false, xhtml, slideDesc);

            // master sheet for this notes
            XSLFNotesMaster notesMaster = slideNotes.getMasterSheet();
            extractContent(notesMaster.getShapes(), true, xhtml, null);
            xhtml.endElement("div");
        }

        // comments (if present)
        XSLFComments comments = slide.getComments();
        if (comments != null) {
            StringBuilder authorStringBuilder = new StringBuilder();
            for (int i = 0; i < comments.getNumberOfComments(); i++) {
                authorStringBuilder.setLength(0);
                CTComment comment = comments.getCommentAt(i);
                xhtml.startElement("p", "class", "slide-comment");
                CTCommentAuthor cta = commentAuthors.getAuthorById(comment.getAuthorId());
                if (cta != null) {
                    if (cta.getName() != null) {
                        authorStringBuilder.append(cta.getName());
                    }
                    if (cta.getInitials() != null) {
                        if (authorStringBuilder.length() > 0) {
                            authorStringBuilder.append(" ");
                        }
                        authorStringBuilder.append("(" + cta.getInitials() + ")");
                    }
                    if (comment.getText() != null && authorStringBuilder.length() > 0) {
                        authorStringBuilder.append(" - ");
                    }
                    if (authorStringBuilder.length() > 0) {
                        xhtml.startElement("b");
                        xhtml.characters(authorStringBuilder.toString());
                        xhtml.endElement("b");
                    }
                }
                xhtml.characters(comment.getText());
                xhtml.endElement("p");
            }
        }
    }
}

From source file:org.joeffice.tools.PptxShapeNotDrawn.java

License:Apache License

public static void main(String[] args) throws IOException {
    if (args.length == 0) {
        System.err.println("Please profile a file path to open");
        System.exit(-1);//from   www.ja v  a 2s .c om
    }

    JPanel mainPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
    XMLSlideShow presentation = new XMLSlideShow(new FileInputStream(args[0]));
    XSLFSlide[] slides = presentation.getSlides();
    for (XSLFSlide slide : slides) {
        XSLFShape[] shapes = slide.getShapes();
        for (XSLFShape shape : shapes) {

            BufferedImage img = new BufferedImage((int) shape.getAnchor().getWidth(),
                    (int) shape.getAnchor().getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D graphics = img.createGraphics();
            graphics.translate(-shape.getAnchor().getX(), -shape.getAnchor().getY());
            shape.draw(graphics);
            graphics.dispose();
            JLabel shapeLabel = new JLabel(new ImageIcon(img));
            shapeLabel.setBorder(BorderFactory.createLineBorder(Color.RED));

            mainPanel.add(shapeLabel);
        }
    }
    showDemo(new JScrollPane(mainPanel), "Shape not displayed");
}

From source file:poi.xslf.usermodel.tutorial.Step1.java

License:Apache License

public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        System.out.println("Input file is required");
        return;//from   w  w w  .  j  a v  a 2  s.  com
    }

    XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(args[0]));

    for (XSLFSlide slide : ppt.getSlides()) {
        System.out.println("Title: " + slide.getTitle());

        for (XSLFShape shape : slide.getShapes()) {
            if (shape instanceof XSLFTextShape) {
                XSLFTextShape tsh = (XSLFTextShape) shape;
                for (XSLFTextParagraph p : tsh) {
                    System.out.println("Paragraph level: " + p.getLevel());
                    for (XSLFTextRun r : p) {
                        System.out.println(r.getText());
                        System.out.println("  bold: " + r.isBold());
                        System.out.println("  italic: " + r.isItalic());
                        System.out.println("  underline: " + r.isUnderline());
                        System.out.println("  font.family: " + r.getFontFamily());
                        System.out.println("  font.size: " + r.getFontSize());
                        System.out.println("  font.color: " + r.getFontColor());
                    }
                }
            }
        }
    }
}