Example usage for org.apache.poi.xslf.usermodel XMLSlideShow createSlide

List of usage examples for org.apache.poi.xslf.usermodel XMLSlideShow createSlide

Introduction

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

Prototype

@Override
public XSLFSlide createSlide() 

Source Link

Document

Create a blank slide using the default (first) master.

Usage

From source file:Tutorial1.java

License:Apache License

public static void main(String[] args) throws IOException {
    XMLSlideShow ppt = new XMLSlideShow();

    // XSLFSlide#createSlide() with no arguments creates a blank slide
    XSLFSlide blankSlide = ppt.createSlide();

    XSLFSlideMaster master = ppt.getSlideMasters()[0];

    XSLFSlideLayout layout1 = master.getLayout(SlideLayout.TITLE);
    XSLFSlide slide1 = ppt.createSlide(layout1);
    XSLFTextShape[] ph1 = slide1.getPlaceholders();
    XSLFTextShape titlePlaceholder1 = ph1[0];
    titlePlaceholder1.setText("This is a title");
    XSLFTextShape subtitlePlaceholder1 = ph1[1];
    subtitlePlaceholder1.setText("this is a subtitle");

    XSLFSlideLayout layout2 = master.getLayout(SlideLayout.TITLE_AND_CONTENT);
    XSLFSlide slide2 = ppt.createSlide(layout2);
    XSLFTextShape[] ph2 = slide2.getPlaceholders();
    XSLFTextShape titlePlaceholder2 = ph2[0];
    titlePlaceholder2.setText("This is a title");
    XSLFTextShape bodyPlaceholder = ph2[1];
    // we are going to add text by paragraphs. Clear the default placehoder text before that
    bodyPlaceholder.clearText();//  www.  ja va  2s.c  o  m
    XSLFTextParagraph p1 = bodyPlaceholder.addNewTextParagraph();
    p1.setLevel(0);
    p1.addNewTextRun().setText("Level1 text");
    XSLFTextParagraph p2 = bodyPlaceholder.addNewTextParagraph();
    p2.setLevel(1);
    p2.addNewTextRun().setText("Level2 text");
    XSLFTextParagraph p3 = bodyPlaceholder.addNewTextParagraph();
    p3.setLevel(3);
    p3.addNewTextRun().setText("Level3 text");

    FileOutputStream out = new FileOutputStream("slides.pptx");
    ppt.write(out);
    out.close();
}

From source file:AddAudioToPptx.java

License:Apache License

public static void main(String[] args) throws Exception {
    URL video = new URL("http://archive.org/download/test-mpeg/test-mpeg.mpg");
    // URL video = new URL("file:test-mpeg.mpg");

    XMLSlideShow pptx = new XMLSlideShow();

    // add video file
    String videoFileName = "lego_edsheeran.mp3";
    PackagePartName partName = PackagingURIHelper.createPartName("/ppt/media/" + videoFileName);
    PackagePart part = pptx.getPackage().createPart(partName, "audio/mpeg");
    OutputStream partOs = part.getOutputStream();
    //InputStream fis = video.openStream();
    FileInputStream fis = new FileInputStream(videoFileName);
    byte buf[] = new byte[1024];
    for (int readBytes; (readBytes = fis.read(buf)) != -1; partOs.write(buf, 0, readBytes))
        ;/*from www. j  av a2 s  . c o  m*/
    fis.close();
    partOs.close();

    XSLFSlide slide1 = pptx.createSlide();

    byte[] picture = IOUtils.toByteArray(new FileInputStream("audio.png"));

    //adding the image to the presentation
    XSLFPictureData idx = pptx.addPicture(picture, XSLFPictureData.PictureType.PNG);

    //creating a slide with given picture on it
    XSLFPictureShape pv1 = slide1.createPicture(idx);

    //XSLFPictureShape pv1 = // addPreview(pptx, slide1, part, 5, 50, 80);
    addAudio(pptx, slide1, part, pv1, 5);
    addTimingInfo(slide1, pv1);
    //XSLFPictureShape pv2 = addPreview(pptx, slide1, part, 9, 50, 250);
    //addVideo(pptx, slide1, part, pv2, 9);
    //addTimingInfo(slide1, pv2);

    FileOutputStream fos = new FileOutputStream("pptx-with-audio.pptx");
    pptx.write(fos);
    fos.close();

    pptx.close();
}

From source file:AddVideoToPptx.java

License:Apache License

