Working with Shapes

This topic discusses how to work programmatically with shapes using Aspose.Words.

The shapes in Aspose.Words represent an object in the drawing layer, such as an AutoShape, textbox, freeform, OLE object, ActiveX control, or picture. A Word document can contain one or more different shapes. Shapes in Aspose.Words are represented by the Shape class.

Inserting Shapes Using Document Builder

You can insert inline shape with specified type and size and free-floating shape with the specified position, size and text wrap type into a document using the InsertShape method. The InsertShape method allows inserting DML shape into the document model. The document must be saved in the format, which supports DML shapes, otherwise, such nodes will be converted to VML shape, while document saving.

The following code example shows how to insert these types of shapes into the document:

Set Aspect Ratio Locked

Using Aspose.Words, you can specify whether the shape’s aspect ratio is locked through the AspectRatioLocked property.

The following code example shows how to work with the AspectRatioLocked property:

Set Shape Layout In Cell

You can also specify whether the shape is displayed inside a table or outside of it using the IsLayoutInCell property.

The following code example shows how to work with the IsLayoutInCell property:

Create Snip Corner Rectangle

You can create a snip corner rectangle using Aspose.Words. The shape types are SingleCornerSnipped, TopCornersSnipped, DiagonalCornersSnipped,  TopCornersOneRoundedOneSnipped,  SingleCornerRounded,  TopCornersRounded, and DiagonalCornersRounded.

The DML shape is created using InsertShape method with these shape types. These types cannot be used to create VML shapes. Attempt to create shape by using the public constructor of the “Shape” class raises the “NotSupportedException” exception.

The following code example shows how to insert these type of shapes into the document:

Get Actual Shape Bounds Points

Using Aspose.Words API, you can get the location and size of the shape containing block in points, relative to the anchor of the topmost shape. To do this, use the BoundsInPoints property.

The following code example shows how to work with the BoundsInPoints property:

Specify Vertical Anchor

You can specify the text vertical alignment within a shape using the VerticalAnchor property.

The following code example shows how to work with the VerticalAnchor property:

Detect SmartArt Shape

Aspose.Words also allows to detect if the Shape has a SmartArt object. To do this, use the HasSmartArt property.

The following code example shows how to work with the HasSmartArt property:

Insert Horizontal Rule into Document

You can insert horizontal rule shape into a document using the InsertHorizontalRule method.

The following code example shows how to do this:

Aspose.Words API provides the HorizontalRuleFormat property to access the properties of the horizontal rule shape. The HorizontalRuleFormat class exposes basic properties like Height, Color, NoShade etc. for the formatting of a horizontal rule.

The following code example shows how to set HorizontalRuleFormat:

Import Shapes with Math XML as Shapes into DOM

You can use the ConvertShapeToOfficeMath property to convert the shapes with EquationXML to Office Math objects. The default value of this property corresponds to Microsoft Word behavior, i.e. shapes with equation XML are not converted to Office math objects.

The following code example shows how to convert shapes to Office Math objects: