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

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

Introduction

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

Prototype

@SuppressWarnings("resource")
public final void write(OutputStream stream) throws IOException 

Source Link

Document

Write out this document to an Outputstream.

Usage

From source file:org.maptalks.poi.animation.TestXSLFAnimationShape.java

License:Apache License

@Test
public void testAnimationSlide() throws Exception {
    XMLSlideShow pptx = new XMLSlideShow();
    XSLFSlide slide = pptx.createSlide();
    Dimension pageSize = pptx.getPageSize();
    //        Rectangle2D rectangle = new Rectangle2D.Double(0,0,pageSize.getWidth(),pageSize.getHeight());
    Rectangle rectangle = new java.awt.Rectangle(0, 0, (int) pageSize.getWidth(), (int) pageSize.getHeight());
    List<XSLFAnimationType> animationTypes = new ArrayList<XSLFAnimationType>();
    XSLFAnimation animation = new XSLFAnimation();
    String[] directions = new String[5];
    directions[0] = MoveDirection.BOTTOM;
    directions[1] = MoveDirection.LEFT;/*  w ww .ja v a  2 s.  c o  m*/
    directions[2] = MoveDirection.TOP;
    directions[3] = MoveDirection.RIGHT;
    directions[4] = MoveDirection.BOTTOM;
    String pathStr = this.getClass().getResource("/images").getPath();
    for (int i = 0; i < 3; i++) {
        String fileName = pathStr + "/" + (i + 1) + ".png";
        File downloadeFile = new File(fileName);
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(downloadeFile));
        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
        int size = 0;
        byte[] temp = new byte[1024];
        while ((size = in.read(temp)) != -1) {
            out.write(temp, 0, size);
        }
        in.close();
        byte[] content = out.toByteArray();
        XSLFPictureData pictureData = pptx.addPicture(content,
                org.apache.poi.sl.usermodel.PictureData.PictureType.PNG);
        XSLFPictureShape picShape = slide.createPicture(pictureData);
        picShape.setAnchor(rectangle);
        picShape.setLineWidth(0);
        XSLFAnimationType animationType = new FlyIn(picShape, directions[i]);
        animationTypes.add(animationType);

        XSLFAnimationType animationOutType = new FlyOut(picShape, directions[i]);
        animationTypes.add(animationOutType);
    }
    slide = animation.addAnimationToSlide(slide, animationTypes);
    FileOutputStream out = new FileOutputStream("d:\\temp.pptx");
    //        FileOutputStream out = new FileOutputStream(pathStr+"/temp.pptx");
    pptx.write(out);
    out.close();
}

From source file:org.maptalks.poi.animation.TestZoomIn.java

License:Apache License

@Test
public void testAnimationSlide() throws Exception {
    XMLSlideShow pptx = new XMLSlideShow();
    XSLFSlide slide = pptx.createSlide();
    Dimension pageSize = pptx.getPageSize();
    Rectangle rectangle = new java.awt.Rectangle(0, 0, (int) pageSize.getWidth(), (int) pageSize.getHeight());
    //        Rectangle2D rectangle = new Rectangle2D.Double();
    List<XSLFAnimationType> animationTypes = new ArrayList<XSLFAnimationType>();
    XSLFAnimation animation = new XSLFAnimation();
    String[] directions = new String[5];
    directions[0] = MoveDirection.BOTTOM;
    directions[1] = MoveDirection.LEFT;//  w  w  w . j  ava 2s  . c o  m
    directions[2] = MoveDirection.TOP;
    directions[3] = MoveDirection.RIGHT;
    directions[4] = MoveDirection.BOTTOM;
    String pathStr = this.getClass().getResource("/images").getPath();
    for (int i = 0; i < 3; i++) {
        String fileName = pathStr + "/" + (i + 1) + ".png";
        File downloadeFile = new File(fileName);
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(downloadeFile));
        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
        int size = 0;
        byte[] temp = new byte[1024];
        while ((size = in.read(temp)) != -1) {
            out.write(temp, 0, size);
        }
        in.close();
        byte[] content = out.toByteArray();
        XSLFPictureData pictureData = pptx.addPicture(content,
                org.apache.poi.sl.usermodel.PictureData.PictureType.PNG);
        XSLFPictureShape picShape = slide.createPicture(pictureData);
        picShape.setAnchor(rectangle);
        picShape.setLineWidth(0);
        XSLFAnimationType animationType = new ZoomIn(picShape, directions[i]);
        animationTypes.add(animationType);

        XSLFAnimationType animationOutType = new FlyOut(picShape, directions[i]);
        animationTypes.add(animationOutType);
    }
    slide = animation.addAnimationToSlide(slide, animationTypes);
    FileOutputStream out = new FileOutputStream("d:\\zoomin.pptx");
    //        FileOutputStream out = new FileOutputStream(pathStr+"/zoomin.pptx");
    pptx.write(out);
    out.close();
}

