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.

Excel text boxes added with GemBox.Spreadsheet
Screenshot of text boxes added with GemBox.Spreadsheet
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?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Spreadsheet yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.