3
votes

The apache POI project explains how to read a shape from a powerpoint slide http://poi.apache.org/slideshow/xslf-cookbook.html#GetShapes

However, I can't find any doc on how to add a shape to a powerpoint slide using this part of the library. If I use an old powerpoint format (ppt as opposed to pptx), I can just use the hslf part of the libaray and do:

SlideShow ppt = new SlideShow();
//add first slide
Slide s1 = ppt.createSlide();

// create shapes./ 
java.awt.geom.GeneralPath path = new java.awt.geom.GeneralPath();
path.moveTo(100, 100);
path.lineTo(200, 100);
path.curveTo(50, 45, 134, 22, 78, 133);
path.curveTo(10, 45, 134, 56, 78, 100);
path.lineTo(100, 200);
path.closePath();

Freeform shape = new Freeform();
shape.setPath(path);
s1.addShape(shape);

//save changes in a file
FileOutputStream out;
try {
    out = new FileOutputStream("slideshow.ppt");
    ppt.write(out);
    out.close(); 
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException ex) {
    e.printStakTrace();
}

How would I do something similar using xlsf part of the library and thus generate a pptx?

Thanks

1

1 Answers

5
votes

It's actually quite similar ...

XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide s1 = ppt.createSlide();

// create shapes 
java.awt.geom.Path2D.Double path = new java.awt.geom.Path2D.Double();
path.moveTo(100, 100);
path.lineTo(200, 100);
path.curveTo(50, 45, 134, 22, 78, 133);
path.curveTo(10, 45, 134, 56, 78, 100);
path.lineTo(100, 200);
path.closePath();

XSLFFreeformShape shape = s1.createFreeform();
shape.setPath(path);
shape.setLineWidth(1);
shape.setLineColor(Color.BLACK);

//save changes in a file
FileOutputStream out;
try {
    out = new FileOutputStream("slideshow.pptx");
    ppt.write(out);
    out.close(); 
} catch (Exception ex) {
    ex.printStackTrace();
}

For more examples and Graphics2D context you can paint on, have a look on my PptxGraphics2D class.