如何在 .NET 中使用连接线连接形状
在 Aspose.Slides FOSS 中,连接线是附着在其他形状的 连接点 上的线形状。当您移动已连接的形状时,连接线的端点会随之移动。最常见的连接线类型是 BentConnector3,它会在障碍物周围以单个拐角弯折的方式进行路由。
先决条件
dotnet add package Aspose.Slides.Foss连接站点索引
每个形状都有四个编号的连接点:
| 索引 | 位置 |
|---|---|
0 | 顶部居中 |
1 | 左侧居中 |
2 | 底部居中 |
3 | 右侧居中 |
连接两个形状
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
// Add two rectangles
var box1 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 200, 200, 100);
var box2 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 450, 200, 200, 100);
box1.AddTextFrame("Start");
box2.AddTextFrame("End");
// Add a bent connector (initial bounds are overwritten by the connection)
var conn = slide.Shapes.AddConnector(ShapeType.BentConnector3, 0, 0, 10, 10);
// Connect right side of box1 (site 3) to left side of box2 (site 1)
conn.StartShapeConnectedTo = box1;
conn.StartShapeConnectionSiteIndex = 3;
conn.EndShapeConnectedTo = box2;
conn.EndShapeConnectionSiteIndex = 1;
prs.Save("connected.pptx", SaveFormat.Pptx);一旦设置了连接端点,传递给 AddConnector 的占位符边界 (0, 0, 10, 10) 将被忽略;PowerPoint 会将连接线重新路由到附加的形状。
连接器类型
// Straight line
ShapeType.StraightConnector1
// Single elbow (L-shape)
ShapeType.BentConnector2
// Double elbow (Z-shape): most common
ShapeType.BentConnector3
// Curved connector
ShapeType.CurvedConnector3为连接线设置样式
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Drawing;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
var box1 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 150, 180, 80);
var box2 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 500, 300, 180, 80);
var conn = slide.Shapes.AddConnector(ShapeType.BentConnector3, 0, 0, 10, 10);
conn.StartShapeConnectedTo = box1;
conn.StartShapeConnectionSiteIndex = 2; // bottom of box1
conn.EndShapeConnectedTo = box2;
conn.EndShapeConnectionSiteIndex = 0; // top of box2
// Style: dashed blue line, 2 pt width
var lf = conn.LineFormat;
lf.Width = 2.0;
lf.FillFormat.SolidFillColor.Color = Color.Blue;
lf.DashStyle = LineDashStyle.Dash;
prs.Save("styled-connector.pptx", SaveFormat.Pptx);带有多个连接器的流程图
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
// Three-step flowchart
var step1 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 350, 50, 200, 70);
var step2 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 350, 220, 200, 70);
var step3 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 350, 390, 200, 70);
step1.AddTextFrame("Step 1");
step2.AddTextFrame("Step 2");
step3.AddTextFrame("Step 3");
void ConnectVertical(IShapeCollection shapes, IShape topShape, IShape bottomShape)
{
var c = shapes.AddConnector(ShapeType.BentConnector3, 0, 0, 10, 10);
c.StartShapeConnectedTo = topShape;
c.StartShapeConnectionSiteIndex = 2; // bottom
c.EndShapeConnectedTo = bottomShape;
c.EndShapeConnectionSiteIndex = 0; // top
}
ConnectVertical(slide.Shapes, step1, step2);
ConnectVertical(slide.Shapes, step2, step3);
prs.Save("flowchart.pptx", SaveFormat.Pptx);读取连接器属性
using Aspose.Slides.Foss;
using var prs = new Presentation("connected.pptx");
foreach (var shape in prs.Slides[0].Shapes)
{
if (shape is Connector connector)
{
var start = connector.StartShapeConnectedTo;
var end = connector.EndShapeConnectedTo;
Console.WriteLine($"Connector: {start?.Name ?? "?"} -> {end?.Name ?? "?"}");
}
}