在 Apache Poi Powerpoint 形状中设置线连接样式

Setting line join style in Apache Poi Powerpoint shapes

我在 Apache Poi 中有一条多段线 XSLFFreeformShape

默认情况下,角连接样式显示为 圆形

如何将其设置为 BevelMiter

我没有在超类 XSLFSimpleShape 支持的笔划样式方法中找到连接样式。

编辑

这是我对其他行属性所做的:

private void applyLineState( @Nonnull XSLFFreeformShape shape) {
    shape.setLineWidth( ...);
    shape.setLineDash( ... );
    shape.setLineColor( ... );
    shape.setLineHeadDecoration(DecorationShape.NONE);
    shape.setLineCap(LineCap.FLAT);

    // Q: How to set join style for corners?
}

似乎 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.2ooxml-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 直接使用。