From source file:org.maptalks.poi.animation.TestZoomOut.java

License:Apache License

@Test
public void testAnimationSlide() throws Exception {
    XMLSlideShow pptx = new XMLSlideShow();
    XSLFSlide slide = pptx.createSlide();
    Dimension pageSize = pptx.getPageSize();
    //        Rectangle2D rectangle = new Rectangle2D.Double(0,0,pageSize.getWidth(),pageSize.getHeight());
    Rectangle rectangle = new java.awt.Rectangle(0, 0, (int) pageSize.getWidth(), (int) pageSize.getHeight());
    List<XSLFAnimationType> animationTypes = new ArrayList<XSLFAnimationType>();
    XSLFAnimation animation = new XSLFAnimation();
    String[] directions = new String[5];
    directions[0] = MoveDirection.BOTTOM;
    directions[1] = MoveDirection.LEFT;//  w w w  .j av  a2s .c  o m
    directions[2] = MoveDirection.TOP;
    directions[3] = MoveDirection.RIGHT;
    directions[4] = MoveDirection.BOTTOM;
    String pathStr = this.getClass().getResource("/images").getPath();
    for (int i = 0; i < 3; i++) {
        String fileName = pathStr + "/" + (i + 1) + ".png";
        File downloadeFile = new File(fileName);
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(downloadeFile));
        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
        int size = 0;
        byte[] temp = new byte[1024];
        while ((size = in.read(temp)) != -1) {
            out.write(temp, 0, size);
        }
        in.close();
        byte[] content = out.toByteArray();
        XSLFPictureData pictureData = pptx.addPicture(content,
                org.apache.poi.sl.usermodel.PictureData.PictureType.PNG);
        XSLFPictureShape picShape = slide.createPicture(pictureData);
        picShape.setAnchor(rectangle);
        picShape.setLineWidth(0);
        XSLFAnimationType animationType = new ZoomIn(picShape, directions[i]);
        animationTypes.add(animationType);

        XSLFAnimationType animationOutType = new ZoomOut(picShape, directions[i]);
        animationTypes.add(animationOutType);
    }
    slide = animation.addAnimationToSlide(slide, animationTypes);
    FileOutputStream out = new FileOutputStream("d:\\zoomout.pptx");
    //        FileOutputStream out = new FileOutputStream(pathStr+"/zoomout.pptx");
    pptx.write(out);
    out.close();
}

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();/*from ww w.  j av a 2s . 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. ja  va 2 s  . co  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.Step2.java

License:Apache License

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

    // first see what slide layouts are available by default
    System.out.println("Available slide layouts:");
    for (XSLFSlideMaster master : ppt.getSlideMasters()) {
        for (XSLFSlideLayout layout : master.getSlideLayouts()) {
            System.out.println(layout.getType());
        }//from ww  w  .j  a v  a2s .  co m
    }

    // blank slide
    XSLFSlide blankSlide = ppt.createSlide();

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

    // title slide
    XSLFSlideLayout titleLayout = defaultMaster.getLayout(SlideLayout.TITLE);
    XSLFSlide slide1 = ppt.createSlide(titleLayout);
    XSLFTextShape title1 = slide1.getPlaceholder(0);
    title1.setText("First Title");

    // title and content
    XSLFSlideLayout titleBodyLayout = defaultMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);
    XSLFSlide slide2 = ppt.createSlide(titleBodyLayout);

    XSLFTextShape title2 = slide2.getPlaceholder(0);
    title2.setText("Second Title");

    XSLFTextShape body2 = slide2.getPlaceholder(1);
    body2.clearText(); // unset any existing text
    body2.addNewTextParagraph().addNewTextRun().setText("First paragraph");
    body2.addNewTextParagraph().addNewTextRun().setText("Second paragraph");
    body2.addNewTextParagraph().addNewTextRun().setText("Third paragraph");

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

}

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

License:Apache License

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

    XSLFSlide slide1 = ppt.createSlide();
    XSLFTextBox shape1 = slide1.createTextBox();
    // initial height of the text box is 100 pt but
    Rectangle anchor = new Rectangle(10, 100, 300, 100);
    shape1.setAnchor(anchor);/*ww  w. j  ava2  s  . c o m*/

    XSLFTextParagraph p1 = shape1.addNewTextParagraph();
    XSLFTextRun r1 = p1.addNewTextRun();
    r1.setText("Paragraph Formatting");
    r1.setFontSize(24);
    r1.setFontColor(new Color(85, 142, 213));

    XSLFTextParagraph p2 = shape1.addNewTextParagraph();
    // If spaceBefore >= 0, then space is a percentage of normal line height.
    // If spaceBefore < 0, the absolute value of linespacing is the spacing in points
    p2.setSpaceBefore(-20); // 20 pt from the previous paragraph
    p2.setSpaceAfter(300); // 3 lines after the paragraph
    XSLFTextRun r2 = p2.addNewTextRun();
    r2.setText("Paragraph  properties apply to all text residing within the corresponding paragraph.");
    r2.setFontSize(16);

    XSLFTextParagraph p3 = shape1.addNewTextParagraph();

    XSLFTextRun r3 = p3.addNewTextRun();
    r3.setText("Run Formatting");
    r3.setFontSize(24);
    r3.setFontColor(new Color(85, 142, 213));

    XSLFTextParagraph p4 = shape1.addNewTextParagraph();
    p4.setSpaceBefore(-20); // 20 pt from the previous paragraph
    p4.setSpaceAfter(300); // 3 lines after the paragraph
    XSLFTextRun r4 = p4.addNewTextRun();
    r4.setFontSize(16);
    r4.setText("Run level formatting is the most granular property level and allows "
            + "for the specifying of all low level text properties. The text run is "
            + "what all paragraphs are derived from and thus specifying various "
            + "properties per run will allow for a diversely formatted text paragraph.");

    // resize the shape to fit text
    shape1.resizeToFitText();

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

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

