Shapes
- 5 minutes to read
The WPF Rich Text Editor allows you to view, print, and export (PDF) documents containing shapes. The RichEditControl supports all shape types: from simple lines and rectangles to 3-D shapes with advanced effects.
This document covers:
Shapes in the User Interface
End-users can move, resize, rotate, or remove shapes.
When end–users select a shape, the Picture Tools contextual tab becomes available. Items on this tab allow you to change the shape’s position, text flow around the drawing object, and define the shape fill or outline color.
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.
Important
The RichEditControl provides user interface elements to create text boxes and pictures only. You cannot create or edit other shape types.
Manage Shapes in Code
You can perform the following actions with shapes.
Important
The RichEditControl does not support using shapes in OpenDocument Text (.odt) documents.
Access a Shape
The ShapeCollection contains all document drawing objects. Access the collection using the Shapes property. You can retrieve the collection item its index or name using the array notation. Use the Name property to specify the drawing object’s name. Assign the Shape.Range to the Document.Selection property to select a shape.
Important
The ShapeCollection collection does not include all shape objects, so you access and modify only text boxes and pictures in code.
Create a Shape
The RichEditControl provides the following API to create only pictures and text boxes.
- ShapeCollection.InsertTextBox - Creates a text box.
- ShapeCollection.InsertPicture - Creates a picture.
Note
The DocumentImageCollection.Append and DocumentImageCollection.Insert methods create images and add them to the ShapeCollection.
The RichEditControl supports the following graphic types:
- Bitmap (*.bmp, *.dib)
- JPEG File Interchange Format (*.jpg, *.jpeg)
- Portable Network Graphics (*.png)
- Graphics Interchange Format (*.gif)
- Tagged Image Format (*.tif, *.tiff)
- Microsoft Enhanced Metafile (*.emf)
- Windows Metafile (*.wmf)
//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 define the drawing object’s position within the document.
API | Description |
---|---|
Shape.ZOrder | Places the floating object behind or in front of other shapes contained in the document. |
Shape.Offset | Positions the object relative to a certain point. |
Shape.HorizontalAlignment Shape.VerticalAlignment |
Positions the object relative to the page elements. |
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.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/winforms-richedit-document-api-e5219.
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 defines 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 pictures.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/winforms-richedit-document-api-e5219.
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.5f;
s.ScaleY = 0.5f;
}
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 drawing 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 text box.
richEditControl1.Document.Shapes[1].Line.Color = System.Drawing.Color.CornflowerBlue;
richEditControl1.Document.Shapes[1].Line.Thickness = 5;
richEditControl1.Document.Shapes[1].Fill.Color = System.Drawing.Color.Beige;
Delete the Shape
The ShapeCollection items cannot be deleted. Delete the range related to 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);