# Manage Shape Text Options

Learn how to change the geometry of a shape and modify its appearance in a worksheet using DsExcel.

## Content

In DsExcel, you can configure the text and text style for the shape as per your own preferences by using the [getTextFrame](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/IShape.html#getTextFrame) of the [IShape](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/IShape.html) interface.
Refer to the following example code to configure the text and text style for the inserted shape.

```Java
// To configure the text and text style of the shape.
IShape shape = worksheet.getShapes().addShape(AutoShapeType.Rectangle, 40, 40, 100, 100);
shape.getTextFrame().getTextRange().getFont().getColor().setRGB(Color.FromArgb(0, 255, 0));
shape.getTextFrame().getTextRange().getFont().setBold(true);
shape.getTextFrame().getTextRange().getFont().setItalic(true);
shape.getTextFrame().getTextRange().getFont().setSize(20);
shape.getTextFrame().getTextRange().getFont().setStrikethrough(true);
shape.getTextFrame().getTextRange().getParagraphs().add("This is a rectangle shape.");
shape.getTextFrame().getTextRange().getParagraphs().add("My name is Excel.");
shape.getTextFrame().getTextRange().getParagraphs().get(0).getRuns().add("Hello World!");
shape.getTextFrame().getTextRange().getParagraphs().get(0).getRuns().get(0).getFont().setStrikethrough(false);
shape.getTextFrame().getTextRange().getParagraphs().get(0).getRuns().get(0).getFont().setSize(14);
```

![Setting text on shape in GcExcel](https://cdn.mescius.io/document-site-files/images/dd59ea42-cd61-4fa6-a018-6231c2a9c598/images/text-on-shape.png)

## Set Formula for Shape Text

You can set formula for a shape by using [setFormula](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/IShape.html#setFormula) method of the [IShape](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/IShape.html) interface. This method 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 acquired 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 reference cell updates value of the shape text also. However, font style remains the same.

![](https://cdn.mescius.io/document-site-files/images/dd59ea42-cd61-4fa6-a018-6231c2a9c598/images/shape-formula.png)

```Java
// set shape formula to G8
IShape shapeResult = worksheet.getShapes().addShape(AutoShapeType.Rectangle, worksheet.getRange("B7:D8"));
shapeResult.setFormula("=G8");
```

You can remove shape reference by setting **setFormula** method 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 reference shape when exporting to JSON IO, DsExcel API, PDF,HTML, or an image.
To view the feature in action, see [Set Shape Formula](https://developer.mescius.com/documents-api-excel-java/demos/setshapeformula) demo.

## Set Alignment of Shape Text

You can align the text in a shape to the left, right, center, distribute, and justify using the **setTextAlignment** method. Also, you can secure the position of the text frame containing the text at the center using the **setHorizontalAnchor** method and at the top, middle, and bottom using the **setVerticalAnchor** method.
These different alignments and positions of text in a shape can also be exported to PDF documents.

### Align Text

The [setTextAlignment](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/ITextRange.html#setTextAlignment) method in [ITextRange](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/ITextRange.html) interface allows you to set the alignment of a text range or a paragraph in a shape using [TextAlignmentAnchor](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/TextAlignmentAnchor.html) enumeration. This method 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:

```Java
Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.getWorksheets().get(0);

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

// Add text range and two paragraphs for the shape.
shape.getTextFrame().getTextRange().setText("Text range alignment");
shape.getTextFrame().getTextRange().getParagraphs().add("Aligned to the left");
shape.getTextFrame().getTextRange().getParagraphs().add("Centered");
shape.getTextFrame().getTextRange().getParagraphs().add("Aligned to the right");

// Align text range to the left.
shape.getTextFrame().getTextRange().setTextAlignment(TextAlignmentAnchor.Left);

// Align paragraph to the center.
shape.getTextFrame().getTextRange().getParagraphs().get(2).setTextAlignment(TextAlignmentAnchor.Center);

// Align paragraph to the right.
shape.getTextFrame().getTextRange().getParagraphs().get(3).setTextAlignment(TextAlignmentAnchor.Right);

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

![](https://cdn.mescius.io/document-site-files/images/dd59ea42-cd61-4fa6-a018-6231c2a9c598/images/text-aligned.png?width=400)

### Anchor Text

The text frame (or text body) contains the text or paragraph you add to a shape. The [setHorizontalAnchor](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/ITextFrame.html#setHorizontalAnchor) and [setVerticalAnchor](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/ITextFrame.html#setVerticalAnchor) methods of [ITextFrame](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/ITextFrame.html) interface allow you to set the horizontal and vertical anchors of a text frame in a shape using [HorizontalAnchor](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/HorizontalAnchor.html) and [VerticalAnchor](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/VerticalAnchor.html) 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:

```Java
// Create a new workbook.
Workbook workbook = new Workbook();

// Fetch default worksheet.
IWorksheet worksheet = workbook.getWorksheets().get(0);

// Add a shape.
IShape shape = worksheet.getShapes().addShape(AutoShapeType.Rectangle, 10, 10, 300, 300);

// Add two paragraphs for the shape.
shape.getTextFrame().getTextRange().getParagraphs().add("Document Solutions for Excel");
shape.getTextFrame().getTextRange().getParagraphs().add("Middle Centered");

// Centers text vertically.
shape.getTextFrame().setVerticalAnchor(VerticalAnchor.AnchorMiddle);

// Centers text horizontally.
shape.getTextFrame().setHorizontalAnchor(HorizontalAnchor.Center);

// Save workbook.
workbook.save("Alignment.xlsx");
workbook.save("Alignment.pdf");
```

![text-anchor](https://cdn.mescius.io/document-site-files/images/871475f5-000f-4c6f-884e-8b80466fda96/text-anchor.f45a6e.png?width=400)

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:

```Java
Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.getWorksheets().get(0);

// Add a shape.
IShape shape = worksheet.getShapes().addShape(AutoShapeType.Rectangle, 10, 10, 300, 300);

// Add a paragraph for the shape.
shape.getTextFrame().getTextRange().getParagraphs().add("Aligned and anchored to bottom right");

// Anchor the text frame to the bottom vertically.
shape.getTextFrame().setVerticalAnchor(VerticalAnchor.AnchorBottom);
        
// Align paragraph to the right.
shape.getTextFrame().getTextRange().getParagraphs().get(0).setTextAlignment(TextAlignmentAnchor.Right);

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

![](https://cdn.mescius.io/document-site-files/images/dd59ea42-cd61-4fa6-a018-6231c2a9c598/images/aligned-bottom-right.png?width=400)

>type=note
> **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 [setDirection](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/ITextFrame.html#setDirection) method in [ITextFrame](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/ITextFrame.html) interface allows you to set the direction of the text frame in shape using [TextDirection](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/TextDirection.html) enumeration.
Refer to the following example code to set the text direction to vertical:

```Java
// Initialize Workbook.
Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.getWorksheets().get(0);

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

// Add paragraph for the shape.
shape.getTextFrame().getTextRange().getParagraphs().add("Document Solutions for Excel");
        
// Set the direction of text frame to vertical.
shape.getTextFrame().setDirection(TextDirection.Vertical);

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

![shape-text-direction](https://cdn.mescius.io/document-site-files/images/871475f5-000f-4c6f-884e-8b80466fda96/shape-text-direction.7732f7.png?width=400)

## Set Margin of Shape Text

You can set the margin of text in a shape in the bottom, left, right and top directions. The [setMarginBottom](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/ITextFrame.html#setMarginBottom), [setMarginLeft](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/ITextFrame.html#setMarginLeft), [setMarginRight](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/ITextFrame.html#setMarginRight) and [setMarginTop](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/ITextFrame.html#setMarginTop) methods of [ITextFrame](/document-solutions/java-excel-api/api/online/com/grapecity/documents/excel/drawing/ITextFrame.html) 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.

```Java
//create a new workbook
Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.getWorksheets().get(0);
IShape shape = worksheet.getShapes().addShape(AutoShapeType.Rectangle, 1, 10, 250, 200);
IShape shape2 = worksheet.getShapes().addShape(AutoShapeType.Rectangle, 300, 10, 250, 200);

//set the margin of text
shape.getTextFrame().setMarginBottom(40);
shape.getTextFrame().setMarginTop(40);
shape.getTextFrame().setMarginRight(40);
shape.getTextFrame().setMarginLeft(40);

shape.getTextFrame().getTextRange().getParagraphs().get(0).getRuns().add("Test setting margin for text in a shape");
shape2.getTextFrame().getTextRange().getParagraphs().get(0).getRuns().add("Test input text with default margin");

//save to an excel file
workbook.save("SetMarginOfShapeText.xlsx");
```

![Setting margin for text on shape in GcExcel](https://cdn.mescius.io/document-site-files/images/dd59ea42-cd61-4fa6-a018-6231c2a9c598/images/shape_text_margin.png)