License:Apache License

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

    XSLFSlide slide = ppt.createSlide();

    XSLFTextShape titleShape = slide.createTextBox();
    titleShape.setPlaceholder(Placeholder.TITLE);
    titleShape.setText("This is a slide title");
    titleShape.setAnchor(new Rectangle(50, 50, 400, 100));

    FileOutputStream out = new FileOutputStream("title.pptx");
    ppt.write(out);
    out.close();/* w  w w  .j av  a 2 s  .  c  om*/
}

From source file:poi.xslf.usermodel.Tutorial4.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 slide = ppt.createSlide();

    XSLFTable tbl = slide.createTable();
    tbl.setAnchor(new Rectangle2D.Double(50, 50, 450, 300));

    int numColumns = 3;
    int numRows = 5;
    XSLFTableRow headerRow = tbl.addRow();
    headerRow.setHeight(50);//w w w.  j a  v  a2s .com
    // header
    for (int i = 0; i < numColumns; i++) {
        XSLFTableCell th = headerRow.addCell();
        XSLFTextParagraph p = th.addNewTextParagraph();
        p.setTextAlign(TextAlign.CENTER);
        XSLFTextRun r = p.addNewTextRun();
        r.setText("Header " + (i + 1));
        r.setBold(true);
        r.setFontColor(Color.white);
        th.setFillColor(new Color(79, 129, 189));
        th.setBorderBottom(2);
        th.setBorderBottomColor(Color.white);

        tbl.setColumnWidth(i, 150); // all columns are equally sized
    }

    // rows

    for (int rownum = 0; rownum < numRows; rownum++) {
        XSLFTableRow tr = tbl.addRow();
        tr.setHeight(50);
        // header
        for (int i = 0; i < numColumns; i++) {
            XSLFTableCell cell = tr.addCell();
            XSLFTextParagraph p = cell.addNewTextParagraph();
            XSLFTextRun r = p.addNewTextRun();

            r.setText("Cell " + (i + 1));
            if (rownum % 2 == 0)
                cell.setFillColor(new Color(208, 216, 232));
            else
                cell.setFillColor(new Color(233, 247, 244));

        }

    }

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

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

License:Apache License

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

    XSLFSlide slide = ppt.createSlide();
    File img = new File(System.getProperty("POI.testdata.path"), "slideshow/clock.jpg");
    byte[] data = IOUtils.toByteArray(new FileInputStream(img));
    int pictureIndex = ppt.addPicture(data, XSLFPictureData.PICTURE_TYPE_PNG);

    XSLFPictureShape shape = slide.createPicture(pictureIndex);

    FileOutputStream out = new FileOutputStream("images.pptx");
    ppt.write(out);
    out.close();/*from w w  w  .  j  a v  a  2 s. co  m*/
}