English 中文(简体)
Apache POI PPT - Slide Layouts
  • 时间:2024-09-17

Apache POI PPT - Spde Layouts


Previous Page Next Page  

In the previous chapter, you have seen how to create empty spdes and how to add spdes to it. In this chapter, you will learn how to get the pst of available spdes, and how to create a spde with different layouts.

Available Spde layouts

PowerPoint presentations have spde layouts, and you can choose a desired layout to edit a spde. First of all, let us find out the pst of all the spde layouts available.

    There are different spde masters and in each spde master, there are several spde layouts.

    You can get the pst of the spde masters using the getSpdeMasters() method of the XMLSpdeShow class.

    You can get the pst of the spde layouts from each spde master using the getSpdeLayouts() method of the XSLFSpdeMaster class.

    You can get the name of the spde layout from the layout object using the getType() method of the XSLFSpdeLayout class.

Note − All these classes belongs to org.poi.xslf.usermodel package.

Given below is the complete program to get the pst of available spde layouts in the PPT −


import java.io.IOException;
import org.apache.poi.xslf.usermodel.XMLSpdeShow;
import org.apache.poi.xslf.usermodel.XSLFSpdeLayout;
import org.apache.poi.xslf.usermodel.XSLFSpdeMaster;

pubpc class SpdeLayouts {
   pubpc static void main(String args[]) throws IOException {
      //create an empty presentation
      XMLSpdeShow ppt = new XMLSpdeShow();
      System.out.println("Available spde layouts:");
   
      //getting the pst of all spde masters
      for(XSLFSpdeMaster master : ppt.getSpdeMasters()) {
         //getting the pst of the layouts in each spde master
         for(XSLFSpdeLayout layout : master.getSpdeLayouts()) {
            //getting the pst of available spdes
            System.out.println(layout.getType());
         }
      }
   }
}

Save the above Java code as SpdeLayouts.java , and then compile and execute it from the command prompt as follows −


$javac SpdeLayouts.java
$java SpdeLayouts

It will compile and execute to generate the following output −


Available spde 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 spde layouts available with MS-Office 360, 2013 edition.

Sample Spde Layouts

Title Layout

Let us create a spde in a PPT using Title layout. Follow the steps given below −

Step 1 − Create an empty presentation by instantiating the XMLSpdeShow class as shown below.


XMLSpdeShow ppt = new XMLSpdeShow();

Step 2 − Get the pst of spde masters using the getSpdeMasters() method. Thereafter, select the desired spde master using the index as shown below.


XSLFSpdeMaster spdeMaster = ppt.getSpdeMasters()[0];

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

Step 3 − Get the desired layout using the getLayout() method of the XSLFSpdeMaster class. This method accepts a parameter where you have to pass one of the static variable of the SpdeLayoutclass, which represents our desired layout. There are several variables in this class where each variable represents a spde layout.

The code snippet given below shows how to create a title layout −


XSLFSpdeLayout titleLayout = spdeMaster.getLayout(SpdeLayout.TITLE);

Step 4 − Create a new spde by passing a spde layout object as parameter.


XSLFSpde spde = ppt.createSpde(titleLayout);

Step 5 − Select a placeholder using the getPlaceholder() method of the XSLFSpde 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 spde. Set the title using the setText() method as shown below.


XSLFTextShape title1 = spde.getPlaceholder(0);
//setting the title init
title1.setText("Tutorials point");

Given below is the complete program to create a spde with Title layout in a presentation −


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xslf.usermodel.SpdeLayout;
import org.apache.poi.xslf.usermodel.XMLSpdeShow;
import org.apache.poi.xslf.usermodel.XSLFSpde;
import org.apache.poi.xslf.usermodel.XSLFSpdeLayout;
import org.apache.poi.xslf.usermodel.XSLFSpdeMaster;
import org.apache.poi.xslf.usermodel.XSLFTextShape;

