Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Poi Create PowerPoint Slide Using Java

Related Tutorials


 
 

Share on Google+Share on Google+

Create PowerPoint Slide Using Java

Advertisement
In this example we are going to create a PowerPoint slide.

Create PowerPoint Slide Using Java

     


In this example we are going to create a PowerPoint slide .

HWSL is used to make the Microsoft PowerPoint 97(-2003) file format by pure Java. It supports read and write capabilities of some, but not yet all of the core records.
HSSF provides a way to read PowerPoint presentations, and extracts text from it. It also provides some edit capabilities (currently limited). 

The org.apache.poi.hslf.extractor.PowerPointExtractor class is used to extract text from PowerPoint. It accepts a file or an input stream. The getText() method can be used to get the text from the slides, and the getNotes() method is used to get the text from the notes. The getText(true,true) is used to get the text from both.

To create a slide show we need  org.apache.poi.hslf.usermodel.SlideShow. To accepts a file or an input stream we need a org.apache.poi.hslf.HSLFSlideShow. We use getSlides() and getNotes() to get the slides and notes.

To get blocks of text we need  getTextRuns().
The getTextAsString()  and getTextAsVector() are two methods which are used to get the text back. The getTextAsString() returns a single string with all the text in it and getTextAsVector() returns a vector of strings, one for each text record found in the file.
We can change the text via  TextRun.setText(String) or RichTextRun.setText(String). But it is not yet possible to add additional TextRuns or RichTextRuns.

We can add new slides by calling SlideShow.createSlide(), which will add a new slide to the end of the SlideShow. It is not possible to re-order slides, nor to add new text to slides tile now .But currently we can add Escher objects.

The following classes can be  used to create the PowerPoint presentations.

The org.apache.poi.hslf.HSLFSlideShow class handles reading in and writing out files. The org.apache.poi.hslf.record.Record class is used to build a tree of all the records in the file. This allows to access the record. The  org.apache.poi.hslf.record.Record class is base class of all records and also provides the main record generation code, which will build up a tree of records for a file. The org.apache.poi.hslf.usermodel.SlideShow class is builds up model entries from the records, and presents a user facing view of the file. The org.apache.poi.hslf.model.Slide class is used to define a user facing view of a Slide in a SlideShow. The org.apache.poi.hslf.model.Slide class allows us to get at the Text of the slide, and at any drawing objects on it. The org.apache.poi.hslf.model.TextRun class is used to hold all the Text in a given area of the Slide, and will contain one or more RichTextRuns. The org.apache.poi.hslf.usermodel.RichTextRun class is used to hold a run of text, all having the same character and paragraph styling. It is possible to modify text, and/or text styling. The org.apache.poi.hslf.extractor.PowerPointExtractor is used to allow extraction of text from files. The org.apache.poi.hslf.extractor.QuickButCruddyTextExtractor class is used to extract all the text from files very fast, but including deleted text and other bits of Crud.

In this example we are creating a PowerPoint presentation. For this we are creating the object of SlideShow after that we are creating an object of slide and an object of  file output to create a .ppt file .
  
 The code of the program is given below:
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
import java.io.*;
public class createNewPersentation
  {
  public static void main(String str[])
  {
  try{
  SlideShow slideShow = new SlideShow();
  Slide slide = slideShow.createSlide();
  FileOutputStream out = new FileOutputStream("slideshow.ppt");
  slideShow.write(out);
  out.close();
  }catch(Exception e){}}
  }

The output of the program is given below:

Download this example.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 14, 2008

Related Tutorials

Discuss: Create PowerPoint Slide Using Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com