Apache | POI |
Home |
|
XSLF CookbookXSLF CookbookThis page offers a short introduction into the XSLF API. More examples can be found in the XSLF Examples in the POI SVN repository. Note
Please note that XSLF is still in early development and is a subject to incompatible changes in a future release.
Index of Features
CookbokNew PresentationThe following code creates a new .pptx slide show and adds a blank slide to it: //create a new empty slide show XMLSlideShow ppt = new XMLSlideShow(); //add first slide XSLFSlide blankSlide = ppt.createSlide(); Read an existing presentation and append a slide to itXMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx")); //append a new slide to the end XSLFSlide blankSlide = ppt.createSlide(); Create a new slide from a predefined slide layoutXMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx")); // first see what slide layouts are available : System.out.println("Available slide layouts:"); for(XSLFSlideMaster master : ppt.getSlideMasters()){ for(XSLFSlideLayout layout : master.getSlideLayouts()){ System.out.println(layout.getType()); } } // blank slide XSLFSlide blankSlide = ppt.createSlide(); // there can be multiple masters each referencing a number of layouts // for demonstration purposes we use the first (default) slide master XSLFSlideMaster defaultMaster = ppt.getSlideMasters().get(0); // title slide XSLFSlideLayout titleLayout = defaultMaster.getLayout(SlideLayout.TITLE); // fill the placeholders 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"); Delete slideXMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx")); ppt.removeSlide(0); // 0-based index of a slide to be removed Re-order slidesXMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx")); List<XSLFSlide> slides = ppt.getSlides(); XSLFSlide thirdSlide = slides.get(2); ppt.setSlideOrder(thirdSlide, 0); // move the third slide to the beginning How to retrieve or change slide sizeXMLSlideShow ppt = new XMLSlideShow(); //retrieve page size. Coordinates are expressed in points (72 dpi) java.awt.Dimension pgsize = ppt.getPageSize(); int pgx = pgsize.width; //slide width in points int pgy = pgsize.height; //slide height in points //set new page size ppt.setPageSize(new java.awt.Dimension(1024, 768)); How to read shapes contained in a particular slideThe following code demonstrates how to iterate over shapes for each slide. XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx")); // get slides for (XSLFSlide slide : ppt.getSlides()) { for (XSLFShape sh : slide.getShapes()) { // name of the shape String name = sh.getShapeName(); // shapes's anchor which defines the position of this shape in the slide if (sh instanceof PlaceableShape) { java.awt.geom.Rectangle2D anchor = ((PlaceableShape)sh).getAnchor(); } if (sh instanceof XSLFConnectorShape) { XSLFConnectorShape line = (XSLFConnectorShape) sh; // work with Line } else if (sh instanceof XSLFTextShape) { XSLFTextShape shape = (XSLFTextShape) sh; // work with a shape that can hold text } else if (sh instanceof XSLFPictureShape) { XSLFPictureShape shape = (XSLFPictureShape) sh; // work with Picture } } } Add Image to SlideXMLSlideShow ppt = new XMLSlideShow(); XSLFSlide slide = ppt.createSlide(); byte[] pictureData = IOUtils.toByteArray(new FileInputStream("image.png")); XSLFPictureData pd = ppt.addPicture(pictureData, PictureData.PictureType.PNG); XSLFPictureShape pic = slide.createPicture(pd); Read Images contained within a presentationXMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx")); for(XSLFPictureData data : ppt.getAllPictures()){ byte[] bytes = data.getData(); String fileName = data.getFileName(); } Basic text formattingXMLSlideShow ppt = new XMLSlideShow(); XSLFSlide slide = ppt.createSlide(); XSLFTextBox shape = slide.createTextBox(); XSLFTextParagraph p = shape.addNewTextParagraph(); XSLFTextRun r1 = p.addNewTextRun(); r1.setText("The"); r1.setFontColor(Color.blue); r1.setFontSize(24.); XSLFTextRun r2 = p.addNewTextRun(); r2.setText(" quick"); r2.setFontColor(Color.red); r2.setBold(true); XSLFTextRun r3 = p.addNewTextRun(); r3.setText(" brown"); r3.setFontSize(12.); r3.setItalic(true); r3.setStrikethrough(true); XSLFTextRun r4 = p.addNewTextRun(); r4.setText(" fox"); r4.setUnderline(true); How to read hyperlinks from a slide showXMLSlideShow ppt = new XMLSlideShow(); XSLFSlide slide = ppt.createSlide(); // assign a hyperlink to a text run XSLFTextBox shape = slide.createTextBox(); XSLFTextRun r = shape.addNewTextParagraph().addNewTextRun(); r.setText("Apache POI"); XSLFHyperlink link = r.createHyperlink(); link.setAddress("http://poi.apache.org"); PPTX2PNG is an application that converts each slide of a .pptx slideshow into a PNG imageUsage: PPTX2PNG [options] <pptx file> Options: -scale <float> scale factor (default is 1.0) -slide <integer> 1-based index of a slide to render. Default is to render all slides. How it works: The XSLFSlide object implements a draw(Graphics2D graphics) method that recursively paints all shapes in the slide into the supplied graphics canvas: slide.draw(graphics); where graphics is a class implementing java.awt.Graphics2D. In PPTX2PNG the graphic canvas is derived from java.awt.image.BufferedImage, i.e. the destination is an image in memory, but in general case you can pass any compliant implementation of java.awt.Graphics2D. The PPTX2SVG example demonstrates how to use Apache Batik to convert .pptx slides into SVG format. Merge multiple presentations togetherXMLSlideShow ppt = new XMLSlideShow(); String[] inputs = {"presentations1.pptx", "presentation2.pptx"}; for(String arg : inputs){ FileInputStream is = new FileInputStream(arg); XMLSlideShow src = new XMLSlideShow(is); is.close(); for(XSLFSlide srcSlide : src.getSlides()){ ppt.createSlide().importContent(srcSlide); } } FileOutputStream out = new FileOutputStream("merged.pptx"); ppt.write(out); out.close(); |