Shape

Shape class

Represents an object in the drawing layer, such as an AutoShape, textbox, freeform, OLE object, ActiveX control, or picture.

To learn more, visit the Working with Shapes documentation article.

public sealed class Shape : ShapeBase

Constructors

NameDescription
Shape(DocumentBaseShapeType)Creates a new shape object.

Properties

NameDescription
Adjustments { get; }Provides access to the adjustment raw values of a shape. For a shape that does not contain any adjustment raw values, it returns an empty collection.
AllowOverlap { get; set; }Gets or sets a value that specifies whether this shape can overlap other shapes.
AlternativeText { get; set; }Defines alternative text to be displayed instead of a graphic.
AnchorLocked { get; set; }Specifies whether the shape’s anchor is locked.
AspectRatioLocked { get; set; }Specifies whether the shape’s aspect ratio is locked.
BehindText { get; set; }Specifies whether the shape is below or above text.
Bottom { get; }Gets the position of the bottom edge of the containing block of the shape.
Bounds { get; set; }Gets or sets the location and size of the containing block of the shape.
BoundsInPoints { get; }Gets the location and size of the containing block of the shape in points, relative to the anchor of the topmost shape.
BoundsWithEffects { get; }Gets final extent that this shape object has after applying drawing effects. Value is measured in points.
CanHaveImage { get; }Returns true if the shape type allows the shape to have an image.
Chart { get; }Provides access to the chart properties if this shape has a Chart.
CoordOrigin { get; set; }The coordinates at the top-left corner of the containing block of this shape.
CoordSize { get; set; }The width and height of the coordinate space inside the containing block of this shape.
Count { get; }Gets the number of immediate children of this node.
CustomNodeId { get; set; }Specifies custom node identifier.
DistanceBottom { get; set; }Returns or sets the distance (in points) between the document text and the bottom edge of the shape.
DistanceLeft { get; set; }Returns or sets the distance (in points) between the document text and the left edge of the shape.
DistanceRight { get; set; }Returns or sets the distance (in points) between the document text and the right edge of the shape.
DistanceTop { get; set; }Returns or sets the distance (in points) between the document text and the top edge of the shape.
virtual Document { get; }Gets the document to which this node belongs.
ExtrusionEnabled { get; }Returns true if an extrusion effect is enabled.
Fill { get; }Gets fill formatting for the shape.
FillColor { get; set; }Defines the brush color that fills the closed path of the shape.
Filled { get; set; }Determines whether the closed path of the shape will be filled.
FirstChild { get; }Gets the first child of the node.
FirstParagraph { get; }Gets the first paragraph in the shape.
FlipOrientation { get; set; }Switches the orientation of a shape.
Font { get; }Provides access to the font formatting of this object.
Glow { get; }Gets glow formatting for the shape.
HasChart { get; }Returns true if this Shape has a Chart.
HasChildNodes { get; }Returns true if this node has any child nodes.
HasImage { get; }Returns true if the shape has image bytes or links an image.
HasSmartArt { get; }Returns true if this Shape has a SmartArt object.
Height { get; set; }Gets or sets the height of the containing block of the shape.
HeightRelative { get; set; }Gets or sets the value that represents the percentage of shape’s relative height.
Hidden { get; set; }Gets or sets a boolean value indicating whether the shape is visible.
HorizontalAlignment { get; set; }Specifies how the shape is positioned horizontally.
HorizontalRuleFormat { get; }Provides access to the properties of the horizontal rule shape. For a shape that is not a horizontal rule, returns null.
HRef { get; set; }Gets or sets the full hyperlink address for a shape.
ImageData { get; }Provides access to the image of the shape. Returns null if the shape cannot have an image.
override IsComposite { get; }Returns true as this node can have child nodes.
IsDecorative { get; set; }Gets or sets the flag that specifies whether the shape is decorative in the document.
IsDeleteRevision { get; }Returns true if this object was deleted in Microsoft Word while change tracking was enabled.
IsGroup { get; }Returns true if this is a group shape.
IsHorizontalRule { get; }Returns true if this shape is a horizontal rule.
IsImage { get; }Returns true if this shape is an image shape.
IsInline { get; }A quick way to determine if this shape is positioned inline with text.
IsInsertRevision { get; }Returns true if this object was inserted in Microsoft Word while change tracking was enabled.
IsLayoutInCell { get; set; }Gets or sets a flag indicating whether the shape is displayed inside a table or outside of it.
IsMoveFromRevision { get; }Returns true if this object was moved (deleted) in Microsoft Word while change tracking was enabled.
IsMoveToRevision { get; }Returns true if this object was moved (inserted) in Microsoft Word while change tracking was enabled.
IsSignatureLine { get; }Indicates that shape is a SignatureLine.
IsTopLevel { get; }Returns true if this shape is not a child of a group shape.
IsWordArt { get; }Returns true if this shape is a WordArt object.
LastChild { get; }Gets the last child of the node.
LastParagraph { get; }Gets the last paragraph in the shape.
Left { get; set; }Gets or sets the position of the left edge of the containing block of the shape.
LeftRelative { get; set; }Gets or sets the value that represents shape’s relative left position in percent.
MarkupLanguage { get; }Gets MarkupLanguage used for this graphic object.
Name { get; set; }Gets or sets the optional shape name.
NextSibling { get; }Gets the node immediately following this node.
override NodeType { get; }Returns Shape.
OleFormat { get; }Provides access to the OLE data of a shape. For a shape that is not an OLE object or ActiveX control, returns null.
ParentNode { get; }Gets the immediate parent of this node.
ParentParagraph { get; }Returns the immediate parent paragraph.
PreviousSibling { get; }Gets the node immediately preceding this node.
Range { get; }Returns a Range object that represents the portion of a document that is contained in this node.
Reflection { get; }Gets reflection formatting for the shape.
RelativeHorizontalPosition { get; set; }Specifies relative to what the shape is positioned horizontally.
RelativeHorizontalSize { get; set; }Gets or sets the value of shape’s relative size in horizontal direction.
RelativeVerticalPosition { get; set; }Specifies relative to what the shape is positioned vertically.
RelativeVerticalSize { get; set; }Gets or sets the value of shape’s relative size in vertical direction.
Right { get; }Gets the position of the right edge of the containing block of the shape.
Rotation { get; set; }Defines the angle (in degrees) that a shape is rotated. Positive value corresponds to clockwise rotation angle.
ScreenTip { get; set; }Defines the text displayed when the mouse pointer moves over the shape.
ShadowEnabled { get; }Returns true if a shadow effect is enabled.
ShadowFormat { get; }Gets shadow formatting for the shape.
ShapeType { get; }Gets the shape type.
SignatureLine { get; }Gets SignatureLine object if the shape is a signature line. Returns null otherwise.
SizeInPoints { get; }Gets the size of the shape in points.
SoftEdge { get; }Gets soft edge formatting for the shape.
StoryType { get; }Returns Textbox.
Stroke { get; }Defines a stroke for a shape.
StrokeColor { get; set; }Defines the color of a stroke.
Stroked { get; set; }Defines whether the path will be stroked.
StrokeWeight { get; set; }Defines the brush thickness that strokes the path of a shape in points.
Target { get; set; }Gets or sets the target frame for the shape hyperlink.
TextBox { get; }Defines attributes that specify how text is displayed in a shape.
TextPath { get; }Defines the text of the text path (of a WordArt object).
Title { get; set; }Gets or sets the title (caption) of the current shape object.
Top { get; set; }Gets or sets the position of the top edge of the containing block of the shape.
TopRelative { get; set; }Gets or sets the value that represents shape’s relative top position in percent.
VerticalAlignment { get; set; }Specifies how the shape is positioned vertically.
Width { get; set; }Gets or sets the width of the containing block of the shape.
WidthRelative { get; set; }Gets or sets the value that represents the percentage of shape’s relative width.
WrapSide { get; set; }Specifies how the text is wrapped around the shape.
WrapType { get; set; }Defines whether the shape is inline or floating. For floating shapes defines the wrapping mode for text around the shape.
ZOrder { get; set; }Determines the display order of overlapping shapes.

