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

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

Introduction

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

Prototype

@Override
public List<XSLFSlide> getSlides() 

Source Link

Document

Return all the slides in the slideshow

Usage

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 w w  w  .  j  ava2  s  .co m
    }

    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:org.olat.search.service.document.file.PowerPointOOXMLDocument.java

License:Apache License

private void extractContent(final StringBuilder buffy, final XMLSlideShow xmlSlideShow)
        throws IOException, XmlException {
    final XSLFSlide[] slides = xmlSlideShow.getSlides();
    for (final XSLFSlide slide : slides) {
        final CTSlide rawSlide = slide._getCTSlide();
        final CTSlideIdListEntry slideId = slide._getCTSlideId();

        final CTNotesSlide notes = xmlSlideShow._getXSLFSlideShow().getNotes(slideId);
        final CTCommentList comments = xmlSlideShow._getXSLFSlideShow().getSlideComments(slideId);

        extractShapeContent(buffy, rawSlide.getCSld().getSpTree());

        if (comments != null) {
            for (final CTComment comment : comments.getCmArray()) {
                buffy.append(comment.getText()).append(' ');
            }/*ww w  .  j av a2  s .  c o m*/
        }

        if (notes != null) {
            extractShapeContent(buffy, notes.getCSld().getSpTree());
        }
    }
}

From source file:poi.xslf.usermodel.DataExtraction.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  ww  w .  jav  a  2  s.c om
    }

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

    // Get the document's embedded files.
    List<PackagePart> embeds = ppt.getAllEmbedds();
    for (PackagePart p : embeds) {
        String type = p.getContentType();
        String name = p.getPartName().getName(); //typically file name

        InputStream pIs = p.getInputStream();
        // make sense of the part data
        pIs.close();

    }

    // Get the document's embedded files.
    List<XSLFPictureData> images = ppt.getAllPictures();
    for (XSLFPictureData data : images) {
        PackagePart p = data.getPackagePart();

        String type = p.getContentType();
        String name = data.getFileName();

        InputStream pIs = p.getInputStream();
        // make sense of the image data
        pIs.close();

    }

    Dimension pageSize = ppt.getPageSize(); // size of the canvas in points
    for (XSLFSlide slide : ppt.getSlides()) {
        for (XSLFShape shape : slide) {
            Rectangle2D anchor = shape.getAnchor(); // position on the canvas
            if (shape instanceof XSLFTextShape) {
                XSLFTextShape txShape = (XSLFTextShape) shape;
                System.out.println(txShape.getText());
            } else if (shape instanceof XSLFPictureShape) {
                XSLFPictureShape pShape = (XSLFPictureShape) shape;
                XSLFPictureData pData = pShape.getPictureData();
                System.out.println(pData.getFileName());
            } else {
                System.out.println("Process me: " + shape.getClass());
            }
        }
    }
}

From source file:poi.xslf.usermodel.MergePresentations.java

License:Apache License

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

    for (String arg : args) {
        FileInputStream is = new FileInputStream(arg);
        XMLSlideShow src = new XMLSlideShow(is);
        is.close();//w  w  w  .j a  va 2  s.c o m

        for (XSLFSlide srcSlide : src.getSlides()) {
            ppt.createSlide().importContent(srcSlide);
        }
    }

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

From source file:poi.xslf.usermodel.PieChartDemo.java

License:Apache License

