Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Manage Shapes

    • 12 minutes to read

    A shape is a graphic object in a document. Shapes include geometric figures (for example, rectangles, arrows, and stars), text boxes, pictures, lines, and charts. You can combine multiple shapes into a group or place shapes on a drawing canvas.

    The Word Processing Document API stores shapes as inline elements (Inline descendants) in paragraphs. You can add shapes to paragraphs in the document body, table cells, headers, and footers. All shape classes derive from the ShapeBase class:

    ShapeBase
    ├── StyledShapeBase
    │   ├── Shape: a shape with preset geometry (can contain text)
    │   ├── PictureShape: a picture
    │   └── ConnectorShape: a line or connector
    ├── ContainerShapeBase
    │   ├── GroupShape: a shape group
    │   └── CanvasShape: a drawing canvas
    ├── ChartShape: a standard chart
    └── ChartExShape: a chart of a type introduced in Microsoft Word 2016
    

    Sizes, offsets, and outline widths are in document units (1/300 of an inch). Rotation angles are in degrees.

    Add a Shape

    To create a shape, pass a ShapeType value (for example, ShapeType.RoundedRectangle or ShapeType.Star5) to the Shape(geometry) constructor.

    To add a shape to a paragraph, add the Shape object to the Paragraph.InlineElements collection. You can also call one of the following DocumentFragment methods:

    If a shape has no name, the API generates a name when you add the shape to a document. Use the ShapeBase.Name property to specify the shape name.

    The following code snippet adds three shapes to a document and writes the shape names to the console:

    using System;
    using System.Drawing;
    using System.IO;
    using DevExpress.Docs.Office;
    using DevExpress.Docs.Word;
    
    using var document = new WordDocument();
    var paragraph = document.Body.AddParagraph("Rating: ");
    
    // Add a named star to the paragraph.
    var star = new Shape(ShapeType.Star5) { Name = "Rating Star", Size = new SizeF(150, 150), Fill = new SolidFill(Color.Yellow) };
    paragraph.InlineElements.Add(star);
    
    // Add a rounded rectangle to the last paragraph of the document body.
    var badge = new Shape(ShapeType.RoundedRectangle) { Size = new SizeF(600, 150), Fill = new SolidFill(Color.Blue) };
    document.Body.Add(badge);
    
    // Insert an arrow at the start of the document body.
    var arrow = new Shape(ShapeType.RightArrow) { Size = new SizeF(300, 150), Fill = new SolidFill(Color.Red) };
    document.Body.Insert(0, arrow);
    
    Console.WriteLine($"{star.Name}, {badge.Name}, {arrow.Name}");
    
    Directory.CreateDirectory("output");
    using var output = File.Create(Path.Combine("output", "Document.docx"));
    document.Save(output);
    

    Use the following members to customize the shape appearance:

    Property Description
    ShapeBase.Width Specifies the shape width.
    ShapeBase.Height Specifies the shape height.
    ShapeBase.Size Specifies the shape width and height.
    Shape.Fill Specifies the shape fill.
    Shape.Outline Specifies the shape outline.
    ShapeBase.Effects Specifies visual effects (for example, a shadow).
    ShapeBase.RotationAngle Specifies the shape rotation angle.
    ShapeBase.FlipType Specifies how to flip the shape (horizontally, vertically, or both).

    The following code snippet adds an arrow to a paragraph, specifies the arrow size, fill, outline, and shadow, and rotates and flips the arrow:

    using System.Drawing;
    using System.IO;
    using DevExpress.Docs;
    using DevExpress.Docs.Office;
    using DevExpress.Docs.Word;
    
    using var document = new WordDocument();
    var paragraph = document.Body.AddParagraph("Direction: ");
    
    var arrow = new Shape(ShapeType.RightArrow) {
        Width = 600,
        Height = 300,
        Fill = new SolidFill(Color.Orange),
        Outline = new LineStyle { Fill = new SolidFill(Color.DarkOrange), Width = 6 },
        Effects = new ShapeEffectProperties {
            OuterShadow = new OuterShadowEffect {
                Color = new OfficeColor(Color.Gray),
                Distance = 20,
                Angle = 45,
                BlurRadius = 30
            }
        },
        RotationAngle = 30,
        FlipType = FlipType.Horizontal
    };
    paragraph.InlineElements.Add(arrow);
    
    Directory.CreateDirectory("output");
    using var output = File.Create(Path.Combine("output", "Document.docx"));
    document.Save(output);
    

    Use the Shape.Geometry property to modify the geometry of an existing shape.

    The following code snippet converts rectangles in a paragraph to rounded rectangles:

    foreach (var shape in paragraph.InlineElements.OfType<Shape>()) {
        if (shape.Geometry == ShapeType.Rectangle)
            shape.Geometry = ShapeType.RoundedRectangle;
    }
    

    Add Text to a Shape

    A shape can contain text (a text box). To add text to a shape, assign a ShapeTextBoxContent object to the Shape.Content property and add paragraphs to the ShapeTextBoxContent object. You can add paragraphs, tables, fields, and inline pictures to text box content.

    The following code snippet adds a rounded rectangle with text to a paragraph, specifies the shape fill and outline, and saves the document to a DOCX file:

    using System.Drawing;
    using System.IO;
    using DevExpress.Docs.Office;
    using DevExpress.Docs.Word;
    
    using var document = new WordDocument();
    var paragraph = document.Body.AddParagraph("Order status: ");
    
    var shape = new Shape(ShapeType.RoundedRectangle) {
        Size = new SizeF(900, 300),
        Fill = new SolidFill(Color.LightSteelBlue),
        Outline = new LineStyle { Fill = new SolidFill(Color.SteelBlue), Width = 6 },
        Content = new ShapeTextBoxContent()
    };
    shape.Content.AddParagraph("Approved");
    paragraph.InlineElements.Add(shape);
    
    Directory.CreateDirectory("output");
    using var output = File.Create(Path.Combine("output", "Document.docx"));
    document.Save(output);
    

    Insert a Picture

    To insert a picture, create a PictureShape object and add it to the Paragraph.InlineElements collection. To specify the image, pass a DXImage object to the PictureShape(image) constructor or assign an OfficeImage object to the PictureShape.Image property.

    If you do not specify the picture size, the API sets the picture size to the image size when you add the picture to a document. The PictureShape.OriginalSize property returns the image size.

    Use PictureShape.CropLeft, PictureShape.CropTop, PictureShape.CropRight, and PictureShape.CropBottom properties to crop the picture. Each property specifies the percentage of the image to crop from the corresponding edge. When you crop a picture, the API reduces the picture size and keeps the image scale. A negative value adds space between the image and the picture edge.

    The following code snippet inserts a picture from the Image.png file, crops 10 percent of the image at the bottom edge, and specifies alternative text:

    using System.IO;
    using DevExpress.Docs.Word;
    using DevExpress.Drawing;
    
    using var imageStream = File.OpenRead("Image.png");
    var picture = new PictureShape(DXImage.FromStream(imageStream)) {
        AlternativeText = "Company logo",
        CropBottom = 10
    };
    document.Body.AddParagraph().InlineElements.Add(picture);
    

    To make a picture accessible, use ShapeBase.AlternativeText and ShapeBase.Title properties. Set the ShapeBase.Decorative property to true to mark a picture or shape as decorative.

    Position Floating Shapes

    The ShapeBase.Anchor property specifies how the document positions a shape:

    • An inline shape (InlineAnchor) is in a text line and moves with text like a character.
    • A floating shape (FloatingAnchor) has a position relative to a page element (for example, a page, a margin, a column, or a paragraph). Text can wrap around a floating shape, or the shape can appear behind or in front of text.

    Assign a FloatingAnchor object to the ShapeBase.Anchor property to make a shape floating, or an InlineAnchor object to make the shape inline. Use the following FloatingAnchor properties to specify the shape position and text wrapping:

    The following code snippet aligns a shape to the right margin and wraps text around the shape:

    var anchor = new FloatingAnchor {
        Wrapping = TextWrapType.Square,
        HorizontalPosition = new ShapeAlignedHorizontalPosition(ShapeHorizontalAlignment.Right, ShapeRelativeHorizontalPosition.Margin),
        VerticalPosition = new ShapeAbsoluteOffsetVerticalPosition(0, ShapeRelativeVerticalPosition.Paragraph)
    };
    anchor.Margins.Left = 60;
    shape.Anchor = anchor;
    

    Draw Lines and Connectors

    To draw a line, add a ConnectorShape object to a paragraph. The parameterless ConnectorShape() constructor creates a straight connector. Use the ConnectorShape.Type property to specify another line type (for example, an elbow or curved connector).

    ShapeBase.Width and ShapeBase.Height properties specify the size of the rectangle that contains the line. Use the ConnectorShape.Outline property to specify the line color, width, dash type, and arrowheads.

    The following code snippet adds a horizontal line with an arrowhead at the end:

    var arrow = new ConnectorShape {
        Size = new SizeF(1200, 0),
        Outline = new LineStyle {
            Fill = new SolidFill(Color.DimGray),
            Width = 6,
            EndArrowType = ArrowType.TriangleArrow
        }
    };
    document.Body.AddParagraph().InlineElements.Add(arrow);
    

    Group Shapes

    A GroupShape object combines multiple shapes into a group. Use the GroupShape.Shapes property to access shapes in the group. Call the following ShapeCollection methods to add shapes to a group:

    Offsets are relative to the top-left corner of the group. Call ShapeCollection.GetOffset(shape) and ShapeCollection.SetOffset(shape, offset) methods to get and modify the position of a shape in the group. The ShapeCollection.GetContentExtent() method returns the size of the area that shapes in the group occupy.

    If you modify the group size, the API scales the size and offset of each shape in the group.

    The following code snippet groups two rectangles and an arrow and adds the group to a paragraph:

    var group = new GroupShape();
    group.Shapes.Add(new Shape(ShapeType.Rectangle), new RectangleF(0, 0, 600, 300));
    group.Shapes.Add(new Shape(ShapeType.RightArrow), new RectangleF(650, 75, 300, 150));
    group.Shapes.Add(new Shape(ShapeType.Rectangle), new RectangleF(1000, 0, 600, 300));
    document.Body.AddParagraph().InlineElements.Add(group);
    

    Add a Drawing Canvas

    A drawing canvas (CanvasShape) is an area that contains shapes. Use the CanvasShape.Shapes collection to add shapes to the canvas in the same way as you add shapes to a group. CanvasShape.Fill and CanvasShape.Outline properties specify the canvas background and border.

    A canvas and a group (GroupShape) differ as follows:

    Behavior Group Canvas
    Size If you do not specify the group size, the group size matches the area that the shapes in the group occupy. The canvas size does not depend on the shapes in the canvas.
    Resize The API scales the size and offset of each shape in the group. The API does not modify the size and offset of shapes in the canvas.
    Border A group has no border. The CanvasShape.Outline property specifies the canvas border.

    Access Shapes

    Iterate through the Paragraph.InlineElements collection and check the element type to find shapes in a paragraph. To find shapes in table cells, headers, footers, and text boxes, iterate through paragraphs in each document part.

    Use the ShapeAnchor.Type property of the ShapeBase.Anchor object to check whether a shape is inline or floating.

    The following code snippet loads a document and writes the name, type, and anchor type of each shape in the document body to the console:

    using System;
    using System.IO;
    using System.Linq;
    using DevExpress.Docs.Word;
    
    using var document = new WordDocument(File.OpenRead("Document.docx"));
    
    foreach (var paragraph in document.Body.OfType<Paragraph>()) {
        foreach (var shape in paragraph.InlineElements.OfType<ShapeBase>())
            Console.WriteLine($"{shape.Name}: {shape.GetType().Name}, {shape.Anchor.Type}");
    }
    

    Copy Shapes

    Call the ShapeBase.Clone() method to create a shape copy. The copy has the same size, anchor, fill, outline, and effects settings as the source shape. A copy of a shape with text contains a copy of the text box content. A copy of a group or canvas contains copies of shapes in the source group or canvas at the same positions.

    The following code snippet copies the first shape in a paragraph and adds the copy to the end of the paragraph:

    var source = paragraph.InlineElements.OfType<ShapeBase>().First();
    paragraph.InlineElements.Add(source.Clone());
    

    Remove Shapes

    Call the Remove(item) or RemoveAt(index) method of the Paragraph.InlineElements collection to remove a shape from a paragraph. To remove a shape from a group or canvas, call the Remove or RemoveAt method of the Shapes collection.

    Next Steps