Methods

NameDescription
override Accept(DocumentVisitor)Accepts a visitor.
override AcceptEnd(DocumentVisitor)Accepts a visitor for visiting the end of the shape.
override AcceptStart(DocumentVisitor)Accepts a visitor for visiting the start of the shape.
AdjustWithEffects(RectangleF)Adds to the source rectangle values of the effect extent and returns the final rectangle.
AppendChild<T>(T)Adds the specified node to the end of the list of child nodes for this node.
Clone(bool)Creates a duplicate of the node.
CreateNavigator()Creates navigator which can be used to traverse and read nodes.
GetAncestor(NodeType)Gets the first ancestor of the specified NodeType.
GetAncestor(Type)Gets the first ancestor of the specified object type.
GetChild(NodeType, int, bool)Returns an Nth child node that matches the specified type.
GetChildNodes(NodeType, bool)Returns a live collection of child nodes that match the specified type.
GetEnumerator()Provides support for the for each style iteration over the child nodes of this node.
GetShapeRenderer()Creates and returns an object that can be used to render this shape into an image.
override GetText()Gets the text of this node and of all its children.
IndexOf(Node)Returns the index of the specified child node in the child node array.
InsertAfter<T>(T, Node)Inserts the specified node immediately after the specified reference node.
InsertBefore<T>(T, Node)Inserts the specified node immediately before the specified reference node.
LocalToParent(PointF)Converts a value from the local coordinate space into the coordinate space of the parent shape.
NextPreOrder(Node)Gets next node according to the pre-order tree traversal algorithm.
PrependChild<T>(T)Adds the specified node to the beginning of the list of child nodes for this node.
PreviousPreOrder(Node)Gets the previous node according to the pre-order tree traversal algorithm.
Remove()Removes itself from the parent.
RemoveAllChildren()Removes all the child nodes of the current node.
RemoveChild<T>(T)Removes the specified child node.
RemoveSmartTags()Removes all SmartTag descendant nodes of the current node.
SelectNodes(string)Selects a list of nodes matching the XPath expression.
SelectSingleNode(string)Selects the first Node that matches the XPath expression.
ToString(SaveFormat)Exports the content of the node into a string in the specified format.
ToString(SaveOptions)Exports the content of the node into a string using the specified save options.
UpdateSmartArtDrawing()Updates SmartArt pre-rendered drawing by using Aspose.Words’s SmartArt cold rendering engine.

