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

How to: Add Text to a Shape

  • 2 minutes to read

This example illustrates how to add text to a shape. You can use one of the following approaches to complete the task.

Create a Text Box

Call the ShapeCollection.AddTextBox method to create a text box.

The code sample below shows how to insert, rotate and color a text box:

Spreadsheet_AddTextBox

Shape textBox = worksheet.Shapes.AddTextBox(50, 120, 500, 100, "Spreadsheet");
textBox.Fill.SetSolidFill(Color.PowderBlue);
textBox.Rotation = 30;

Tip

You can change the text box’s geometry type by setting the ShapeGeometry.Preset property to one of the ShapeGeometryPreset values.

Add Text to a Shape

The table below lists an API used to add and format a shape text.

Member Description
Shape.ShapeText Provides access to the shape’s text options.
ShapeText.Characters Returns a ShapeTextRange object representing a shape text range.
ShapeTextRange.AddBefore Adds a new text range before the current range.
ShapeTextRange.AddAfter Adds a new range after the current range.
ShapeTextRange.Text Defines a shape’s text.
ShapeTextRange.Font Provides access to the shape text’s font properties.
ShapeTextRange.ParagraphFormat Provides access to the shape text’s paragraph properties.
ShapeText.VerticalAnchor Specifies the shape text’s vertical alignment.
ShapeText.HorizontalAnchor Specifies the shape text’s horizontal alignment.

The code sample below creates and formats a shape text to look as it does on the image below.

XtraSpreadsheet_Shapes_AddShapeText


ShapeText shapeText = shape.ShapeText;

// Create a text range.
ShapeTextRange range = shapeText.Characters();

// Specify the shape's text.
range.Text = "Shape ";

// Set font properties.
range.Font.Bold = true;
range.Font.Color = Color.YellowGreen;

// Add a new text range after the existing text
// and specify its font attributes.         
ShapeTextRange range2 = range.AddAfter("Text");
range2.Font.Italic = true;
range2.Font.Name = "Arial";
range2.Font.Color = Color.BurlyWood;

// Define the text's vertical and horizontal alignment.
shapeText.VerticalAnchor = ShapeTextVerticalAnchorType.Center;
shapeText.HorizontalAnchor = ShapeTextHorizontalAnchorType.Center;
See Also