在 .NET 中自定义演示文稿形状

使用编辑点更改形状

考虑一个正方形。在 PowerPoint 中,使用 编辑点,您可以

  • 将正方形的角向内或向外移动
  • 为角或点指定曲率
  • 向正方形添加新点
  • 操作正方形上的点,等等

本质上,您可以对任何形状执行上述任务。通过编辑点,您可以更改形状或从现有形状创建新形状。

形状编辑技巧

overview_image

在通过编辑点开始编辑 PowerPoint 形状之前,您可能想先考虑以下关于形状的要点:

  • 形状(或其路径)可以是闭合的,也可以是打开的。
  • 所有形状至少由 2 个锚点组成,这些锚点通过线相连。
  • 线段可以是直线或曲线。锚点决定线段的性质。
  • 锚点有角点、直线点或平滑点:
    • 角点是两条直线在一个角度相连的点。
    • 平滑点是两个控制柄在同一直线上,且线段以平滑曲线相连的点。在这种情况下,所有控制柄与锚点的距离相等。
    • 直线点是两个控制柄在同一直线上,但线段仍以平滑曲线相连的点。在这种情况下,控制柄与锚点的距离不必相等。
  • 通过移动或编辑锚点(从而改变线段的角度),可以改变形状的外观。

要通过编辑点编辑 PowerPoint 形状,Aspose.Slides 提供了 GeometryPath 类和 IGeometryPath 接口。

简单编辑操作

下面的 C# 代码演示如何

在路径末尾添加直线

void LineTo(PointF point);
void LineTo(float x, float y);

在路径的指定位置添加直线

void LineTo(PointF point, uint index);
void LineTo(float x, float y, uint index);

在路径末尾添加三次贝塞尔曲线

void CubicBezierTo(PointF point1, PointF point2, PointF point3);
void CubicBezierTo(float x1, float y1, float x2, float y2, float x3, float y3);

在路径的指定位置添加三次贝塞尔曲线

void CubicBezierTo(PointF point1, PointF point2, PointF point3, uint index);
void CubicBezierTo(float x1, float y1, float x2, float y2, float x3, float y3, uint index);

在路径末尾添加二次贝塞尔曲线

void QuadraticBezierTo(PointF point1, PointF point2);
void QuadraticBezierTo(float x1, float y1, float x2, float y2);

在路径的指定位置添加二次贝塞尔曲线

void QuadraticBezierTo(PointF point1, PointF point2, uint index);
void QuadraticBezierTo(float x1, float y1, float x2, float y2, uint index);

向路径追加给定弧段

void ArcTo(float width, float heigth, float startAngle, float sweepAngle);

关闭路径的当前图形

void CloseFigure();

设置下一个点的位置

void MoveTo(PointF point);
void MoveTo(float x, float y);

删除给定索引处的路径段

void RemoveAt(int index);

向形状添加自定义点

  1. 创建一个 GeometryShape 类的实例,并将 ShapeType.Rectangle 类型设置为矩形。
  2. 从形状获取一个 GeometryPath 类的实例。
  3. 在路径的两个顶部点之间添加一个新点。
  4. 在路径的两个底部点之间添加一个新点。
  5. 将路径应用到形状。

下面的 C# 代码演示如何向形状添加自定义点:

using (Presentation pres = new Presentation())
{
    GeometryShape shape = pres.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 200, 100) as GeometryShape;
    IGeometryPath geometryPath = shape.GetGeometryPaths()[0];

    geometryPath.LineTo(100, 50, 1);
    geometryPath.LineTo(100, 50, 4);
    shape.SetGeometryPath(geometryPath);
}

example1_image

从形状中移除点

  1. 创建一个 GeometryShape 类的实例,并将 ShapeType.Heart 类型设置为心形。
  2. 从形状获取一个 GeometryPath 类的实例。
  3. 删除路径的段。
  4. 将路径应用到形状。

下面的 C# 代码演示如何从形状中移除点:

using (Presentation pres = new Presentation())
{
	GeometryShape shape = pres.Slides[0].Shapes.AddAutoShape(ShapeType.Heart, 100, 100, 300, 300) as GeometryShape;

	IGeometryPath path = shape.GetGeometryPaths()[0];
	path.RemoveAt(2);
	shape.SetGeometryPath(path);
}

example2_image

创建自定义形状

  1. 计算形状的点。
  2. 创建一个 GeometryPath 类的实例。
  3. 使用这些点填充路径。
  4. 创建一个 GeometryShape 类的实例。
  5. 将路径应用到形状。

下面的 C# 代码演示如何创建自定义形状:

List<PointF> points = new List<PointF>();

float R = 100, r = 50;
int step = 72;

for (int angle = -90; angle < 270; angle += step)
{
    double radians = angle * (Math.PI / 180f);
    double x = R * Math.Cos(radians);
    double y = R * Math.Sin(radians);
    points.Add(new PointF((float)x + R, (float)y + R));

    radians = Math.PI * (angle + step / 2) / 180.0;
    x = r * Math.Cos(radians);
    y = r * Math.Sin(radians);
    points.Add(new PointF((float)x + R, (float)y + R));
}

GeometryPath starPath = new GeometryPath();
starPath.MoveTo(points[0]);

for (int i = 1; i < points.Count; i++)
{
    starPath.LineTo(points[i]);
}

starPath.CloseFigure();

using (Presentation pres = new Presentation())
{
    GeometryShape shape = pres.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, R * 2, R * 2) as GeometryShape;

    shape.SetGeometryPath(starPath);
}

