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

Apache POI PPT - Spde Management


Previous Page Next Page  

After completing this chapter, you will be able to delete, reorder, and perform read and write operations on a spde.

Changing a Spde

We can change the page size of a spde using the setPageSize() method of the XMLSpdeShow class.

Initially create a presentation as shown below −


File file = new File("C://POIPPT//Examples// TitleAndContentLayout.pptx");

//create presentation
XMLSpdeShow ppt = new XMLSpdeShow(new FileInputStream(file));

Get the size of the current spde using the getPageSize() method of the XMLSpdeShow 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 spde is given below −


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xslf.usermodel.XMLSpdeShow;

pubpc class ChangingSpde {
   pubpc static void main(String args[]) throws IOException {
      //create file object
      File file = new File("TitleAndContentLayout.pptx");
	 
      //create presentation
      XMLSpdeShow ppt = new XMLSpdeShow();
      
      //getting the current page size
      java.awt.Dimension pgsize = ppt.getPageSize();
      int pgw = pgsize.width; //spde width in points
      int pgh = pgsize.height; //spde 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("spde size changed to given dimentions ");
      out.close();	
   }
}

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


$javac ChangingSpde.java
$java ChangingSpde

It will compile and execute to generate the following output.


current page size of the presentation is : 
width :720
height :540
spde size changed to given dimensions

Given below is the snapshot of the presentation before changing the spde size −

TitleAndContentLayout

The spde appears as follows after changing its size −

SpdeAfterChange

Reordering Spdes

You can set the spde order using the setSpdeOrder() method. Given below is the procedure to set the order of the spdes.

Open an existing PPT document as shown below −


File file = new File("C://POIPPT//Examples//example1.pptx");
XMLSpdeShow ppt = new XMLSpdeShow(new FileInputStream(file));

Get the spdes using the getSpdes() method as shown below −


List<XSLFSpde> spdes = ppt.getSpdes();

Select a spde from the array of the spdes, and change the order using the setSpdeOrder() method as shown below −


//selecting the fourth spde
XSLFSpde selectesdspde = spdes.get(4);

//bringing it to the top
ppt.setSpdeOrder(selectesdspde, 1);

Given below is the complete program to reorder the spdes 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.XMLSpdeShow;
import org.apache.poi.xslf.usermodel.XSLFSpde;

pubpc class ReorderSpde {	
   pubpc static void main(String args[]) throws IOException {
      //opening an existing presentation
      File file = new File("example1.pptx");
      XMLSpdeShow ppt = new XMLSpdeShow(new FileInputStream(file));
      
      //get the spdes 
      List<XSLFSpde> spdes = ppt.getSpdes();    
      
      //selecting the fourth spde
      XSLFSpde selectesdspde = spdes.get(13);
      
      //bringing it to the top
      ppt.setSpdeOrder(selectesdspde, 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 ReorderSpde.java, and then compile and execute it from the command prompt as follows −


$javac ReorderSpde.java
$java ReorderSpde

It will compile and execute to generate the following output.


Reordering of the spdes is done

Given below is the snapshot of the presentation before reordering the spdes −

Before Reorder

After reordering the spdes, the presentation appears as follows. Here we have selected the spde with image and moved it to the top.

After Reorder

Deleting Spdes

You can delete the spdes using the removeSpde() method. Follow the steps given below to delete spdes.

Open an existing presentation using the XMLSpdeShow class as shown below −


File file = new File("C://POIPPT//Examples//image.pptx");
XMLSpdeShow ppt = new XMLSpdeShow(new FileInputStream(file));

Delete the required spde using the removeSpde() method. This method accepts an integer parameter. Pass the index of the spde that is to be deleted to this method.


ppt.removeSpde(1);

Given below is the program to delete spdes 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.XMLSpdeShow;

pubpc class Deletespde { 
   pubpc static void main(String args[]) throws IOException {
      //Opening an existing spde
      File file = new File("image.pptx");
      XMLSpdeShow ppt = new XMLSpdeShow(new FileInputStream(file));
      
      //deleting a spde
      ppt.removeSpde(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 Deletespde.java, and then compile and execute it from the command prompt as follows −


$javac Deletespde.java
$java Deletespde

It will compile and execute to generate the following output −


reordering of the spdes is done

The snapshot below is of the presentation before deleting the spde −

After Reorder

After deleting the spde, the presentation appears as follows −

Delete Spdes Advertisements