Text Boxes
TextBox
elements are special rectangular areas that contain text. ExcelShape
elements can specify text boxes using the ExcelShape.Text
property.
For more information about text boxes, text paragraphs and text elements, see the GemBox.Spreadsheet content model help page.
The following example shows how you can create and customize textboxes and their content and add them to a worksheet.

using GemBox.Spreadsheet;
using GemBox.Spreadsheet.Drawing;
class Program
{
static void Main()
{
// If using Professional version, put your serial key below.
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
var workbook = new ExcelFile();
var worksheet = workbook.Worksheets.Add("Text Boxes");
// Create the first shape.
var shape = worksheet.Shapes.Add(ShapeType.Rectangle, "B2", "D8");
// Get the shape's text content.
var textBox = shape.Text;
// Create the first paragraph with bold, red run element.
var run = textBox.Paragraphs.Add().Elements.AddRun("Shows how to use text boxes with GemBox.Spreadsheet component.");
run.Format.Bold = true;
run.Format.Fill.SetSolid(DrawingColor.FromName(DrawingColorName.Orange));
// Create an empty paragraph.
textBox.Paragraphs.Add();
// Create a right-aligned (multi-line) paragraph.
var paragraph = textBox.Paragraphs.Add();
paragraph.Format.Alignment = HorizontalAlignment.Right;
// Create and add a run element.
run = paragraph.Elements.AddRun("This is a ...");
var lineBreak = paragraph.Elements.AddLineBreak();
run = paragraph.Elements.AddRun("... multi-line paragraph.");
// Create the second shape.
shape = worksheet.Shapes.Add(ShapeType.Oval, 200, 50, 150, 150, LengthUnit.Point);
shape.Fill.SetSolid(DrawingColor.FromName(DrawingColorName.DarkOliveGreen));
shape.Outline.Fill.SetNone();
textBox = shape.Text;
textBox.Format.VerticalAlignment = VerticalAlignment.Middle;
// Create a list.
paragraph = textBox.Paragraphs.Add();
paragraph.Elements.AddRun("This is a paragraph list:");
paragraph = textBox.Paragraphs.Add();
paragraph.Format.List.NumberType = ListNumberType.DecimalPeriod;
run = paragraph.Elements.AddRun("First list item");
paragraph = textBox.Paragraphs.Add();
paragraph.Format.List.NumberType = ListNumberType.DecimalPeriod;
run = paragraph.Elements.AddRun("Second list item");
paragraph = textBox.Paragraphs.Add();
paragraph.Format.List.NumberType = ListNumberType.DecimalPeriod;
run = paragraph.Elements.AddRun("Third list item");
workbook.Save("Text Boxes.%OutputFileType%");
}
}
Imports GemBox.Spreadsheet
Imports GemBox.Spreadsheet.Drawing
Module Program
Sub Main()
' If using Professional version, put your serial key below.
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
Dim workbook = New ExcelFile
Dim worksheet = workbook.Worksheets.Add("Text Boxes")
' Create the first shape.
Dim shape = worksheet.Shapes.Add(ShapeType.Rectangle, "B2", "D8")
' Get the shape's text content.
Dim textBox = shape.Text
' Create the first paragraph with bold, red run element.
Dim run = textBox.Paragraphs.Add().Elements.AddRun("Shows how to use text boxes with GemBox.Spreadsheet component.")
run.Format.Bold = true
run.Format.Fill.SetSolid(DrawingColor.FromName(DrawingColorName.Orange))
' Create an empty paragraph.
textBox.Paragraphs.Add()
' Create a right-aligned (multi-line) paragraph.
Dim paragraph = textBox.Paragraphs.Add()
paragraph.Format.Alignment = HorizontalAlignment.Right
' Create and add a run element.
run = paragraph.Elements.AddRun("This is a ...")
Dim lineBreak = paragraph.Elements.AddLineBreak()
run = paragraph.Elements.AddRun("... multi-line paragraph.")
' Create the second shape.
shape = worksheet.Shapes.Add(ShapeType.Oval, 200, 50, 150, 150, LengthUnit.Point)
shape.Fill.SetSolid(DrawingColor.FromName(DrawingColorName.DarkOliveGreen))
shape.Outline.Fill.SetNone()
textBox = shape.Text
textBox.Format.VerticalAlignment = VerticalAlignment.Middle
' Create a list.
paragraph = textBox.Paragraphs.Add()
paragraph.Elements.AddRun("This is a paragraph list:")
paragraph = textBox.Paragraphs.Add()
paragraph.Format.List.NumberType = ListNumberType.DecimalPeriod
run = paragraph.Elements.AddRun("First list item")
paragraph = textBox.Paragraphs.Add()
paragraph.Format.List.NumberType = ListNumberType.DecimalPeriod
run = paragraph.Elements.AddRun("Second list item")
paragraph = textBox.Paragraphs.Add()
paragraph.Format.List.NumberType = ListNumberType.DecimalPeriod
run = paragraph.Elements.AddRun("Third list item")
workbook.Save("Text Boxes.%OutputFileType%")
End Sub
End Module
Want more?
Like it?
Published: January 14, 2020 | Modified: September 10, 2020 | Author: Marek Turis