Apache POI PPT Images Apache POI PPT

How to Add/Read images in Apache POI PPT?

This chapter will give you detailed description on how to add an image to a PPT and how to read an image from it.

Adding Image

Images can be added to a presentation using createPicture() method of XSLFSlide. This method will accept images in byte array format. Therefore, you have to create a byte array of the image which has to be added to the presentation.

Follow the given procedure for adding an image to a presentation and create an empty slideshow using XMLSlideShow as shown below:

Create an empty presentation in it using createSlide().

Read the image file which has to be added and convert it into byte array using IOUtils.toByteArray() of the IOUtils class as follows:

Add the image to the presentation using addPicture(). This method will accept two variables: byte array format of the image which has to be added and the static variable which represents the file format of the image. Usage of the addPicture() method is shown below:

Embed the image to the slide using createPicture() as shown below:

Below is the complete program to add an image to the slide in a presentation:

Save the above Java code as AddingImage.java and then compile and execute it from the command prompt as follows:

It will compile and execute to generate below output:

Presentation with the newly added slide with image appears as follows:

AddingImage

Reading Image

You can get the data of all the pictures using getAllPictures() method of the XMLSlideShow class. Below program will read the images from a presentation:

Save the above Java code as Readingimage.java and then compile and execute it from the command prompt as shown below:

It will compile and execute to generate below output:


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Apache POI PPT Topics