[]
        
(Showing Draft Content)

Manage Shape Text Options

In DsExcel, you can configure the text and text style for the shape as per your own preferences by using the TextFrame property of the IShape interface.

Refer to the following example code to configure the text and text style for the inserted shape.

// To config shape's text and text style.
IShape shape = worksheet.Shapes.AddShape(AutoShapeType.Rectangle, 40, 40, 200, 200);
shape.TextFrame.TextRange.Font.Color.RGB = System.Drawing.Color.FromArgb(0, 255, 0);
shape.TextFrame.TextRange.Font.Bold = true;
shape.TextFrame.TextRange.Font.Italic = true;
shape.TextFrame.TextRange.Font.Size = 12;
shape.TextFrame.TextRange.Font.Strikethrough = true;

shape.TextFrame.TextRange.Paragraphs.Add("This is a rectangle shape.");
shape.TextFrame.TextRange.Paragraphs.Add("My name is Excel.");
shape.TextFrame.TextRange.Paragraphs[0].Runs.Add("Hello World!");

shape.TextFrame.TextRange.Paragraphs[0].Runs[0].Font.Strikethrough = false;
shape.TextFrame.TextRange.Paragraphs[0].Runs[0].Font.Size = 14;

Setting text on shape in GcExcel

Set Formula for Shape Text

You can set formula for a shape by using Formula property of the IShape interface. This property configures a formula that refers to text of the range or a defined name. When you set a shape formula for the first time, the shape acquires text and font style of the first cell of the reference. Once shape text has been set, any kind of changes in content of the referenced cell updates value of the shape text also. However, font style remains the same.


// set shape formula to G8 
IShape shapeResult = worksheet.Shapes.AddShape(AutoShapeType.Rectangle, worksheet.Range["B7:D8"]); 
shapeResult.Formula = "=G8";

You can remove shape reference by setting the Formula property to null. On removing reference, the shape text becomes a custom normal text; shape content gets text of the first cell of removed reference and the font style is the default style. If shape text is removed from the shape, cell reference stops having any affect on the shape text.

Further, you can also retain formula of the referenced shape when exporting to JSON IO, DsExcel API, PDF, HTML, or an image.

To view the feature in action, see Set Shape Formula demo.

Set Alignment of Shape Text

You can align the text in a shape to the left, right, center, distribute, and justify using the TextAlignment property. Also, you can secure the position of the text frame containing the text at the center using the HorizontalAnchor property and at the top, middle, and bottom using the VerticalAnchor property.

These different alignments and positions of text in a shape can also be exported to PDF documents.

Align Text

The TextAlignment property in ITextRange interface allows you to set the alignment of a text range or a paragraph in a shape using TextAlignmentAnchor enumeration. This property sets the text alignment to left, right, center, distribute, and justify.

Refer to the following example code to set the alignment of text range and paragraphs in a shape:

Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.Worksheets[0];

// Add a shape.
IShape shape = worksheet.Shapes.AddShape(AutoShapeType.Rectangle, 10, 10, 200, 200);

// Add text range and two paragraphs for the shape.
shape.TextFrame.TextRange.Text = "Text range alignment";
shape.TextFrame.TextRange.Paragraphs.Add("Aligned to the left");
shape.TextFrame.TextRange.Paragraphs.Add("Centered");
shape.TextFrame.TextRange.Paragraphs.Add("Aligned to the right");

// Align text range to the left.
shape.TextFrame.TextRange.TextAlignment = TextAlignmentAnchor.Left;

// Align paragraph to the center.
shape.TextFrame.TextRange.Paragraphs[2].TextAlignment = TextAlignmentAnchor.Center;

// Align paragraph to the right.
shape.TextFrame.TextRange.Paragraphs[3].TextAlignment = TextAlignmentAnchor.Right;

// Save the workbook in XLSX and PDF formats.
workbook.Save("Alignment.xlsx");
workbook.Save("Alignment.pdf");


Anchor Text

