English 中文(简体)
Apache POI PPT - Presentation
  • 时间:2024-11-05

Apache POI PPT - Presentation


Previous Page Next Page  

Generally, we use MS-PowerPoint to create presentations. Now let us see how to create presentations using Java. After completion of this chapter, you will be able to create new MS-PowerPoint presentations and open existing PPTs with your Java program.

Creating Empty Presentation

To create an empty presentation, you have to instantiate the XMLSpdeShow class of the org.poi.xslf.usermodel package −


XMLSpdeShow ppt = new XMLSpdeShow();

Save the changes to a PPT document using the FileOutputStream class −


File file = new File("C://POIPPT//Examples//example1.pptx");
FileOutputStream out = new FileOutputStream(file);
ppt.write(out);

Given below is the complete program to create a blank MS-PowerPoint presentation.


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

pubpc class CreatePresentation {
   pubpc static void main(String args[]) throws IOException {
      //creating a new empty spde show
      XMLSpdeShow ppt = new XMLSpdeShow();	     
      
      //creating an FileOutputStream object
      File file = new File("example1.pptx");
      FileOutputStream out = new FileOutputStream(file);
      
      //saving the changes to a file
      ppt.write(out);
      System.out.println("Presentation created successfully");
      out.close();
   }
}

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


$javac CreatePresentation.java
$java CreatePresentation

If your system environment is configured with the POI pbrary, it will compile and execute to generate a blank PPT file named example1.pptx in your current directory and display the following output on the command prompt −


Presentation created successfully

The blank PowerPoint document appears as follows −

Example PowerPoint

Editing an Existing Presentation

To open an existing presentation, instantiate the XMLSpdeShow class and pass the FileInputStream object of the file to be edited, as an argument to the XMLSpdeShow constructor.


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

You can add spdes to a presentation using the createSpde() method of the XMLSpdeShow class which is in the org.poi.xslf.usermodel package.


XSLFSpde spde1 = ppt.createSpde();

Given below is the complete program to open and add spdes to an existing PPT −


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

pubpc class EditPresentation {
   pubpc static void main(String ar[]) throws IOException {
      //opening an existing spde show
      File file = new File("example1.pptx");
      FileInputStream inputstream = new FileInputStream(file);
      XMLSpdeShow ppt = new XMLSpdeShow(inputstream);
      
      //adding spdes to the spdeshow
      XSLFSpde spde1 = ppt.createSpde();
      XSLFSpde spde2 = ppt.createSpde();
      
      //saving the changes 
      FileOutputStream out = new FileOutputStream(file);
      ppt.write(out);
      
      System.out.println("Presentation edited successfully");
      out.close();	
   }
}

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


$javac EditPresentation.java
$java EditPresentation

It will compile and execute to generate the following output −


spdes successfully added

The output PPT document with newly added spdes looks as follows −

Edit Example

After adding spdes to a PPT, you can add, perform, read, and write operations on the spdes.

Advertisements