Shapes

  • 4 minutes to read

The Word Processing Document API allows you to load/save, print and export (PDF) documents containing shapes (drawing objects). The RichEditDocumentServer supports all shape types: from simple lines and rectangles to shapes with advanced effects.

XtraRichEdit_Shapes

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 Name property to specify the drawing object's name.

// 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 RichEditDocumentServer provides the following API to create shapes, pictures and text boxes.

NOTE

The DocumentImageCollection.Append and DocumentImageCollection.Insert methods create images and add them to the ShapeCollection.

// Insert a rectangle at the beginning of the document.
Shape rectangle = richEditDocumentServer1.Document.Shapes.InsertShape(document.Range.Start, ShapeGeometryPreset.Rectangle, new RectangleF(300, 200, 500, 300));
// Fill the rectangle with color.
rectangle.Fill.SetSolidFill(Color.FromArgb(0xFF, 0xEE, 0xAD));

// Insert a picture at the end of the document.
richEditDocumentServer1.Document.Shapes.InsertPicture(richEditControl1.Document.Range.End, Image.FromFile("image001.jpg"));

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 example below uses the Shape.ZOrder property to position a picture behind the specified drawing object and sets the picture's Shape.TextWrapping property to BehindText.

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 RotationAngle property to rotate a shape. The Size property determines the shape's size. Set the ScaleX or ScaleY property to scale the shape horizontally or vertically.

The following code rotates and resizes pictures in the document.

XtraRichEdit_Shapes_RotateResize_Code

document.LoadDocument("FirstLook.docx", DevExpress.XtraRichEdit.DocumentFormat.OpenXml);
foreach (Shape s in document.Shapes)
{
    // Rotate and resize pictures.
    if (s.Type == ShapeType.Picture)
    {
        s.ScaleX = 0.8f;
        s.ScaleY = 0.8f;
        s.RotationAngle = 45;
    }
}

Format a Shape

Use the following API members to customize a shape's appearance:

  • Shape.Fill - Allows you to specify shape fill options.

  • Shape.Line - Provides access to format settings for a line or shape's border.

  • ShapeLine.Fill - Allows you to specify line fill options.

The example below shows how to create a rectangle and change its border settings.

Rich_ShapeLine_Settings

// Add a rectangle to a document.
Shape rectangle = document.Shapes.InsertShape(document.Range.Start, ShapeGeometryPreset.Rectangle, new RectangleF(300, 200, 500, 300));
// Fill the rectangle with color.
rectangle.Fill.SetSolidFill(Color.FromArgb(0xFF, 0xEE, 0xAD));
// Format the rectangle border.
ShapeLine border = rectangle.Line;
border.Color = Color.FromArgb(0x4D, 0x64, 0x8D);
border.Thickness = 6;
border.JoinType = LineJoinType.Miter;
border.DashType = LineDashType.Solid;
border.CompoundType = LineCompoundType.ThickThin;

Delete a Shape

Use the ShapeCollection.Remove and ShapeCollection.RemoveAt methods to delete drawing objects from a document.

// Remove the first shape from the collection.
richEditDocumentServer1.Document.Shapes.RemoveAt(0);

You can also delete the range that contains the shape's anchor to remove the drawing object.

See Also