The text frame (or text body) contains the text or paragraph you add to a shape. The HorizontalAnchor and VerticalAnchor properties of ITextFrame interface allow you to set the horizontal and vertical anchors of a text frame in a shape using HorizontalAnchor and VerticalAnchor enumerations. The text frame can be positioned horizontally at the center or vertically at the top, middle, or bottom.

Refer to the following example code to anchor the text frame in a shape:

Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.Worksheets[0];

//Add a shape.
IShape shape = worksheet.Shapes.AddShape(AutoShapeType.Rectangle, 10, 10, 200, 200);

//Add two paragraphs for the shape.
shape.TextFrame.TextRange.Paragraphs.Add("Document Solutions for Excel");
shape.TextFrame.TextRange.Paragraphs.Add("Middle Centered");

//Centers text vertically.
shape.TextFrame.VerticalAnchor = VerticalAnchor.AnchorMiddle;
//Centers text horizontally.
shape.TextFrame.HorizontalAnchor = HorizontalAnchor.Center;

workbook.Save("Alignment.xlsx");
workbook.Save("Alignment.pdf");

text-anchor

You can also set the alignment of a text range and a paragraph, along with the anchor of the text frame in a shape. Refer to the following example code to align and anchor a paragraph at the bottom right:

Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.Worksheets[0];

// Add a shape.
IShape shape = worksheet.Shapes.AddShape(AutoShapeType.Rectangle, 10, 10, 400, 200);

// Add a paragraph for the shape.
shape.TextFrame.TextRange.Paragraphs.Add("Aligned and anchored to bottom right");

// Anchor the text frame to the bottom vertically.
shape.TextFrame.VerticalAnchor = VerticalAnchor.AnchorBottom;
            
// Align paragraph to the right.
shape.TextFrame.TextRange.Paragraphs[0].TextAlignment = TextAlignmentAnchor.Right;

// Save the workbook in XLSX and PDF formats.
workbook.Save("Alignment.xlsx");
workbook.Save("Alignment.pdf");


Note: The TextAlignmentAnchor.Mixed is a special enumeration value returned for a shape having different alignments applied to paragraphs in a text range. If you set the alignment of a text or paragraph in Shape using TextAlignment.Mixed, this will throw an exception.

Set Direction of Shape Text

You can set the direction of the text in shape to horizontal, vertical, rotated (to 90 or 270 degree), and stacked (with text reading left-to-right or right to left). The Direction property in ITextFrame interface allows you to set the direction of the text frame in shape using TextDirection enumeration.

Refer to the following example code to set the text direction to vertical:

// Initialize Workbook.
Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.Worksheets[0];

// Add a shape.
IShape shape = worksheet.Shapes.AddShape(AutoShapeType.Rectangle, 10, 10, 200, 200);

// Add paragraph for the shape.
shape.TextFrame.TextRange.Paragraphs.Add("Document Solutions for Excel");
            
// Set the direction of text frame to vertical.
shape.TextFrame.Direction = TextDirection.Vertical;

// Save the workbook.
workbook.Save("TextDirection.xlsx");

shape-text-direction

Set Margin for Shape Text

You can set the margin of text in a shape in the bottom, left, right and top directions. The MarginBottom, MarginLeft, MarginRight and MarginTop properties of ITextFrame interface can be used to achieve the same.

Refer to the following example code which configures the text margins in first shape and keeps it as default in the other.

IShape shape = worksheet.Shapes.AddShape(AutoShapeType.Rectangle, 1, 10, 150, 100);
IShape shape2 = worksheet.Shapes.AddShape(AutoShapeType.Rectangle, 200, 10, 150, 100);

//set the margin of text
shape.TextFrame.MarginBottom = 30;
shape.TextFrame.MarginLeft = 30;
shape.TextFrame.MarginRight = 30;
shape.TextFrame.MarginTop = 30;

shape.TextFrame.TextRange.Paragraphs[0].Runs.Add("Test setting margin for text in a shape");
shape2.TextFrame.TextRange.Paragraphs[0].Runs.Add("Test input text with default margin");

Setting margin for text on shape in GcExcel