public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        usage();/*  w w w.j av a2  s .  c  o m*/
        return;
    }

    BufferedReader modelReader = new BufferedReader(new FileReader(args[1]));

    String chartTitle = modelReader.readLine(); // first line is chart title

    XMLSlideShow pptx = new XMLSlideShow(new FileInputStream(args[0]));
    XSLFSlide slide = pptx.getSlides()[0];

    // find chart in the slide
    XSLFChart chart = null;
    for (POIXMLDocumentPart part : slide.getRelations()) {
        if (part instanceof XSLFChart) {
            chart = (XSLFChart) part;
            break;
        }
    }

    if (chart == null)
        throw new IllegalStateException("chart not found in the template");

    // embedded Excel workbook that holds the chart data
    POIXMLDocumentPart xlsPart = chart.getRelations().get(0);
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet();

    CTChart ctChart = chart.getCTChart();
    CTPlotArea plotArea = ctChart.getPlotArea();

    CTPieChart pieChart = plotArea.getPieChartArray(0);
    //Pie Chart Series
    CTPieSer ser = pieChart.getSerArray(0);

    // Series Text
    CTSerTx tx = ser.getTx();
    tx.getStrRef().getStrCache().getPtArray(0).setV(chartTitle);
    sheet.createRow(0).createCell(1).setCellValue(chartTitle);
    String titleRef = new CellReference(sheet.getSheetName(), 0, 1, true, true).formatAsString();
    tx.getStrRef().setF(titleRef);

    // Category Axis Data
    CTAxDataSource cat = ser.getCat();
    CTStrData strData = cat.getStrRef().getStrCache();

    // Values
    CTNumDataSource val = ser.getVal();
    CTNumData numData = val.getNumRef().getNumCache();

    strData.setPtArray(null); // unset old axis text
    numData.setPtArray(null); // unset old values

    // set model
    int idx = 0;
    int rownum = 1;
    String ln;
    while ((ln = modelReader.readLine()) != null) {
        String[] vals = ln.split("\\s+");
        CTNumVal numVal = numData.addNewPt();
        numVal.setIdx(idx);
        numVal.setV(vals[1]);

        CTStrVal sVal = strData.addNewPt();
        sVal.setIdx(idx);
        sVal.setV(vals[0]);

        idx++;
        XSSFRow row = sheet.createRow(rownum++);
        row.createCell(0).setCellValue(vals[0]);
        row.createCell(1).setCellValue(Double.valueOf(vals[1]));
    }
    numData.getPtCount().setVal(idx);
    strData.getPtCount().setVal(idx);

    String numDataRange = new CellRangeAddress(1, rownum - 1, 1, 1).formatAsString(sheet.getSheetName(), true);
    val.getNumRef().setF(numDataRange);
    String axisDataRange = new CellRangeAddress(1, rownum - 1, 0, 0).formatAsString(sheet.getSheetName(), true);
    cat.getStrRef().setF(axisDataRange);

    // updated the embedded workbook with the data
    OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream();
    wb.write(xlsOut);
    xlsOut.close();

    // save the result
    FileOutputStream out = new FileOutputStream("pie-chart-demo-output.pptx");
    pptx.write(out);
    out.close();
}

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;/*  www  .  j  a va 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());
                    }
                }
            }
        }
    }
}

From source file:pptxtopdf.TestPptxToPdf.java

public static void main(String[] args)
        throws FileNotFoundException, IOException, COSVisitorException, OpenXML4JException {
    // TODO code application logic here
    String filepath = "/home/sagar/Desktop/Shareback/test.pptx";
    FileInputStream is = new FileInputStream(filepath);
    XMLSlideShow pptx = new XMLSlideShow(is);
    Dimension pgsize = pptx.getPageSize();

    int idx = 1;/*  w  ww  . ja va 2  s  . c om*/
    for (XSLFSlide slide : pptx.getSlides()) {

        BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = img.createGraphics();
        // clear the drawing area
        graphics.setPaint(Color.white);
        graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

        // render
        slide.draw(graphics);

        // save the output
        FileOutputStream out = new FileOutputStream("/home/sagar/Desktop/Shareback/img/slide-" + idx + ".jpg");
        javax.imageio.ImageIO.write(img, "jpg", out);
        out.close();

        idx++;
    }

    String someimg = "/home/sagar/Desktop/Shareback/pptx/img/";

    PDDocument document = new PDDocument();
    File file = new File(someimg);
    if (!file.exists())
        file.mkdir();

    if (file.isDirectory()) {
        for (File f : file.listFiles()) {

            InputStream in = new FileInputStream(f);

            BufferedImage bimg = ImageIO.read(in);
            float width = bimg.getWidth();
            float height = bimg.getHeight();
            PDPage page = new PDPage(new PDRectangle(width + 10, height + 10));
            document.addPage(page);
            PDXObjectImage img = new PDJpeg(document, new FileInputStream(f));
            PDPageContentStream contentStream = new PDPageContentStream(document, page);
            contentStream.drawImage(img, 0, 0);
            contentStream.close();
            in.close();
        }

        document.save("/home/sagar/Desktop/Shareback/test-generated-pptx.pdf");
        document.close();
    } else {
        System.out.println(someimg + "is not a Directory");
    }

}

