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

时间:2013-02-01 18:32:36

标签: apache-poi powerpoint

apache POI项目解释了如何从幻灯片幻灯片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?

由于

1 个答案:

答案 0 :(得分:4)

它实际上非常相似...

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课程。