Introduction
Very important and useful feature of MS PowerPoint to draw diagrams and schemes is connectors. Aspose.Slides also supports creating Connectors on a slide that make it possible for developers to create presentations with diagrams easily. This topic will help developers to follow the simple steps with examples for creating simple diagram.
Connector types and connection points
There are 3 types of connectors:
We will create all of them in our example below.
Almost each shape in ppt document has special connection point where connectors can start or finish. Number and position of such point can differ. For example Rectangle has 4 connection points:
|
Figure: Connection Points
|
Creating Connectors on a Slide
To add a Connector on a slide using Aspose.Slides, please follow the steps below:
- Add shapes you need to connect or just obtain the references to these shapes if they already exist
- Add the Connector into the slide
In the example given below, we have added 4 rectangles and connected them with straight, elbow and curve connectors.
Example:
At first let's define functions for creating rectangles and connectors:
[C#]
static Rectangle CreateRectangle(Slide slide, int x, int y, int w, int h, string text)
{
// Create new Rectangle shape on a slide
Rectangle rectangle = slide.Shapes.AddRectangle(x, y, w, h);
// Set format of lines for the rectangle
rectangle.LineFormat.Width = 5;
rectangle.LineFormat.ForeColor = Color.Red;
// Add centered text
rectangle.AddTextFrame(text);
TextFrame tf = rectangle.TextFrame;
tf.Paragraphs[0].Alignment = TextAlignment.Center;
tf.Paragraphs[0].Portions[0].FontBold = true;
tf.Paragraphs[0].Portions[0].FontHeight = 36;
// Return created shape
return rectangle;
}
static Connector CreateConnector(Slide slide, ConnectorType type,
Shape shape1, int connPoint1,
Shape shape2, int connPoint2)
{
// Add new connector with some random default coordinates
Connector connector = slide.Shapes.AddConnector(
type, new Point(500, 500), new Point(1000, 1000));
// Connect connector with 2 shapes
connector.ConnectBegin(shape1, connPoint1);
connector.ConnectEnd(shape2, connPoint2);
// Set connector style
connector.LineFormat.ForeColor = Color.Blue;
connector.LineFormat.Width = 5;
connector.LineFormat.EndArrowheadStyle = LineArrowheadStyle.Open;
// Return created connector
return connector;
}
[VB.NET]
Shared Function CreateRectangle(ByVal slide As Slide, ByVal x As Integer, ByVal y As Integer, _
ByVal w As Integer, ByVal h As Integer, ByVal text As String) As Rectangle
' Create new Rectangle shape on a slide
Dim rectangle As Rectangle = slide.Shapes.AddRectangle(x,y,w,h)
' Set format of lines for the rectangle
rectangle.LineFormat.Width = 5
rectangle.LineFormat.ForeColor = Color.Red
' Add centered text
rectangle.AddTextFrame(text)
Dim tf As TextFrame = rectangle.TextFrame
tf.Paragraphs(0).Alignment = TextAlignment.Center
tf.Paragraphs(0).Portions(0).FontBold = True
tf.Paragraphs(0).Portions(0).FontHeight = 36
' Return created shape
Return rectangle
End Function
Shared Function CreateConnector(ByVal slide As Slide, ByVal type As ConnectorType, _
ByVal shape1 As Shape, ByVal connPoint1 As Integer, _
ByVal shape2 As Shape, ByVal connPoint2 As Integer) As Connector
' Add new connector with some random default coordinates
Connector connector = slide.Shapes.AddConnector(
type, New Point(500, 500), New Point(1000, 1000))
' Connect connector with 2 shapes
connector.ConnectBegin(shape1, connPoint1)
connector.ConnectEnd(shape2, connPoint2)
' Set connector style
connector.LineFormat.ForeColor = Color.Blue
connector.LineFormat.Width = 5
connector.LineFormat.EndArrowheadStyle = LineArrowheadStyle.Open
' Return created connector
Return connector
End Function
Now we can create shapes and connect it:
[C#]
//Instantiate a Presentation object with new empty PPT file
Presentation pres=new Presentation();
//Accessing a slide using its slide position
Slide slide = pres.GetSlideByPosition(1);
//Creating 4 rectangles
Rectangle root = CreateRectangle(slide, 500, 500, 2760, 500, "Connectors");
Rectangle straight = CreateRectangle(slide, 200, 3500, 2000, 400, "Straight");
Rectangle elbow = CreateRectangle(slide, 3500, 1500, 2000, 400, "Elbow");
Rectangle curve = CreateRectangle(slide, 3000, 2500, 2000, 400, "Curve");
[VB.NET]
'Instantiate a Presentation object with new empty PPT file
Dim pres As Presentation = New Presentation()
'Accessing a slide using its slide position
Dim slide As Slide = pres.GetSlideByPosition(1)
'Creating 4 rectangles
Dim root As Rectangle = CreateRectangle(slide,500,500,2760,500,"Connectors")
Dim straight As Rectangle = CreateRectangle(slide,200,3500,2000,400,"Straight")
Dim elbow As Rectangle = CreateRectangle(slide,3500,1500,2000,400,"Elbow")
Dim curve As Rectangle = CreateRectangle(slide,3000,2500,2000,400,"Curve")
After this step we have slide with rectangles:
The last step is connecting our shapes and creating real diagram:
[C#]
//Create straight connector
CreateConnector(slide, ConnectorType.Straight, root, 2, straight, 0);
//Create elbow connector
CreateConnector(slide, ConnectorType.Elbow, root, 3, elbow, 0);
//Create curve connector
CreateConnector(slide, ConnectorType.Curve, root, 2, curve, 1);
[VB.NET]
'Create straight connector
CreateConnector(slide, ConnectorType.Straight, root, 2, straight, 0)
'Create elbow connector
CreateConnector(slide, ConnectorType.Elbow, root, 3, elbow, 0)
'Create curve connector
CreateConnector(slide, ConnectorType.Curve, root, 2, curve, 1)
Our diagram should look like this: