officefileapi-405425-presentation-api-slides-shapes.md
The Presentation API library supports the following operations that involve shapes:
Slide masters, layouts, and slides store shapes in the Shapes collection. This collection can contain Shape (presets and custom), PictureShape, GroupShape, and ConnectorShape objects.
Shape shape = (Shape)slide.Shapes[0];
ConnectorShape connector = (ConnectorShape)slide.Shapes[2];
PictureShape picture = (PictureShape)slide.Shapes[3];
Dim shape = CType(slide.Shapes(0), Shape)
Dim connector = CType(slide.Shapes(2), ConnectorShape)
Dim picture = CType(slide.Shapes(3), PictureShape)
To add a shape to a slide, add a Shape object to a slide’s Shapes collection. Pass the shape type as a constructor parameter. The ShapeType enumeration lists available shape presets.
Use the following properties to customize the shape:
X | YSpecify the top-left corner position of the shape bounding box, in Document units (1/300 inch). The original point (0, 0) is the slide’s top-left corner.Width | HeightSpecify the shape bounding box width and height, in Document units (1/300 inch).OutlineSpecifies shape outline parameters.FillSpecifies shape fill parameters.
The following code snippet adds a star shape to a slide:
Shape shape = new Shape(ShapeType.Star12);
shape.Outline = new LineStyle { Fill = new SolidFill(Color.DarkRed), Width = 4 };
shape.Fill = new SolidFill(Color.Coral);
shape.X = 30;
shape.Y = 30;
shape.Width = 800;
shape.Height = 800;
slide.Shapes.Add(shape);
Dim shape As Shape = New Shape(ShapeType.Star12)
shape.Outline = New LineStyle With {
.Fill = New SolidFill(Color.DarkRed),
.Width = 4
}
shape.Fill = New SolidFill(Color.Coral)
shape.X = 30
shape.Y = 30
shape.Width = 800
shape.Height = 800
slide.Shapes.Add(shape)
To add a picture to a slide, add a PictureShape object to a slide’s Shapes collection.
Specify the PictureShape.Image property to set the source image.
To specify picture outline, position, and size settings, use the properties described in Add a Shape.
PictureShape picture = new PictureShape();
string imagePath = "D:\\AirbusA318.png";
Stream stream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
picture.Image = DXImage.FromStream(stream);
picture.Width = 700;
picture.Height = 500;
picture.Outline = new LineStyle { Fill = new SolidFill(Color.DarkGreen), Width = 4 };
slide.Shapes.Add(picture);
Dim picture As PictureShape = New PictureShape()
Dim imagePath = "D:\AirbusA318.png"
Dim stream As Stream = New FileStream(imagePath, FileMode.Open, FileAccess.Read)
picture.Image = DXImage.FromStream(stream)
picture.Width = 700
picture.Height = 500
picture.Outline = New LineStyle With {
.Fill = New SolidFill(Color.DarkGreen),
.Width = 4
}
slide.Shapes.Add(picture)
You can group shapes to apply transformations and visual effects to all shapes in the group simultaneously.
To create a shape group, initialize a GroupShape instance and add shapes to its Shapes collection.
If you want a shape to obtain its fill from the group shape, assign a GroupFill instance to the shape’s Fill property.
Shape shape1 = new Shape(ShapeType.Star12);
shape1.Outline = new LineStyle { Fill = new SolidFill(Color.DarkGreen), Width = 4 };
shape1.Fill = new SolidFill(Color.Yellow);
shape1.X = 200;
shape1.Y = 200;
shape1.Width = 800;
shape1.Height = 800;
Shape shape2 = new Shape(ShapeType.Star10);
shape2.Outline = new LineStyle { Fill = new SolidFill(Color.Black), Width = 4 };
shape2.Fill = new GroupFill();
shape2.X = 900;
shape2.Y = 900;
shape2.Width = 800;
shape2.Height = 800;
GroupShape group = new GroupShape();
group.Fill = new SolidFill(Color.DarkMagenta);
group.X = 200;
group.Y = 200;
group.Width = 1800;
group.Height = 1800;
group.Shapes.Add(shape1);
group.Shapes.Add(shape2);
slide.Shapes.Add(group);
Dim shape1 As Shape = New Shape(ShapeType.Star12)
shape1.Outline = New LineStyle With {
.Fill = New SolidFill(Color.DarkGreen),
.Width = 4
}
shape1.Fill = New SolidFill(Color.Yellow)
shape1.X = 200
shape1.Y = 200
shape1.Width = 800
shape1.Height = 800
Dim shape2 As Shape = New Shape(ShapeType.Star10)
shape2.Outline = New LineStyle With {
.Fill = New SolidFill(Color.Black),
.Width = 4
}
shape2.Fill = New GroupFill()
shape2.X = 900
shape2.Y = 900
shape2.Width = 800
shape2.Height = 800
Dim group As GroupShape = New GroupShape()
group.Fill = New SolidFill(Color.DarkMagenta)
group.X = 200
group.Y = 200
group.Width = 1800
group.Height = 1800
group.Shapes.Add(shape1)
group.Shapes.Add(shape2)
slide.Shapes.Add(group)
To ungroup shapes in a group, remove the shapes from the group’s Shapes collection:
GroupShape group = slide.Shapes[0] as GroupShape;
foreach(var gr_shape in group.Shapes)
slide.Shapes.Add(gr_shape);
slide.Shapes.Remove(group);
Dim group As GroupShape = TryCast(slide.Shapes(0), GroupShape)
For Each gr_shape In group.Shapes
slide.Shapes.Add(gr_shape)
Next
slide.Shapes.Remove(group)
To add a connector between shapes, create a ConnectorShape object and add it to the Slide.Shapes collection. Specify the following connector properties:
StartShape | EndShapeSpecify start and end shapes to connect.StartShapeSiteIndex | EndShapeSiteIndexSpecify connection point indexes of connected shapes. The index starts with the top side (0) and increases counterclockwise. Use the shape.Type.Geometry.ConnectionSites.Count property to obtain the number of connection sites for the current shape.TypeSpecifies the connector type.OutlineSpecifies connector appearance.
The following code snippet adds a connector between two shapes:
using DevExpress.Docs.Presentation;
using System.Drawing;
namespace PresentationApiSample;
public class Program {
public static void Main(string[] _) {
//...
Shape shape1 = new Shape(ShapeType.Rectangle);
shape1.Outline = new OutlineStyle { Fill = new SolidFill(Color.DarkGreen), Width = 8 };
shape1.X = 30;
shape1.Y = 30;
shape1.Width = 800;
shape1.Height = 800;
slide.Shapes.Add(shape1);
Shape shape2 = new Shape(ShapeType.Rectangle);
shape2.Outline = new OutlineStyle { Fill = new SolidFill(Color.DarkGreen), Width = 8 };
shape2.X = 1200;
shape2.Y = 1200;
shape2.Width = 800;
shape2.Height = 800;
slide.Shapes.Add(shape2);
ConnectorShape connector = new ConnectorShape();
connector.StartShape = shape1;
connector.EndShape = shape2;
connector.StartShapeSiteIndex = 2;
connector.EndShapeSiteIndex = 0;
connector.Type = ConnectorShapeType.Curved;
connector.Outline = new LineStyle {
Fill = new SolidFill(Color.Red),
Width = 6,
EndArrowType = ArrowType.TriangleArrow,
EndLength = ArrowSize.Large,
EndWidth = ArrowSize.Large
};
slide.Shapes.Add(connector);
}
}
Imports DevExpress.Docs.Presentation
Imports System.Drawing
Namespace PresentationApiSample
Public Class Program
Public Shared Sub Main(__ As String())
' ...
Dim shape1 As Shape = New Shape(ShapeType.Rectangle)
shape1.Outline = New OutlineStyle With {
.Fill = New SolidFill(Color.DarkGreen),
.Width = 8
}
shape1.X = 30
shape1.Y = 30
shape1.Width = 800
shape1.Height = 800
slide.Shapes.Add(shape1)
Dim shape2 As Shape = New Shape(ShapeType.Rectangle)
shape2.Outline = New OutlineStyle With {
.Fill = New SolidFill(Color.DarkGreen),
.Width = 8
}
shape2.X = 1200
shape2.Y = 1200
shape2.Width = 800
shape2.Height = 800
slide.Shapes.Add(shape2)
Dim connector As ConnectorShape = New ConnectorShape()
connector.StartShape = shape1
connector.EndShape = shape2
connector.StartShapeSiteIndex = 2
connector.EndShapeSiteIndex = 0
connector.Type = ConnectorShapeType.Curved
connector.Outline = New LineStyle With {
.Fill = New SolidFill(Color.Red),
.Width = 6,
.EndArrowType = ArrowType.TriangleArrow,
.EndLength = ArrowSize.Large,
.EndWidth = ArrowSize.Large
}
slide.Shapes.Add(connector)
End Sub
End Class
End Namespace
The following methods allow you to rearrange shapes within the Shapes collection. The shape order within the collection affects the shape stacking order (z-index) on a slide:
SendToBackPlaces a shape behind all other shapes on the slide.SendBackwardMoves a shape down one level in the shape plot order.BringToFrontPlaces a shape in front of all other shapes on the slide.BringForwardMoves a shape up one level in the shape plot order.MoveMoves a shape to a specified position.
slide.Shapes.SendToBack(shape1);
slide.Shapes.SendToBack(0);
slide.Shapes.SendBackward(shape2);
slide.Shapes.SendBackward(1);
slide.Shapes.BringForward(shape3);
slide.Shapes.BringForward(2);
slide.Shapes.BringToFront(shape4);
slide.Shapes.BringToFront(3);
slide.Shapes.Move(shape2, 0);
slide.Shapes.Move(1, 0);
slide.Shapes.SendToBack(shape1)
slide.Shapes.SendToBack(0)
slide.Shapes.SendBackward(shape2)
slide.Shapes.SendBackward(1)
slide.Shapes.BringForward(shape3)
slide.Shapes.BringForward(2)
slide.Shapes.BringToFront(shape4)
slide.Shapes.BringToFront(3)
slide.Shapes.Move(shape2, 0)
slide.Shapes.Move(1, 0)
You can create custom shapes - build the geometry you need from basic elements, such as lines and arcs.
To create a custom shape, pass a ShapeGeometry instance to the Shape constructor. Populate geometry Paths with ShapePath objects. Each shape path should consist of simple building blocks:
The following code snippet adds a custom shape to a slide:
ShapeGeometry shapeGeometry = new ShapeGeometry();
ShapePath path = new ShapePath(400, 400, true);
path.Segments.Add(new PathMove(new AdjustCoordinate(0), new AdjustCoordinate(0)));
path.Segments.Add(new PathLine(new AdjustCoordinate(0), new AdjustCoordinate(400)));
path.Segments.Add(new PathLine(new AdjustCoordinate(400), new AdjustCoordinate(400)));
path.Segments.Add(new PathLine(new AdjustCoordinate(400), new AdjustCoordinate(0)));
path.Segments.Add(new PathClose());
path.FillMode = FillMode.Normal;
shapeGeometry.Paths.Add(path);
Shape shape = new Shape(new ShapeType(shapeGeometry), 500, 1800, 400, 400);
shape.Fill = new SolidFill(Color.DarkGreen);
shape.Outline.Fill = new SolidFill(Color.LimeGreen);
slide.Shapes.Add(shape);
Dim shapeGeometry As ShapeGeometry = New ShapeGeometry()
Dim path As ShapePath = New ShapePath(400, 400, True)
path.Segments.Add(New PathMove(New AdjustCoordinate(0), New AdjustCoordinate(0)))
path.Segments.Add(New PathLine(New AdjustCoordinate(0), New AdjustCoordinate(400)))
path.Segments.Add(New PathLine(New AdjustCoordinate(400), New AdjustCoordinate(400)))
path.Segments.Add(New PathLine(New AdjustCoordinate(400), New AdjustCoordinate(0)))
path.Segments.Add(New PathClose())
path.FillMode = FillMode.Normal
shapeGeometry.Paths.Add(path)
Dim shape As Shape = New Shape(New ShapeType(shapeGeometry), 500, 1800, 400, 400)
shape.Fill = New SolidFill(Color.DarkGreen)
shape.Outline.Fill = New SolidFill(Color.LimeGreen)
slide.Shapes.Add(shape)
You can apply different visual effects to shapes. Use the shape’s Effects property to access effect settings. The following effects are available:
The following code snippet adds a shadow to a shape:
Shape shape = new Shape(ShapeType.Rectangle);
shape.Outline = new LineStyle { Fill = new SolidFill(Color.DarkRed), Width = 4 };
shape.Fill = new SolidFill(Color.LightGreen);
shape.X = 30;
shape.Y = 30;
shape.Width = 800;
shape.Height = 800;
ShapeEffectProperties effects = new ShapeEffectProperties();
effects.OuterShadow = new OuterShadowEffect { BlurRadius = 50, Color = new OfficeColor(Color.Gray), HorizontalScale = 120, VerticalScale = 120 };
shape.Effects = effects;
slide.Shapes.Add(shape);
Dim shape As Shape = New Shape(ShapeType.Rectangle)
shape.Outline = New LineStyle With {
.Fill = New SolidFill(Color.DarkRed),
.Width = 4
}
shape.Fill = New SolidFill(Color.LightGreen)
shape.X = 30
shape.Y = 30
shape.Width = 800
shape.Height = 800
Dim effects As ShapeEffectProperties = New ShapeEffectProperties()
effects.OuterShadow = New OuterShadowEffect With {
.BlurRadius = 50,
.Color = New OfficeColor(Color.Gray),
.HorizontalScale = 120,
.VerticalScale = 120
}
shape.Effects = effects
slide.Shapes.Add(shape)
Call the following methods to remove a shape from a presentation:
Remove - Removes the given shape object.
RemoveAt - Removes the shape with the given index.
Clear - Removes all shapes from the collection.
slide.Shapes.Remove(shape);
slide.Shapes.RemoveAt(0);
slide.Shapes.Clear();
slide.Shapes.Remove(shape)
slide.Shapes.RemoveAt(0)
slide.Shapes.Clear()
In presentations, locks are settings that restrict user operations or hide visual elements. The DevExpress Presentation API library ships with individual lock settings for regular shapes, group shapes, picture shapes, and connectors. Use the following properties to access lock settings:
The following code snippet locks all user operations for shapes:
using DevExpress.Docs.Presentation;
using System.Drawing;
namespace PresentationApiSample;
public class Program {
public static void Main(string[] _) {
// ...
shape.LockSettings.DisableArrowheadsChange = true;
shape.LockSettings.DisableAspectRatioChange = true;
shape.LockSettings.DisableGrouping = true;
shape.LockSettings.DisableHandles = true;
shape.LockSettings.DisableMoving = true;
shape.LockSettings.DisablePointsEdit = true;
shape.LockSettings.DisableResize = true;
shape.LockSettings.DisableRotation = true;
shape.LockSettings.DisableSelection = true;
shape.LockSettings.DisableShapeTypeChange = true;
shape.LockSettings.DisableTextEdit = true;
group.LockSettings.DisableAspectRatioChange = true;
group.LockSettings.DisableGrouping = true;
group.LockSettings.DisableMoving = true;
group.LockSettings.DisableRotation = true;
group.LockSettings.DisableSelection = true;
group.LockSettings.DisableUngrouping = true;
group.LockSettings.DisableResize = true;
picture.LockSettings.DisableSelection = true;
picture.LockSettings.DisableArrowheadsChange = true;
picture.LockSettings.DisableAspectRatioChange = true;
picture.LockSettings.DisableCropping = true;
picture.LockSettings.DisableGrouping = true;
picture.LockSettings.DisableHandles = true;
picture.LockSettings.DisableMoving = true;
picture.LockSettings.DisablePointsEdit = true;
picture.LockSettings.DisableResize = true;
picture.LockSettings.DisableRotation = true;
picture.LockSettings.DisableSelection = true;
picture.LockSettings.DisableShapeTypeChange = true;
connector.LockSettings.DisableArrowheadsChange = true;
connector.LockSettings.DisableAspectRatioChange = true;
connector.LockSettings.DisableGrouping = true;
connector.LockSettings.DisableHandles = true;
connector.LockSettings.DisableMoving = true;
connector.LockSettings.DisablePointsEdit = true;
connector.LockSettings.DisableResize = true;
connector.LockSettings.DisableRotation = true;
connector.LockSettings.DisableSelection = true;
connector.LockSettings.DisableShapeTypeChange = true;
}
}
Imports DevExpress.Docs.Presentation
Imports System.Drawing
Namespace PresentationApiSample
Public Class Program
Public Shared Sub Main(__ As String())
' ...
shape.LockSettings.DisableArrowheadsChange = True
shape.LockSettings.DisableAspectRatioChange = True
shape.LockSettings.DisableGrouping = True
shape.LockSettings.DisableHandles = True
shape.LockSettings.DisableMoving = True
shape.LockSettings.DisablePointsEdit = True
shape.LockSettings.DisableResize = True
shape.LockSettings.DisableRotation = True
shape.LockSettings.DisableSelection = True
shape.LockSettings.DisableShapeTypeChange = True
shape.LockSettings.DisableTextEdit = True
group.LockSettings.DisableAspectRatioChange = True
group.LockSettings.DisableGrouping = True
group.LockSettings.DisableMoving = True
group.LockSettings.DisableRotation = True
group.LockSettings.DisableSelection = True
group.LockSettings.DisableUngrouping = True
group.LockSettings.DisableResize = True
picture.LockSettings.DisableSelection = True
picture.LockSettings.DisableArrowheadsChange = True
picture.LockSettings.DisableAspectRatioChange = True
picture.LockSettings.DisableCropping = True
picture.LockSettings.DisableGrouping = True
picture.LockSettings.DisableHandles = True
picture.LockSettings.DisableMoving = True
picture.LockSettings.DisablePointsEdit = True
picture.LockSettings.DisableResize = True
picture.LockSettings.DisableRotation = True
picture.LockSettings.DisableSelection = True
picture.LockSettings.DisableShapeTypeChange = True
connector.LockSettings.DisableArrowheadsChange = True
connector.LockSettings.DisableAspectRatioChange = True
connector.LockSettings.DisableGrouping = True
connector.LockSettings.DisableHandles = True
connector.LockSettings.DisableMoving = True
connector.LockSettings.DisablePointsEdit = True
connector.LockSettings.DisableResize = True
connector.LockSettings.DisableRotation = True
connector.LockSettings.DisableSelection = True
connector.LockSettings.DisableShapeTypeChange = True
End Sub
End Class
End Namespace
Slides can inherit shapes from slide masters and layouts. You can also populate placeholder shapes obtained from layouts with content.
For more information, refer to the following help topic: Configure Slide Masters and Layouts.
For information on how to add and format text within shapes, refer to the following help topic: Work with Text in Shapes.