public static void main(String[] args) throws Exception {
    URL video = new URL("http://archive.org/download/test-mpeg/test-mpeg.mpg");
    // URL video = new URL("file:test-mpeg.mpg");

    XMLSlideShow pptx = new XMLSlideShow();

    // add video file
    String videoFileName = video.getPath().substring(video.getPath().lastIndexOf('/') + 1);
    PackagePartName partName = PackagingURIHelper.createPartName("/ppt/media/" + videoFileName);
    PackagePart part = pptx.getPackage().createPart(partName, "video/mpeg");
    OutputStream partOs = part.getOutputStream();
    //InputStream fis = video.openStream();
    FileInputStream fis = new FileInputStream("Lea Michele - Cannonball.mp4");
    byte buf[] = new byte[1024];
    for (int readBytes; (readBytes = fis.read(buf)) != -1; partOs.write(buf, 0, readBytes))
        ;/*from w  w  w .  ja va2 s  . c om*/
    fis.close();
    partOs.close();

    XSLFSlide slide1 = pptx.createSlide();
    XSLFPictureShape pv1 = addPreview(pptx, slide1, part, 5, 50, 80);
    addVideo(pptx, slide1, part, pv1, 5);
    addTimingInfo(slide1, pv1);
    //XSLFPictureShape pv2 = addPreview(pptx, slide1, part, 9, 50, 250);
    //addVideo(pptx, slide1, part, pv2, 9);
    //addTimingInfo(slide1, pv2);

    FileOutputStream fos = new FileOutputStream("pptx-with-video.pptx");
    pptx.write(fos);
    fos.close();

    pptx.close();
}

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

License:MIT License

@Override
public XMLSlideShow topicmap(final TopicMapData topicmap) throws TemplateLoadException {
    final XMLSlideShow ppt = loadTemplate().getSlideShow();
    final XSLFSlide slide = ppt.createSlide();

    addTopicMap(slide, createPageAnchor(ppt), topicmap);

    return ppt;/* w  w w .ja  v a2 s .  c om*/
}

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

License:MIT License

@Override
public XMLSlideShow sunburst(final SunburstData sunburst) throws TemplateLoadException {
    if (!sunburst.validateInput()) {
        throw new IllegalArgumentException(
                "Number of values should match the number of categories, and color / stroke color must either be null or nonempty");
    }/*from   w w  w  .  ja v  a  2 s. c  om*/

    final SlideShowTemplate template = loadTemplate();
    final XMLSlideShow ppt = template.getSlideShow();
    final XSLFSlide slide = ppt.createSlide();

    final int shapeId = 1;

    addSunburst(template, slide, null, sunburst, shapeId, "relId" + shapeId);

    return ppt;
}

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

License:MIT License

@Override
public XMLSlideShow table(final TableData tableData, final String title) throws TemplateLoadException {
    final int rows = tableData.getRows(), cols = tableData.getCols();
    final String[] data = tableData.getCells();

    final XMLSlideShow ppt = loadTemplate().getSlideShow();
    final XSLFSlide sl = ppt.createSlide();

    final Rectangle2D.Double pageAnchor = createPageAnchor(ppt);

    final double textHeight;

    if (StringUtils.isNotBlank(title)) {
        textHeight = 0.1 * pageAnchor.getHeight();
        final XSLFTextBox textBox = sl.createTextBox();
        textBox.setText(title);//from  w w w .j av  a 2 s. com
        textBox.setHorizontalCentered(true);
        textBox.setTextAutofit(TextShape.TextAutofit.SHAPE);
        textBox.setAnchor(startingSpace(pageAnchor, textHeight));
    } else {
        textHeight = 0;
    }

    addTable(sl, remainingSpace(pageAnchor, textHeight), rows, cols, data, false);

    return ppt;
}

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

License:MIT License

@Override
public XMLSlideShow map(final MapData map, final String title) throws TemplateLoadException {
    final String image = map.getImage();

    final XMLSlideShow ppt = loadTemplate().getSlideShow();
    final XSLFSlide sl = ppt.createSlide();

    final Rectangle2D.Double pageAnchor = createPageAnchor(ppt);
    final double textHeight;

    if (StringUtils.isNotBlank(title)) {
        textHeight = 0.1 * pageAnchor.getHeight();

        final XSLFTextBox textBox = sl.createTextBox();
        textBox.clearText();//from  ww w .  ja  va  2 s.com
        final XSLFTextParagraph paragraph = textBox.addNewTextParagraph();
        paragraph.setTextAlign(TextParagraph.TextAlign.CENTER);
        paragraph.addNewTextRun().setText(title);
        textBox.setHorizontalCentered(true);
        textBox.setTextAutofit(TextShape.TextAutofit.SHAPE);
        textBox.setAnchor(startingSpace(pageAnchor, textHeight));
    } else {
        textHeight = 0;
    }

    final XSLFPictureData picture = addPictureData(imageSource, ppt, image);
    addMap(sl, remainingSpace(pageAnchor, textHeight), picture, map.getMarkers(), map.getPolygons());

    return ppt;
}

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

