Skip to main content
A newer version of this page is available. .

Shapes

  • 5 minutes to read

The WinForms Rich Text Editor allows you to view, print, and export (PDF) documents containing shapes (drawing objects). The RichEditControl supports all shape types: from simple lines and rectangles, to shapes with advanced effects.

XtraRichEdit_Shapes

This document covers:

Shapes in the User Interface

End-users can move, resize, rotate, and remove shapes.

XtraRichEdit_Shapes_RotateResize

When end-users select the shape, the Picture Tools contextual tab becomes available. Items on this tab allow you to change the shape’s position, the text flow around the drawing object, and define the shape fill or outline color.

XtraRichEdit_Shapes_PictureTools

The RichEditControl provides the Shape Layout Dialog. This dialog allows end-users to specify advanced drawing object’s properties. You can invoke this dialog from the shape’s context menu.

XtraRichEdit_Shapes_ContextMenu

Important

The RichEditControl provides user interface elements to create pictures and text boxes only. You cannot create and edit other shape types.

Manage Shapes in Code

You can perform the following actions with shapes.

Important

The Word Processing Document API does not support using shapes in OpenDocument Text (.odt) documents.

Access a Shape

The ShapeCollection contains Shape objects. Access the collection using the SubDocument.Shapes property. You can retrieve the collection item by its index or name using the array notation. Use the Shape.Name property to specify the drawing object’s name.

Important

The ShapeCollection contains only text boxes and pictures. Other shape types are not accessible in code.


//Access a shape by its index
Shape myShape = document.Shapes[0];

//Access a shape by its name
Shape myShape = document.Shapes["Object1"];

Create a Shape

The RichEditControl provides the following API to create only pictures and text boxes.


//Insert a picture at the document start
richEditControl1.Document.Shapes.InsertPicture(richEditControl1.Document.Range.Start, Image.FromFile("image001.jpg"));

//Insert a text box at the end of the document
richEditControl1.Document.Shapes.InsertTextBox(richEditControl1.Document.Range.End);

Tip

Check the shape’s Shape.TextBox property to distinguish the text box from the picture.

Modify a Shape

Position

Use the Shape.TextWrapping property to specify the text flow around the drawing object. The following properties allow you to define the shape’s position within the document.

API

Description

Shape.ZOrder

Places the floating object behind or in front of other shapes.

Shape.Offset

Positions the object relative to a certain point.

Shape.HorizontalAlignment

Shape.VerticalAlignment

Positions the object relative to the page element.

Shape.RelativeHorizontalPosition

Shape.RelativeVerticalPosition

Defines the page element to which the object position is relative.

The code snippet below uses the Shape.ZOrder property to position an image behind the specified drawing object and sets the Shape.TextWrapping property to place the image behind the text.

XtraRichEdit_Shapes_PlaceBehindText

document.LoadDocument("FirstLook.docx", DevExpress.XtraRichEdit.DocumentFormat.OpenXml);
Shape myPicture = document.Shapes[1];
myPicture.VerticalAlignment = ShapeVerticalAlignment.Top;
myPicture.ZOrder = document.Shapes[0].ZOrder - 1;
myPicture.TextWrapping = TextWrappingType.BehindText;

Rotate and Resize

Use the Shape.RotationAngle property to rotate the shape. The Shape.Size property determines the shape’s size. Set the Shape.ScaleX or Shape.ScaleY property to scale the shape horizontally or vertically.

The following code rotates text boxes and resizes floating pictures.

XtraRichEdit_Shapes_RotateResize_Code

document.LoadDocument("FirstLook.docx", DevExpress.XtraRichEdit.DocumentFormat.OpenXml);
foreach (Shape s in document.Shapes)
{
 // Rotate a text box and resize a floating picture.
    if (s.TextBox == null)
    {
        s.ScaleX = 0.1f;
        s.ScaleY = 0.1f;
    }
    else
    {
        s.RotationAngle = 45;
    }
}

Change Fill and Outline Colors

You can specify the shape outline color and thickness. Retrieve the border line using the Shape.Line property and set its ShapeLine.Color and ShapeLine.Thickness properties. If the floating object’s background is transparent, you can change the fill color using the ShapeFill.Color property.

The code sample below changes the fill color and specifies the outline thickness and color for the selected shape.

XtraRichEdit_Shapes_FillAndOutline


//Access the selected shape
ReadOnlyShapeCollection selectedShapes = richEditControl1.Document.Shapes.Get(richEditControl1.Document.Selection);
Shape selectedShape = selectedShapes[0];

//Set the shape's fill and outline options
selectedShape.Line.Color = Color.CornflowerBlue;
selectedShape.Line.Thickness = 5;
selectedShape.Fill.Color = Color.Beige;

Delete a Shape

The ShapeCollection items cannot be deleted. Delete the range containing the shape’s anchor to remove the drawing object. Retrieve the range using the Shape.Range property and pass it to the SubDocument.Delete method.


DocumentRange range = richEditControl1.Document.Shapes[1].Range;
richEditControl1.Document.Delete(range);
See Also