Other API Samples

Live SpreadsheetGear API Samples

Shapes TextBox

Add a textbox to a worksheet and set the text and font.

// Create a new workbook and get a reference to the active worksheet and window info.
SpreadsheetGear.IWorkbook workbook =  SpreadsheetGear.Factory.GetWorkbook();
SpreadsheetGear.IWorksheet worksheet = workbook.ActiveWorksheet;
SpreadsheetGear.IWorksheetWindowInfo windowInfo = worksheet.WindowInfo;

// Calculate the left, top, width and height of the textbox by 
// converting row and column coordinates to points.  Use fractional 
// values to get coordinates in between row and column boundaries.
double left = windowInfo.ColumnToPoints(0.5);
double top = windowInfo.RowToPoints(1.5);
double right = windowInfo.ColumnToPoints(5.5);
double bottom = windowInfo.RowToPoints(10.5);
double width = right - left;
double height = bottom - top;

// Add the textbox using the calculated bounds.
SpreadsheetGear.Shapes.IShape shape =
    worksheet.Shapes.AddTextBox(left, top, width, height);

// Get a reference to the shape's textframe and characters.
SpreadsheetGear.Shapes.ITextFrame textFrame = shape.TextFrame;
SpreadsheetGear.ICharacters characters = textFrame.Characters;

// Set the text to display in the textbox.
characters.Text =
    "SpreadsheetGear...\r\n\r\nIncludes API and " +
    "GUI support for cell comments, data validation, " +
    "pictures, text boxes, check boxes, dropdowns, " +
    "list boxes, spinners, scrollbars, buttons, lines, " +
    "many AutoShapes and more...";

// Set various font options to all text in the TextBox.
SpreadsheetGear.IFont font = characters.Font;
font.Italic = true;
font.Name = "Times New Roman";
font.Size = 12;

// Demonstrating specifying rich text by applying various formatting
// options to only the first line.
characters = textFrame.GetCharacters(0, "SpreadsheetGear...".Length);
characters.Font.Name = "Calibri";
characters.Font.Italic = false;
characters.Font.Bold = true;
characters.Font.Size = 16;
characters.Font.Color = SpreadsheetGear.Color.FromArgb(255, 233, 14, 14);

// Select the textbox.
Run This Sample
Render Image

Generate an image representation of the results of this sample, which uses the SpreadsheetGear.Drawing.Image class to convert ranges, charts and shapes to images.

Download File

Download an Excel Open XML Workbook (*.xlsx) file with the results of this sample.