Tutorials Navigation

Tutorials :: New :: Popular :: Top Rated

Tutorials: 18,326 Categories: 12

Total Tutorial Views: 41,302,282

Apache POI PPT Tutorial - Presentation

Tutorial Name: Apache POI PPT Tutorial - Presentation  

Category: PC Tutorials

Submitted By: hoot

Date Added:

Comments: 0

Views: 396

Related Forum: PC Building Forum

Share:

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 XMLSlideShow class of the org.poi.xslf.usermodel package

XMLSlideShow ppt = new XMLSlideShow();


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.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;

public class CreatePresentation {
   
   public static void main(String args[]) throws IOException {
   
      //creating a new empty slide show
      XMLSlideShow ppt = new XMLSlideShow();        
     
      //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 library, 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

[ Register or Signin to view external links. ]

Editing an Existing Presentation

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

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


You can add slides to a presentation using the createSlide() method of the XMLSlideShow class which is in the org.poi.xslf.usermodel package.

XSLFSlide slide1 = ppt.createSlide();


Given below is the complete program to open and add slides 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.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;

public class EditPresentation {

   public static void main(String ar[]) throws IOException {
      
      //opening an existing slide show
      File file = new File("example1.pptx");
      FileInputStream inputstream = new FileInputStream(file);
      XMLSlideShow ppt = new XMLSlideShow(inputstream);
     
      //adding slides to the slodeshow
      XSLFSlide slide1 = ppt.createSlide();
      XSLFSlide slide2 = ppt.createSlide();
     
      //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

slides successfully added


The output PPT document with newly added slides looks as follows

[ Register or Signin to view external links. ]

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

Ratings

Please take one second and rate this tutorial...

Not a Chance
1
2
3
4
5
6
7
8
9
10
Absolutely

Comments

"Apache POI PPT Tutorial - Presentation" :: Login/Create an Account :: 0 comments

If you would like to post a comment please signin to your account or register for an account.