使用 XSLF(Apache POI 项目)向 Powerpoint 幻灯片添加形状



apache POI项目解释了如何从PowerPoint幻灯片中读取形状 http://poi.apache.org/slideshow/xslf-cookbook.html#GetShapes

但是,我找不到任何有关如何使用库的这一部分向PowerPoint幻灯片添加形状的文档。 如果我使用旧的PowerPoint格式(ppt而不是pptx),我可以只使用libaray的hslf部分并执行以下操作:

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();
}

我将如何使用库的 xlsf 部分做类似的事情,从而生成一个 pptx?

谢谢

它实际上非常相似...

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();
}

有关更多示例和 Graphics2D 上下文,您可以在其上绘制,请查看我的 PptxGraphics2D 类。

最新更新