In Java, using createPicture() method from XSLFSlide, images can be added to the PPT. Now the computer doesn’t understand, what’s there in the picture, so internally, this method accepts the image in a byte array format —> (a series of non-understandable random numbers, letters, symbols, etc.) something like this, AGETRDYDC5545#$^NHVGCFFSFSFGSDF#@@?.0976*).
The syntax for an empty slideshow
XMLSlideShow ppt = new XMLSlideShow();
The syntax for creating slide
XSLFSlide slide = ppt.createSlide();
The next task is to read the image file, which we wish to insert, and then convert it into a byte array using IOUtils.toByteArray() —> IOUtils class.
Add image into the presentation, using addPicture():
- Byte array format of the picture which is to be added(written in the code as byte photo)
- A static variable, which represents the file format of the image.
int idx = ppt.addPicture(photo, XSLFPictureData.PICTURE_TYPE_PNG);
Insert the image to a slide, using createPicture().
XSLFPictureShape pic = slide.createPicture(idx);
Commands for Compilation and Execution:
$javac InsertingImage.java $java InsertingImage
Reordering of the slides is done.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.