Back to Devexpress

How to: Add Text to a Shape

officefileapi-120690-spreadsheet-document-api-examples-shapes-how-to-add-text-to-a-shape.md

latest4.4 KB
Original Source

How to: Add Text to a Shape

  • Sep 19, 2023
  • 2 minutes to read

This topic describes how to add text to a shape.

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:

csharp
Shape textBox = worksheet.Shapes.AddTextBox(50, 120, 500, 100, "Spreadsheet");
textBox.Fill.SetSolidFill(Color.PowderBlue);
textBox.Rotation = 30;
vb
Dim textBox As Shape = 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 an Existing Shape

The table below lists API members used to add and format shape text:

MemberDescription
Shape.ShapeTextReturns shape text options.
ShapeText.CharactersReturns a ShapeTextRange object that specifies a text range within a shape.
ShapeTextRange.TextDefines shape text.
ShapeText.FormulaAllows you to link shape text to a cell.
ShapeTextRange.AddBeforeAdds a new text range before the current range.
ShapeTextRange.AddAfterAdds a new text range after the current range.
ShapeTextRange.FontAllows you to change font attributes for a shape text range.
ShapeTextRange.ParagraphFormatAllows you to specify paragraph options for a shape text range.
ShapeText.VerticalAnchorSpecifies the vertical alignment for shape text.
ShapeText.HorizontalAnchorSpecifies the horizontal alignment for shape text.

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

csharp
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;
vb
Dim shapeText As ShapeText = shape.ShapeText

' Create a text range.
Dim range As ShapeTextRange = 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.  
Dim range2 As ShapeTextRange = 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