Remarks

Using the Shape class you can create or modify shapes in a Microsoft Word document.

An important property of a shape is its ShapeType. Shapes of different types can have different capabilities in a Word document. For example, only image and OLE shapes can have images inside them. Most of the shapes can have text, but not all.

Shapes that can have text, can contain Paragraph and Table nodes as children.

Examples

Shows how to insert a floating image to the center of a page.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert a floating image that will appear behind the overlapping text and align it to the page's center.
Shape shape = builder.InsertImage(ImageDir + "Logo.jpg");
shape.WrapType = WrapType.None;
shape.BehindText = true;
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
shape.HorizontalAlignment = HorizontalAlignment.Center;
shape.VerticalAlignment = VerticalAlignment.Center;

doc.Save(ArtifactsDir + "Image.CreateFloatingPageCenter.docx");

Shows how to extract images from a document, and save them to the local file system as individual files.

Document doc = new Document(MyDir + "Images.docx");

// Get the collection of shapes from the document,
// and save the image data of every shape with an image as a file to the local file system.
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

Assert.AreEqual(9, shapes.Count(s => ((Shape)s).HasImage));

int imageIndex = 0;
foreach (Shape shape in shapes.OfType<Shape>())
{
    if (shape.HasImage)
    {
        // The image data of shapes may contain images of many possible image formats. 
        // We can determine a file extension for each image automatically, based on its format.
        string imageFileName =
            $"File.ExtractImages.{imageIndex}{FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)}";
        shape.ImageData.Save(ArtifactsDir + imageFileName);
        imageIndex++;
    }
}

Shows how to delete all shapes from a document.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert two shapes along with a group shape with another shape inside it.
builder.InsertShape(ShapeType.Rectangle, 400, 200);
builder.InsertShape(ShapeType.Star, 300, 300);

GroupShape group = new GroupShape(doc);
group.Bounds = new RectangleF(100, 50, 200, 100);
group.CoordOrigin = new Point(-1000, -500);

Shape subShape = new Shape(doc, ShapeType.Cube);
subShape.Width = 500;
subShape.Height = 700;
subShape.Left = 0;
subShape.Top = 0;

group.AppendChild(subShape);
builder.InsertNode(group);

Assert.AreEqual(3, doc.GetChildNodes(NodeType.Shape, true).Count);
Assert.AreEqual(1, doc.GetChildNodes(NodeType.GroupShape, true).Count);

// Remove all Shape nodes from the document.
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
shapes.Clear();

// All shapes are gone, but the group shape is still in the document.
Assert.AreEqual(1, doc.GetChildNodes(NodeType.GroupShape, true).Count);
Assert.AreEqual(0, doc.GetChildNodes(NodeType.Shape, true).Count);

// Remove all group shapes separately.
NodeCollection groupShapes = doc.GetChildNodes(NodeType.GroupShape, true);
groupShapes.Clear();

Assert.AreEqual(0, doc.GetChildNodes(NodeType.GroupShape, true).Count);
Assert.AreEqual(0, doc.GetChildNodes(NodeType.Shape, true).Count);

See Also