example3_image

创建复合自定义形状

  1. 创建一个 GeometryShape 类的实例。
  2. 创建第一个 GeometryPath 类的实例。
  3. 创建第二个 GeometryPath 类的实例。
  4. 将路径应用到形状。

下面的 C# 代码演示如何创建复合自定义形状:

using (Presentation pres = new Presentation())
{
    GeometryShape shape = pres.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 200, 100) as GeometryShape;

    GeometryPath geometryPath0 = new GeometryPath();
    geometryPath0.MoveTo(0, 0);
    geometryPath0.LineTo(shape.Width, 0);
    geometryPath0.LineTo(shape.Width, shape.Height/3);
    geometryPath0.LineTo(0, shape.Height / 3);
    geometryPath0.CloseFigure();

    GeometryPath geometryPath1 = new GeometryPath();
    geometryPath1.MoveTo(0, shape.Height/3 * 2);
    geometryPath1.LineTo(shape.Width, shape.Height / 3 * 2);
    geometryPath1.LineTo(shape.Width, shape.Height);
    geometryPath1.LineTo(0, shape.Height);
    geometryPath1.CloseFigure();

    shape.SetGeometryPaths(new GeometryPath[] { geometryPath0, geometryPath1});
}

example4_image

创建带曲线角的自定义形状

下面的 C# 代码演示如何创建带曲线角(向内)的自定义形状;

var shapeX = 20f;
var shapeY = 20f;
var shapeWidth = 300f;
var shapeHeight = 200f;

var leftTopSize = 50f;
var rightTopSize = 20f;
var rightBottomSize = 40f;
var leftBottomSize = 10f;

using (var presentation = new Presentation())
{
    var childShape = presentation.Slides[0].Shapes.AddAutoShape(
        ShapeType.Custom, shapeX, shapeY, shapeWidth, shapeHeight);

    var geometryPath = new GeometryPath();

    var point1 = new PointF(leftTopSize, 0);
    var point2 = new PointF(shapeWidth - rightTopSize, 0);
    var point3 = new PointF(shapeWidth, shapeHeight - rightBottomSize);
    var point4 = new PointF(leftBottomSize, shapeHeight);
    var point5 = new PointF(0, leftTopSize);

    geometryPath.MoveTo(point1);
    geometryPath.LineTo(point2);
    geometryPath.ArcTo(rightTopSize, rightTopSize, 180, -90);
    geometryPath.LineTo(point3);
    geometryPath.ArcTo(rightBottomSize, rightBottomSize, -90, -90);
    geometryPath.LineTo(point4);
    geometryPath.ArcTo(leftBottomSize, leftBottomSize, 0, -90);
    geometryPath.LineTo(point5);
    geometryPath.ArcTo(leftTopSize, leftTopSize, 90, -90);

    geometryPath.CloseFigure();

    childShape.SetGeometryPath(geometryPath);

    presentation.Save("output.pptx", SaveFormat.Pptx);
}

判断形状几何是否闭合

闭合形状的定义是其所有边都相连,形成没有间隙的单一边界。此类形状可以是简单的几何形状,也可以是复杂的自定义轮廓。下面的代码示例演示如何检查形状几何是否闭合:

bool IsGeometryClosed(IGeometryShape geometryShape)
{
    bool? isClosed = null;

    foreach (var geometryPath in geometryShape.GetGeometryPaths())
    {
        var dataLength = geometryPath.PathData.Length;
        if (dataLength == 0)
            continue;

        var lastSegment = geometryPath.PathData[dataLength - 1];
        isClosed = lastSegment.PathCommand == PathCommandType.Close;

        if (isClosed == false)
            return false;
    }
    
    return isClosed == true;
}

将 GeometryPath 转换为 GraphicsPath (System.Drawing.Drawing2D)

  1. 创建一个 GeometryShape 类的实例。
  2. 创建一个位于 System.Drawing.Drawing2D 命名空间下的 GraphicsPath 类的实例。
  3. 使用 ShapeUtilGraphicsPath 实例转换为 GeometryPath 实例。
  4. 将路径应用到形状。

下面的 C# 代码实现了上述步骤,演示了 GeometryPathGraphicsPath 的转换过程:

using (Presentation pres = new Presentation())
{
    GeometryShape shape = pres.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 300, 100) as GeometryShape;

    IGeometryPath originalPath = shape.GetGeometryPaths()[0];
    originalPath.FillMode = PathFillModeType.None;

    GraphicsPath gPath = new GraphicsPath();

    gPath.AddString("Text in shape", new FontFamily("Arial"), 1, 40, new PointF(10, 10), StringFormat.GenericDefault);

    IGeometryPath textPath = ShapeUtil.GraphicsPathToGeometryPath(gPath);
    textPath.FillMode = PathFillModeType.Normal;

    shape.SetGeometryPaths(new[] {originalPath, textPath}) ;
}

example5_image

常见问题

替换几何后填充和轮廓会怎样?
样式仍保留在形状上;仅轮廓发生变化。填充和轮廓会自动应用到新几何上。

如何正确旋转自定义形状以及其几何?
使用形状的 rotation 属性;几何会随形状一起旋转,因为它绑定在形状的坐标系上。

我可以将自定义形状转换为图像以“锁定”结果吗?
可以。将所需的 slide 区域或 shape 本身导出为栅格格式;这可简化对复杂几何的后续处理。