似乎apache poi 直到现在才提供设置任何线连接属性。但是可以查看XSLFSimpleShape 的源代码,了解如何设置其他CTLineProperties。然后使用低级org.openxmlformats.schemas.drawingml.x2006.main.* 类编写所需的方法。
下面的完整示例显示了这一点:
import java.io.FileOutputStream;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.sl.usermodel.*;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.main.*;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
import java.awt.Rectangle;
import java.awt.geom.Path2D;
public class CreatePPTXFreeformShapeFromPath {
private static CTLineProperties getLn(XSLFShape shape) {
XmlObject o = shape.getXmlObject();
if (!(o instanceof CTShape)) return null;
CTShape sp = (CTShape)o;
CTShapeProperties spr = sp.getSpPr();
if (spr == null) return null;
return (spr.isSetLn()) ? spr.getLn() : spr.addNewLn();
}
private enum LineJoinProperty {
ROUND, MITER, BEVEL
}
public static void setLineJoinProperty(XSLFSimpleShape shape, LineJoinProperty property) {
CTLineProperties ln = getLn(shape);
if (ln == null) return;
if (ln.isSetBevel()) ln.unsetBevel();
if (ln.isSetMiter()) ln.unsetMiter();
if (ln.isSetRound()) ln.unsetRound();
if (property == LineJoinProperty.BEVEL) {
CTLineJoinBevel bevel = ln.addNewBevel();
} else if (property == LineJoinProperty.MITER) {
CTLineJoinMiterProperties miter = ln.addNewMiter();
} else if (property == LineJoinProperty.ROUND) {
CTLineJoinRound round = ln.addNewRound();
}
}
public static void main(String[] args) throws Exception {
XMLSlideShow slideShow = new XMLSlideShow();
XSLFSlide slide = slideShow.createSlide();
Path2D.Double path2D = new Path2D.Double();
path2D.moveTo(0d,0d);
path2D.lineTo(20d,0d);
path2D.lineTo(20d,20d);
path2D.lineTo(40d,20d);
path2D.lineTo(40d,40d);
path2D.lineTo(60d,40d);
path2D.lineTo(60d,60d);
path2D.lineTo(80d,60d);
path2D.lineTo(80d,80d);
path2D.lineTo(100d,80d);
path2D.lineTo(100d,100d);
path2D.closePath();
XSLFFreeformShape shape;
//default
shape = slide.createFreeform();
shape.setPath(path2D);
shape.setLineWidth(5.0);
shape.setLineColor(java.awt.Color.BLACK);
shape.setAnchor(new Rectangle(50, 100, 300, 300));
//miter
shape = slide.createFreeform();
shape.setPath(path2D);
shape.setLineWidth(5.0);
shape.setLineColor(java.awt.Color.BLACK);
shape.setAnchor(new Rectangle(200, 100, 300, 300));
setLineJoinProperty(shape, LineJoinProperty.MITER);
//bevel
shape = slide.createFreeform();
shape.setPath(path2D);
shape.setLineWidth(5.0);
shape.setLineColor(java.awt.Color.BLACK);
shape.setAnchor(new Rectangle(350, 100, 300, 300));
setLineJoinProperty(shape, LineJoinProperty.BEVEL);
FileOutputStream out = new FileOutputStream("CreatePPTXFreeformShapeFromPath.pptx");
slideShow.write(out);
out.close();
}
}
这是使用apache poi 4.1.2 和ooxml-schemas-1.4.jar 测试的。
注意,这个例子需要ooxml-schemas-1.4.jar,因为poi-ooxml-schemas-4.1.2.jar只包含org.openxmlformats.schemas.drawingml.x2006.main.*类,这些类是apache poi 4.1.2直接使用的。