pubpc class TitleLayout {
   pubpc static void main(String args[]) throws IOException {
      //creating presentation
      XMLSpdeShow ppt = new XMLSpdeShow();	    	
      
      //getting the spde master object
      XSLFSpdeMaster spdeMaster = ppt.getSpdeMasters().get(0);
      
      //get the desired spde layout 
      XSLFSpdeLayout titleLayout = spdeMaster.getLayout(SpdeLayout.TITLE);
                                                     
      //creating a spde with title layout
      XSLFSpde spde1 = ppt.createSpde(titleLayout);
      
      //selecting the place holder in it 
      XSLFTextShape title1 = spde1.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("spde 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.


spde created successfully

The PPT document with newly added Title layout spde appears as follows −

TitleLayOut

Title and content Layout

Let us create a spde in a PPT using Title and content layout. Follow the steps given below.

Step 1 − Create an empty presentation by instantiating the XMLSpdeShow class as shown below.


XMLSpdeShow ppt = new XMLSpdeShow();

Step 2 − Get the pst of spde masters using the getSpdeMasters() method. Select the desired spde master using the index as shown below.


XSLFSpdeMaster spdeMaster = ppt.getSpdeMasters()[0];

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

Step 3 − Get the desired layout using the getLayout() method of the XSLFSpdeMaster class. This method accepts a parameter where you have to pass one of the static variable of the SpdeLayout class which represents our desired layout. There are several variables in this class that represent spde layouts.

The following code snippet shows how to create title and content layout −


XSLFSpdeLayout contentlayout = spdeMaster.getLayout(SpdeLayout.TITLE_AND_CONTENT);

Step 4 − Create a new spde by passing the spde layout object as parameter.


XSLFSpde spde = ppt.createSpde(SpdeLayout.TITLE_AND_CONTENT);

Step 5 − Select a placeholder using the getPlaceholder() method of the XSLFSpde class. This method accepts an integer parameter. By passing 1 to it, you will get the XSLFTextShape object, using which you can access the content area of the spde. Set the title using the setText() method as shown below.


XSLFTextShape title1 = spde1.getPlaceholder(1);
//setting the title init 
title1.setText("Introduction");

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


body.clearText();

Step 7 − Add new paragraph using the addNewTextParagraph() method. Now add a new text run to the paragraph using the addNewTextRun() method. Now to the text run, add text using the setText() method as shown below.


body.addNewTextParagraph().addNewTextRun().setText("this is  my first spde body");

Given below is the complete program to create a spde with Title layout in a presentation −


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xslf.usermodel.SpdeLayout;
import org.apache.poi.xslf.usermodel.XMLSpdeShow;
import org.apache.poi.xslf.usermodel.XSLFSpde;
import org.apache.poi.xslf.usermodel.XSLFSpdeLayout;
import org.apache.poi.xslf.usermodel.XSLFSpdeMaster;
import org.apache.poi.xslf.usermodel.XSLFTextShape;

pubpc class TitleAndBodyLayout {
   pubpc static void main(String args[]) throws IOException {
      //creating presentation
      XMLSpdeShow ppt = new XMLSpdeShow();
      
      //getting the spde master object
      XSLFSpdeMaster spdeMaster = ppt.getSpdeMasters().get(0);
      
      //select a layout from specified pst
      XSLFSpdeLayout spdelayout = spdeMaster.getLayout(SpdeLayout.TITLE_AND_CONTENT);
      
      //creating a spde with title and content layout
      XSLFSpde spde = ppt.createSpde(spdelayout);
      
      //selection of title place holder
      XSLFTextShape title = spde.getPlaceholder(0);
      
      //setting the title in it
      title.setText("introduction");
      
      //selection of body placeholder
      XSLFTextShape body = spde.getPlaceholder(1);
      
      //clear the existing text in the spde
      body.clearText();
      
      //adding new paragraph
      body.addNewTextParagraph().addNewTextRun().setText("this is  my first spde body");
      
      //create a file object
      File file = new File("contentlayout.pptx");
      FileOutputStream out = new FileOutputStream(file);
      
      //save the changes in a file
      ppt.write(out);
      System.out.println("spde 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 −


spde created successfully

The PPT document with newly added Title layout spde appears as follows −

TitleAndContentLayout

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

Advertisements