Apache POI PPT Slide Layouts in Apache POI PPT

What are the available slide layouts in Apache POI PPT?

In the last chapter, we explained creating empty slides and adding slides to it. In this chapter, we will explain how to get the list of available slides and how to create a slide with different layouts.

Available Slide layouts

Below is the list of available slide layouts.

  • Different slide masters are available and each slide master has many slide layouts.
  • To get the list of the slide masters, you can use getSlideMasters() method of the XMLSlideShow class.
  • To get the list of slide layouts from each slide master, you can use getSlideLayouts() method of the XSLFSlideMaster class.
  • To get the name of the slide layout from the layout object, you can use getType() method of the XSLFSlideLayout class.

Note: All these classes belong to org.poi.xslf.usermodel package.

Below is the complete program to get the list of available slide layouts in the PPT:

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

It will compile and execute to generate below output:

Below are some of the sample slide layouts available with MS-Office 360, 2013 edition.

sample-slide-layouts

Title Layout

Let’s 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:

Step 2: You can get the list of slide masters using getSlideMasters() method. After that, select the desired slide master using the index as shown below:

Here we are getting the default slide master which is in 0th location of the slide masters array.

Step 3: You can get the desired layout using getLayout() method of XSLFSlideMaster class. This method will accept a parameter where you should pass one of the static variables of the SlideLayoutclass, which represents the desired layout. There are many variables in this class where each variable represents a slide layout.

Code snippet given below shows how to create a title layout:

Step 4: Create a new slide by passing a slide layout object as parameter.

Step 5: Select a placeholder using getPlaceholder() method of the XSLFSlide class. This method will accept an integer parameter and by passing 0 to it, you will get the XSLFTextShape object. By using this, you can access the title text area of the slide. Set the title using the setText() method as shown below:

Given below is the complete program to create a slide with Title layout in a presentation:

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

It will compile and execute to generate below output.

PPT document with newly added Title layout slide appears as shown below:

Title and content Layout

Let’s create a slide in PPT using the Title and content layout. Follow below steps.

Step 1: Create an empty presentation by instantiating the XMLSlideShow class as shown below:

Step 2: you can get the list of slide masters using the getSlideMasters() method. Select the desired slide master using the index as shown below:

Here we are getting the default slide master which is in the 0th location of the slide masters array.

Step 3: You can get the desired layout using getLayout() method of the XSLFSlideMaster class. This method will accept a parameter where you should pass one of the static variables of the SlideLayout class which represents the desired layout. There are many variables in this class which represent slide layouts.

Below code snippet shows how to create title and content layout:

TitleLayOut

Step 4: Create a new slide by passing the slide layout object as parameter.

Step 5: Select a placeholder using getPlaceholder() method of the XSLFSlide class. This method will accept an integer parameter and by passing 1 to it, you will get the XSLFTextShape object. By using this, you can access the content area of the slide. Set the title using the setText() method as shown below:

Step 6: Clear the existing text in the slide using the clearText() method of the XSLFTextShape class.

Step 7: You can add new paragraph using addNewTextParagraph() method. Add a new text run to the paragraph using addNewTextRun() method and to the text run, add text using the setText() method as shown below:

Below is the complete program to create a slide with Title layout in a presentation:

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

It will compile and execute to generate below output:

PPT document with newly added Title layout slide appears as shown below:

TitleAndContentLayout

In the same way, you can create slides with different layouts as well.


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

Apache POI PPT Topics