Apache POI PPT – Images

Apache POI PPT – Images ”; Previous Next In this chapter, you will learn how to add an image to a PPT and how to read an image from it. Adding Image You can add images to a presentation using the createPicture() method of XSLFSlide. This method accepts image in the form of byte array format. Therefore, you have to create a byte array of the image that is to be added to the presentation. Follow the given procedure to add an image to a presentation. Create an empty slideshow using XMLSlideShow as shown below − XMLSlideShow ppt = new XMLSlideShow(); Create an empty presentation in it using createSlide(). XSLFSlide slide = ppt.createSlide(); Read the image file that is to be added and convert it into byte array using IOUtils.toByteArray() of the IOUtils class as shown below − //reading an image File image = new File(“C://POIPPT//boy.jpg”); //converting it into a byte array byte[] picture = IOUtils.toByteArray(new FileInputStream(image)); Add the image to the presentation using addPicture(). This method accepts two variables: byte array format of the image that is to be added and the static variable representing the file format of the image. The usage of the addPicture() method is shown below − XSLFPictureData idx = ppt.addPicture(picture, XSLFPictureData.PICTURE_TYPE_PNG); Embed the image to the slide using createPicture() as shown below − XSLFPictureShape pic = slide.createPicture(idx); Given below is the complete program to add an image to the slide in a presentation − import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.sl.usermodel.PictureData.PictureType; import org.apache.poi.util.IOUtils; import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xslf.usermodel.XSLFPictureData; import org.apache.poi.xslf.usermodel.XSLFPictureShape; import org.apache.poi.xslf.usermodel.XSLFSlide; public class AddingImage { public static void main(String args[]) throws IOException { //creating a presentation XMLSlideShow ppt = new XMLSlideShow(); //creating a slide in it XSLFSlide slide = ppt.createSlide(); //reading an image File image = new File(“C://POIPPT//boy.jpg”); //converting it into a byte array byte[] picture = IOUtils.toByteArray(new FileInputStream(image)); //adding the image to the presentation XSLFPictureData idx = ppt.addPicture(picture, PictureType.PNG); //creating a slide with given picture on it XSLFPictureShape pic = slide.createPicture(idx); //creating a file object File file = new File(“addingimage.pptx”); FileOutputStream out = new FileOutputStream(file); //saving the changes to a file ppt.write(out); System.out.println(“image added successfully”); out.close(); } } Save the above Java code as AddingImage.java, and then compile and execute it from the command prompt as follows − $javac AddingImage.java $java AddingImage It will compile and execute to generate the following output − reordering of the slides is done The presentation with the newly added slide with image appears as follows − Reading Image You can get the data of all the pictures using the getPictureData() method of the XMLSlideShow class. The following program reads the images from a presentation − import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.sl.usermodel.PictureData.PictureType; import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xslf.usermodel.XSLFPictureData; public class Readingimage { public static void main(String args[]) throws IOException { //open an existing presentation File file = new File(“addingimage.pptx”); XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file)); //reading all the pictures in the presentation for(XSLFPictureData data : ppt.getPictureData()){ byte[] bytes = data.getData(); String fileName = data.getFileName(); PictureType pictureFormat = data.getType(); System.out.println(“picture name: ” + fileName); System.out.println(“picture format: ” + pictureFormat); } //saving the changes to a file FileOutputStream out = new FileOutputStream(file); ppt.write(out); out.close(); } } Save the above Java code as Readingimage.java, and then compile and execute it from the command prompt as follows − $javac Readingimage.java $java Readingimage It will compile and execute to generate the following output − picture name: image1.png picture format: 6 Print Page Previous Next Advertisements ”;

Apache POI PPT – Slide Layouts