License:MIT License

/**
 * Internal implementation to add a list of documents to a presentation; either as a single slide or a series of slides.
 * @param imageSource the image source to convert images to data.
 * @param ppt the presentation to add to.
 * @param sl the slide to add to (can be null if pagination is enabled).
 * @param anchor bounding rectangle to draw onto, in PowerPoint coordinates.
 * @param paginate whether to render results as multiple slides if they don't fit on one slide.
 * @param data the documents to render./*from  ww w  .  java2  s .  co  m*/
 * @param results optional string to render into the top-left corner of the available space.
 *                  Will appear on each page if pagination is enabled.
 * @param sortBy optional string to render into the top-right corner of the available space.
 *                  Will appear on each page if pagination is enabled.
 */
private static void addList(final ImageSource imageSource, final XMLSlideShow ppt, XSLFSlide sl,
        final Rectangle2D.Double anchor, final boolean paginate, final ListData data, final String results,
        final String sortBy) {
    final double
    // How much space to leave at the left and right edge of the slide
    xMargin = 20,
            // How much space to leave at the top
            yMargin = 5,
            // Size of the icon
            iconWidth = 20, iconHeight = 24,
            // Find's thumbnail height is 97px by 55px, hardcoded in the CSS in .document-thumbnail
            thumbScale = 0.8, thumbW = 97 * thumbScale, thumbH = 55 * thumbScale,
            // Margin around the thumbnail
            thumbMargin = 4.,
            // Space between list items
            listItemMargin = 5.;

    final Pattern highlightPattern = Pattern
            .compile("<HavenSearch-QueryText-Placeholder>(.*?)</HavenSearch-QueryText-Placeholder>");

    double yCursor = yMargin + anchor.getMinY(), xCursor = xMargin + anchor.getMinX();

    int docsOnPage = 0;

    final Document[] docs = data.getDocs();
    for (int docIdx = 0; docIdx < docs.length; ++docIdx) {
        final Document doc = docs[docIdx];

        if (sl == null) {
            sl = ppt.createSlide();
            yCursor = yMargin + anchor.getMinY();
            xCursor = xMargin + anchor.getMinX();
            docsOnPage = 0;

            double yStep = 0;

            if (StringUtils.isNotBlank(results)) {
                final XSLFTextBox textBox = sl.createTextBox();
                textBox.clearText();
                final Rectangle2D.Double textBounds = new Rectangle2D.Double(xCursor, yCursor,
                        Math.max(0, anchor.getMaxX() - xCursor - xMargin), 20);
                textBox.setAnchor(textBounds);

                addTextRun(textBox.addNewTextParagraph(), results, 12., Color.LIGHT_GRAY);

                yStep = textBox.getTextHeight();
            }

            if (StringUtils.isNotBlank(sortBy)) {
                final XSLFTextBox sortByEl = sl.createTextBox();
                sortByEl.clearText();
                final XSLFTextParagraph sortByText = sortByEl.addNewTextParagraph();
                sortByText.setTextAlign(TextParagraph.TextAlign.RIGHT);

                addTextRun(sortByText, sortBy, 12., Color.LIGHT_GRAY);

                sortByEl.setAnchor(new Rectangle2D.Double(xCursor, yCursor,
                        Math.max(0, anchor.getMaxX() - xCursor - xMargin), 20));

                yStep = Math.max(sortByEl.getTextHeight(), yStep);
            }

            if (yStep > 0) {
                yCursor += listItemMargin + yStep;
            }
        }

        XSLFAutoShape icon = null;
        if (data.isDrawIcons()) {
            icon = sl.createAutoShape();
            icon.setShapeType(ShapeType.SNIP_1_RECT);
            icon.setAnchor(new Rectangle2D.Double(xCursor, yCursor + listItemMargin, iconWidth, iconHeight));
            icon.setLineColor(Color.decode("#888888"));
            icon.setLineWidth(2.0);

            xCursor += iconWidth;
        }

        final XSLFTextBox listEl = sl.createTextBox();
        listEl.clearText();
        listEl.setAnchor(new Rectangle2D.Double(xCursor, yCursor,
                Math.max(0, anchor.getMaxX() - xCursor - xMargin), Math.max(0, anchor.getMaxY() - yCursor)));

        final XSLFTextParagraph titlePara = listEl.addNewTextParagraph();
        addTextRun(titlePara, doc.getTitle(), data.getTitleFontSize(), Color.BLACK).setBold(true);

        if (StringUtils.isNotBlank(doc.getDate())) {
            final XSLFTextParagraph datePara = listEl.addNewTextParagraph();
            datePara.setLeftMargin(5.);
            addTextRun(datePara, doc.getDate(), data.getDateFontSize(), Color.GRAY).setItalic(true);
        }

        if (StringUtils.isNotBlank(doc.getRef())) {
            addTextRun(listEl.addNewTextParagraph(), doc.getRef(), data.getRefFontSize(), Color.GRAY);
        }

        final double thumbnailOffset = listEl.getTextHeight();

        final XSLFTextParagraph contentPara = listEl.addNewTextParagraph();

        Rectangle2D.Double pictureAnchor = null;
        XSLFPictureData pictureData = null;

        if (StringUtils.isNotBlank(doc.getThumbnail())) {
            try {
                // Picture reuse is automatic
                pictureData = addPictureData(imageSource, ppt, doc.getThumbnail());
                // We reserve space for the picture, but we don't actually add it yet.
                // The reason is we may have to remove it later if it doesn't fit; but due to a quirk of OpenOffice,
                //   deleting the picture shape removes the pictureData as well; which is a problem since the
                //   pictureData can be shared between multiple pictures.
                pictureAnchor = new Rectangle2D.Double(xCursor, yCursor + thumbnailOffset + thumbMargin, thumbW,
                        thumbH);

                // If there is enough horizontal space, put the text summary to the right of the thumbnail image,
                //    otherwise put it under the thumbnail,
                if (listEl.getAnchor().getWidth() > 2.5 * thumbW) {
                    contentPara.setLeftMargin(thumbW);
                } else {
                    contentPara.addLineBreak().setFontSize(thumbH);
                }

            } catch (RuntimeException e) {
                // if there's any errors, we'll just ignore the image
            }
        }

        final String rawSummary = doc.getSummary();
        if (StringUtils.isNotBlank(rawSummary)) {
            // HTML treats newlines and multiple whitespace as a single whitespace.
            final String summary = rawSummary.replaceAll("\\s+", " ");
            final Matcher matcher = highlightPattern.matcher(summary);
            int idx = 0;

            while (matcher.find()) {
                final int start = matcher.start();

                if (idx < start) {
                    addTextRun(contentPara, summary.substring(idx, start), data.getSummaryFontSize(),
                            Color.DARK_GRAY);
                }

                addTextRun(contentPara, matcher.group(1), data.getSummaryFontSize(), Color.DARK_GRAY)
                        .setBold(true);
                idx = matcher.end();
            }

            if (idx < summary.length()) {
                addTextRun(contentPara, summary.substring(idx), data.getSummaryFontSize(), Color.DARK_GRAY);
            }
        }

        double elHeight = Math.max(listEl.getTextHeight(), iconHeight);
        if (pictureAnchor != null) {
            elHeight = Math.max(elHeight, pictureAnchor.getMaxY() - yCursor);
        }

        yCursor += elHeight;
        xCursor = xMargin + anchor.getMinX();

        docsOnPage++;

        if (yCursor > anchor.getMaxY()) {
            if (docsOnPage > 1) {
                // If we drew more than one list element on this page; and we exceeded the available space,
                //   delete the last element's shapes and redraw it on the next page.
                // We don't have to remove the picture since we never added it.
                sl.removeShape(listEl);
                if (icon != null) {
                    sl.removeShape(icon);
                }

                --docIdx;
            } else if (pictureAnchor != null) {
                // We've confirmed we need the picture, add it.
                sl.createPicture(pictureData).setAnchor(pictureAnchor);
            }

            sl = null;

            if (!paginate) {
                break;
            }
        } else {
            yCursor += listItemMargin;

            if (pictureAnchor != null) {
                // We've confirmed we need the picture, add it.
                sl.createPicture(pictureData).setAnchor(pictureAnchor);
            }
        }
    }
}

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

