Adding shapes to Powerpoint slide using XSLF (Apache POI Project)

你说的曾经没有我的故事 提交于 2019-12-10 09:48:55

问题


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:


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.



来源:https://stackoverflow.com/questions/14652821/adding-shapes-to-powerpoint-slide-using-xslf-apache-poi-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!