Apache POI PPT – Slide Layouts ”; Previous Next In the previous chapter, you have seen how to create empty slides and how to add slides to it. In this chapter, you will learn how to get the list of available slides, and how to create a slide with different layouts. Available Slide layouts PowerPoint presentations have slide layouts, and you can choose a desired layout to edit a slide. First of all, let us find out the list of all the slide layouts available. There are different slide masters and in each slide master, there are several slide layouts. You can get the list of the slide masters using the getSlideMasters() method of the XMLSlideShow class. You can get the list of the slide layouts from each slide master using the getSlideLayouts() method of the XSLFSlideMaster class. You can get the name of the slide layout from the layout object using the getType() method of the XSLFSlideLayout class. Note − All these classes belongs to org.poi.xslf.usermodel package. Given below is the complete program to get the list of available slide layouts in the PPT − import java.io.IOException; import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xslf.usermodel.XSLFSlideLayout; import org.apache.poi.xslf.usermodel.XSLFSlideMaster; public class SlideLayouts { public static void main(String args[]) throws IOException { //create an empty presentation XMLSlideShow ppt = new XMLSlideShow(); System.out.println(“Available slide layouts:”); //getting the list of all slide masters for(XSLFSlideMaster master : ppt.getSlideMasters()) { //getting the list of the layouts in each slide master for(XSLFSlideLayout layout : master.getSlideLayouts()) { //getting the list of available slides System.out.println(layout.getType()); } } } } Save the above Java code as SlideLayouts.java , and then compile and execute it from the command prompt as follows − $javac SlideLayouts.java $java SlideLayouts It will compile and execute to generate the following output − Available slide layouts: TITLE PIC_TX VERT_TX TWO_TX_TWO_OBJ BLANK VERT_TITLE_AND_TX TITLE_AND_CONTENT TITLE_ONLY SECTION_HEADER TWO_OBJ OBJ_TX Shown below are some of the sample slide layouts available with MS-Office 360, 2013 edition. Title Layout Let us create a slide in a PPT using Title layout. Follow the steps given below − Step 1 − Create an empty presentation by instantiating the XMLSlideShow class as shown below. XMLSlideShow ppt = new XMLSlideShow(); Step 2 − Get the list of slide masters using the getSlideMasters() method. Thereafter, select the desired slide master using the index as shown below. XSLFSlideMaster slideMaster = ppt.getSlideMasters()[0]; Here we are getting the default slide master which is in the 0th location of the slide masters array. Step 3 − Get the desired layout using the getLayout() method of the XSLFSlideMaster class. This method accepts a parameter where you have to pass one of the static variable of the SlideLayoutclass, which represents our desired layout. There are several variables in this class where each variable represents a slide layout. The code snippet given below shows how to create a title layout − XSLFSlideLayout titleLayout = slideMaster.getLayout(SlideLayout.TITLE); Step 4 − Create a new slide by passing a slide layout object as parameter. XSLFSlide slide = ppt.createSlide(titleLayout); Step 5 − Select a placeholder using the getPlaceholder() method of the XSLFSlide class. This method accepts an integer parameter. By passing 0 to it, you will get the XSLFTextShape object, using which you can access the title text area of the slide. Set the title using the setText() method as shown below. XSLFTextShape title1 = slide.getPlaceholder(0); //setting the title init title1.setText(“Tutorials point”); Given below is the complete program to create a slide with Title layout in a presentation − import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.xslf.usermodel.SlideLayout; import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xslf.usermodel.XSLFSlide; import org.apache.poi.xslf.usermodel.XSLFSlideLayout; import org.apache.poi.xslf.usermodel.XSLFSlideMaster; import org.apache.poi.xslf.usermodel.XSLFTextShape; public class TitleLayout { public static void main(String args[]) throws IOException { //creating presentation XMLSlideShow ppt = new XMLSlideShow(); //getting the slide master object XSLFSlideMaster slideMaster = ppt.getSlideMasters().get(0); //get the desired slide layout XSLFSlideLayout titleLayout = slideMaster.getLayout(SlideLayout.TITLE); //creating a slide with title layout XSLFSlide slide1 = ppt.createSlide(titleLayout); //selecting the place holder in it XSLFTextShape title1 = slide1.getPlaceholder(0); //setting the title init title1.setText(“Tutorials point”); //create a file object File file = new File(“F://Titlelayout.pptx”); FileOutputStream out = new FileOutputStream(file); //save the changes in a PPt document ppt.write(out); System.out.println(“slide cretated successfully”); out.close(); } } Save the above Java code as TitleLayout.java, and then compile and execute it from the command prompt as follows − $javac TitleLayout.java $java TitleLayout It will compile and execute to generate the following output. slide created successfully The PPT document with newly added Title layout slide appears as follows − Title and content Layout Let us create a slide in a PPT using Title and content layout. Follow the steps given below. Step 1 − Create an empty presentation by instantiating the XMLSlideShow class as shown below. XMLSlideShow ppt = new XMLSlideShow(); Step 2 − Get the list of slide masters using the getSlideMasters() method. Select the desired slide master using the index as shown below. XSLFSlideMaster slideMaster = ppt.getSlideMasters()[0]; Here we are getting the default slide master which is in the 0th location of the slide masters array. Step 3 − Get the desired layout using the getLayout() method of the XSLFSlideMaster class. This method accepts a parameter where you have to pass one of the static variable of the SlideLayout class which represents our desired layout. There are several variables in this class that represent slide layouts. The following code snippet shows how to create title and content layout − XSLFSlideLayout contentlayout = slideMaster.getLayout(SlideLayout.TITLE_AND_CONTENT); Step 4 − Create a new slide by passing the slide layout object as parameter. XSLFSlide slide = ppt.createSlide(SlideLayout.TITLE_AND_CONTENT);

Apache POI PPT – Slide Management

Apache POI PPT – Slide Management ”; Previous Next After completing this chapter, you will be able to delete, reorder, and perform read and write operations on a slide. Changing a Slide We can change the page size of a slide using the setPageSize() method of the XMLSlideShow class. Initially create a presentation as shown below − File file = new File(“C://POIPPT//Examples// TitleAndContentLayout.pptx”); //create presentation XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file)); Get the size of the current slide using the getPageSize() method of the XMLSlideShow class. java.awt.Dimension pgsize = ppt.getPageSize(); Set the size of the page using the setPageSize() method. ppt.setPageSize(new java.awt.Dimension(1024, 768)); The complete program for changing the size of a slide is given below − import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.xslf.usermodel.XMLSlideShow; public class ChangingSlide { public static void main(String args[]) throws IOException { //create file object File file = new File(“TitleAndContentLayout.pptx”); //create presentation XMLSlideShow ppt = new XMLSlideShow(); //getting the current page size java.awt.Dimension pgsize = ppt.getPageSize(); int pgw = pgsize.width; //slide width in points int pgh = pgsize.height; //slide height in points System.out.println(“current page size of the PPT is:”); System.out.println(“width :” + pgw); System.out.println(“height :” + pgh); //set new page size ppt.setPageSize(new java.awt.Dimension(2048,1536)); //creating file object FileOutputStream out = new FileOutputStream(file); //saving the changes to a file ppt.write(out); System.out.println(“slide size changed to given dimentions “); out.close(); } } Save the above Java code as ChangingSlide.java, and then compile and execute it from the command prompt as follows − $javac ChangingSlide.java $java ChangingSlide It will compile and execute to generate the following output. current page size of the presentation is : width :720 height :540 slide size changed to given dimensions Given below is the snapshot of the presentation before changing the slide size − The slide appears as follows after changing its size − Reordering Slides You can set the slide order using the setSlideOrder() method. Given below is the procedure to set the order of the slides. Open an existing PPT document as shown below − File file = new File(“C://POIPPT//Examples//example1.pptx”); XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file)); Get the slides using the getSlides() method as shown below − List<XSLFSlide> slides = ppt.getSlides(); Select a slide from the array of the slides, and change the order using the setSlideOrder() method as shown below − //selecting the fourth slide XSLFSlide selectesdslide = slides.get(4); //bringing it to the top ppt.setSlideOrder(selectesdslide, 1); Given below is the complete program to reorder the slides in a presentation − import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xslf.usermodel.XSLFSlide; public class ReorderSlide { public static void main(String args[]) throws IOException { //opening an existing presentation File file = new File(“example1.pptx”); XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file)); //get the slides List<XSLFSlide> slides = ppt.getSlides(); //selecting the fourth slide XSLFSlide selectesdslide = slides.get(13); //bringing it to the top ppt.setSlideOrder(selectesdslide, 0); //creating an file object FileOutputStream out = new FileOutputStream(file); //saving the changes to a file ppt.write(out); out.close(); } } Save the above Java code as ReorderSlide.java, and then compile and execute it from the command prompt as follows − $javac ReorderSlide.java $java ReorderSlide It will compile and execute to generate the following output. Reordering of the slides is done Given below is the snapshot of the presentation before reordering the slides − After reordering the slides, the presentation appears as follows. Here we have selected the slide with image and moved it to the top. Deleting Slides You can delete the slides using the removeSlide() method. Follow the steps given below to delete slides. Open an existing presentation using the XMLSlideShow class as shown below − File file = new File(“C://POIPPT//Examples//image.pptx”); XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file)); Delete the required slide using the removeSlide() method. This method accepts an integer parameter. Pass the index of the slide that is to be deleted to this method. ppt.removeSlide(1); Given below is the program to delete slides from a presentation − import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.xslf.usermodel.XMLSlideShow; public class Deleteslide { public static void main(String args[]) throws IOException { //Opening an existing slide File file = new File(“image.pptx”); XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file)); //deleting a slide ppt.removeSlide(1); //creating a file object FileOutputStream out = new FileOutputStream(file); //Saving the changes to the presentation ppt.write(out); out.close(); } } Save the above Java code as Deleteslide.java, and then compile and execute it from the command prompt as follows − $javac Deleteslide.java $java Deleteslide It will compile and execute to generate the following output − reordering of the slides is done The snapshot below is of the presentation before deleting the slide − After deleting the slide, the presentation appears as follows − Print Page Previous Next Advertisements ”;