Working on Rich Text Format on Slide Using Java

In this example we are going to create a slide then we will learn how we can work on rich text using java.

Working on Rich Text Format on Slide Using Java

Working on Rich Text Format on Slide Using Java

     


In this example we are going to create a slide then we will learn how we can work on rich text using java.

In this example we are creating an object of  SlideShow after that we are using createSlide() method to create an object of  Slide. To make a textbox we need first create an object of  TextBox .Then by use of setText(String str) we can pass the string value in textbox.To get blocks of text we need  getTextRuns() 
The getTextAsString()  and getTextAsVector() are two method which is 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.
Changing Tex. 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
Methods used in this example::

setText(java.lang.String text):
 This method is used to change the text or to set the text.

setBold(boolean bold):
This method is used set text is bold or not.

setItalic(boolean italic):
This method is used to set italic type .

setUnderlined(boolean underlined):
This method is used to set underline.

setFontSize(int fontSize):
This method is used to set the font size.

setFontColor(int bgr):
This method is used to set the background color of the text.

setFontColor(java.awt.Color color):
This method is used to set the color of the text, as a java.awt.Color 

setAlignment(int align):
This method is used to set the type of horizontal alignment for the text.

 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 org.apache.poi.hslf.usermodel.RichTextRun;
import java.io.*;
import java.awt.*;
import org.apache.poi.hslf.model.TextBox;
class workingWithTextFormat
{
  public static void main(String a[])
  {
  try
  SlideShow slideShow = new SlideShow();
 Slide slide = slideShow.createSlide();
  
 TextBox txt = new TextBox();
  txt.setText("Rajesh Kumar!");
  txt.setAnchor(new java.awt.Rectangle
(
30010020050));
  RichTextRun richTextRun = txt.getTextRun().
getRichTextRuns
()[0];
  richTextRun.setFontSize(45);
  richTextRun.setFontName("Arial");
  richTextRun.setBold(true);
  richTextRun.setItalic(true);
  richTextRun.setUnderlined(true);
  richTextRun.setFontColor(Color.red);
  richTextRun.setAlignment(TextBox.AlignRight);
  slide.addShape(txt);
  FileOutputStream out = new FileOutputStream
(
"richTextRun.ppt");
  slideShow.write(out);
  out.close();
  }catch(Exception e){}
  }}

The output of the program is given below:

Download this example.