License:MIT License

@Override
public XMLSlideShow graph(final DategraphData data) throws TemplateLoadException {
    final SlideShowTemplate template = loadTemplate();
    final XMLSlideShow ppt = template.getSlideShow();
    final int shapeId = 1;
    final String relId = "relId" + shapeId;

    addDategraph(template, ppt.createSlide(), null, data, shapeId, relId);

    return ppt;/*  w w  w  .  j a va  2 s. c om*/
}

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

License:MIT License

@Override
public XMLSlideShow report(final ReportData report, final boolean slidePerVisualizer)
        throws TemplateLoadException {
    final SlideShowTemplate template = loadTemplate();
    final XMLSlideShow ppt = template.getSlideShow();

    final Rectangle2D.Double pageAnchor = createPageAnchor(ppt);
    double width = pageAnchor.getWidth();
    double height = pageAnchor.getHeight();

    if (!slidePerVisualizer) {
        // If drawing multiple visualizations on a single slide, we need to render the charts first since adding
        //   chart objects directly via XML after calling slide.getShapes() or createShape() etc. will break things.
        Arrays.sort(report.getChildren(), Comparator.comparingInt(PowerPointServiceImpl::prioritizeCharts));
    }/*from   w  w w.  j a va 2  s  .com*/

    // As above, we need to have a separate slide to place our sizing textbox for calculations.
    XSLFSlide sizingSlide = ppt.createSlide();
    // This is the slide to draw on.
    XSLFSlide slide = ppt.createSlide();

    int shapeId = 1;
    boolean first = true;

    for (final ReportData.Child child : report.getChildren()) {
        if (slidePerVisualizer && !first) {
            sizingSlide = ppt.createSlide();
            slide = ppt.createSlide();
        }

        first = false;

        final ComposableElement data = child.getData();
        final Rectangle2D.Double anchor = new Rectangle2D.Double(pageAnchor.getMinX() + width * child.getX(),
                pageAnchor.getMinY() + height * child.getY(), width * child.getWidth(),
                height * child.getHeight());

        if (child.getMargin() >= 0) {
            final double margin = child.getMargin();
            final double marginX2 = margin * 2;
            final double textMargin = child.getTextMargin();

            if (anchor.getWidth() > marginX2) {
                double xCursor = anchor.getMinX() + margin, xWidthAvail = anchor.getWidth() - marginX2,
                        yCursor = anchor.getMinY() + margin, yHeightAvail = anchor.getHeight() - marginX2;
                XSLFTextBox sizingBox = null;

                final String title = child.getTitle();
                if (StringUtils.isNotEmpty(title) && yHeightAvail > 0) {
                    sizingBox = sizingSlide.createTextBox();
                    final Rectangle2D.Double sizingAnchor = new Rectangle2D.Double(xCursor, yCursor,
                            xWidthAvail, yHeightAvail);
                    sizingBox.setAnchor(sizingAnchor);
                    sizingBox.clearText();
                    addTextRun(sizingBox.addNewTextParagraph(), title, child.getFontSize(), Color.BLACK)
                            .setFontFamily(child.getFontFamily());

                    final double textHeight = sizingBox.getTextHeight() + textMargin;
                    yCursor += textHeight;
                    yHeightAvail -= textHeight;
                }

                if (yHeightAvail > 0) {
                    anchor.setRect(xCursor, yCursor, xWidthAvail, yHeightAvail);
                } else if (sizingBox != null) {
                    sizingSlide.removeShape(sizingBox);
                }
            }
        }

        if (data instanceof DategraphData) {
            addDategraph(template, slide, anchor, (DategraphData) data, shapeId, "relId" + shapeId);
            shapeId++;
        } else if (data instanceof ListData) {
            final ListData listData = (ListData) data;
            addList(imageSource, ppt, slide, anchor, false, listData, null, null);
        } else if (data instanceof MapData) {
            final MapData mapData = (MapData) data;
            addMap(slide, anchor, addPictureData(imageSource, ppt, mapData.getImage()), mapData.getMarkers(),
                    mapData.getPolygons());
        } else if (data instanceof SunburstData) {
            addSunburst(template, slide, anchor, (SunburstData) data, shapeId, "relId" + shapeId);
            shapeId++;
        } else if (data instanceof TableData) {
            final TableData tableData = (TableData) data;
            addTable(slide, anchor, tableData.getRows(), tableData.getCols(), tableData.getCells(), true);
        } else if (data instanceof TopicMapData) {
            addTopicMap(slide, anchor, (TopicMapData) data);
        } else if (data instanceof TextData) {
            addTextData(slide, anchor, (TextData) data);
        }

        if (slidePerVisualizer) {
            transferSizedTextboxes(ppt, slide, sizingSlide);
        }
    }

    if (!slidePerVisualizer) {
        transferSizedTextboxes(ppt, slide, sizingSlide);
    }

    return ppt;
}