3

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?

谢谢

4

1 回答 1

5

其实挺像的。。。

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类。

于 2013-04-28T09:32:55.780 回答