From source file:testppttopdf.TestPptToPdf.java

static void testPptxToPdf() {
    String filepath = "/home/sagar/Desktop/Shareback/test.pptx";
    FileInputStream is;/*from   w w  w.  ja v  a2 s  .  c om*/
    try {
        is = new FileInputStream(filepath);

        XMLSlideShow pptx = new XMLSlideShow(is);
        Dimension pgsize = pptx.getPageSize();

        int idx = 1;
        for (XSLFSlide slide : pptx.getSlides()) {

            BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = img.createGraphics();
            // clear the drawing area
            graphics.setPaint(Color.white);
            graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

            // render
            slide.draw(graphics);

            // save the output
            FileOutputStream out = new FileOutputStream(
                    "/home/sagar/Desktop/Shareback/img/slide-" + idx + ".jpg");
            javax.imageio.ImageIO.write(img, "jpg", out);
            out.close();

            idx++;
        }

        String someimg = "/home/sagar/Desktop/Shareback/pptx/img/";

        PDDocument document = new PDDocument();
        File file = new File(someimg);
        if (!file.exists())
            file.mkdir();

        if (file.isDirectory()) {
            for (File f : file.listFiles()) {

                InputStream in = new FileInputStream(f);

                BufferedImage bimg = ImageIO.read(in);
                float width = bimg.getWidth();
                float height = bimg.getHeight();
                PDPage page = new PDPage(new PDRectangle(width + 10, height + 10));
                document.addPage(page);
                PDXObjectImage img = new PDJpeg(document, new FileInputStream(f));
                PDPageContentStream contentStream = new PDPageContentStream(document, page);
                contentStream.drawImage(img, 0, 0);
                contentStream.close();
                in.close();
            }

            document.save("/home/sagar/Desktop/Shareback/test-generated-pptx.pdf");
            document.close();
        } else {
            System.out.println(someimg + "is not a Directory");
        }

    } catch (Exception ex) {
        Logger.getLogger(TestPptToPdf.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:testpptxtopdf.TestPptxToPdf.java

static void pptxToPdf() throws Exception {
    String dir = "/home/sagar/Desktop/Shareback/";
    String filepath = dir + "test.pptx";

    //creating an empty presentation
    File file = new File(filepath);
    XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file));

    //getting the dimensions and size of the slide 
    Dimension pgsize = ppt.getPageSize();
    XSLFSlide[] slide = ppt.getSlides();

    int idx = 1;/*ww w . java 2 s  . c o  m*/
    for (int i = 0; i < slide.length; i++) {
        BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = img.createGraphics();

        //clear the drawing area
        graphics.setPaint(Color.white);
        graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

        //render
        slide[i].draw(graphics);

        //creating an image file as output

        File f = new File(dir + "ptx/");
        if (!f.exists()) {
            f.mkdir();
        }
        FileOutputStream out = new FileOutputStream(f.getAbsolutePath() + "img" + idx + ".png");

        javax.imageio.ImageIO.write(img, "png", out);
        ppt.write(out);

        System.out.println("Image successfully created");
        out.close();
        idx++;
    }
}