Inserting Text on Shape Using Java

In this example we are going to create auto shape and text in PowerPoint slide using java.

Inserting Text on Shape Using Java

In this example we are going to create auto shape and text in PowerPoint slide using java.

Inserting Text on Shape Using Java

Inserting Text on Shape Using Java

     

In this example we are going to create auto shape and text in PowerPoint slide using java.

In this example we are creating the object of AutoShape .We are passing the shape type into AutoShape as argument parameter .We are passing ShapeTypes.Star32 as shape type. Then we are using setAnchor() method to give the position of the shape. To fill the color we are using setFillColr(Color colr)  method. To insert text we are creating an object of RichTextRun constructor. In this we are passing text value. Finally we are adding it by using addShape() method.
  
 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.model.*;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.usermodel.*;
import java.io.*;
import java.awt.*;
import org.apache.poi.hslf.model.TextBox;
class insertingTextInShape
{
  public static void main(String a[])
  {
  try
  SlideShow slideShow = new SlideShow();
  Slide slide = slideShow.createSlide();
  AutoShape sh1 = new AutoShape(ShapeTypes.Star32);
  TextBox txt = new TextBox();
  txt.setText("RAJESH KUMAR");
  txt.setAnchor(new java.awt.Rectangle(23527515050));
  RichTextRun rt = txt.getTextRun().getRichTextRuns()[0];
  rt.setFontColor(Color.blue);
  sh1.setAnchor(new java.awt.Rectangle(00600600));
  sh1.setFillColor(Color.red);
  AutoShape sh2 = new AutoShape(ShapeTypes.Star32);
  sh2.setAnchor(new java.awt.Rectangle(200200200200));
  sh2.setFillColor(Color.green);
  AutoShape sh3 = new AutoShape(ShapeTypes.Star32);
  sh3.setAnchor(new java.awt.Rectangle(150150300300));
  sh3.setFillColor(Color.red);
  AutoShape sh4 = new AutoShape(ShapeTypes.Star32);
  sh4.setAnchor(new java.awt.Rectangle(100100400400));
  sh4.setFillColor(Color.green);
  slide.addShape(sh1);
  slide.addShape(sh4);
  slide.addShape(sh3);
  slide.addShape(sh2);
  slide.addShape(txt);
 FileOutputStream out = new FileOutputStream
(
"insertingNameInShape.ppt");
 slideShow.write(out);
 out.close();
  }catch(Exception e){}
  }}

The output of the program is given below:

Download this example.