Working with Shapes
Working with Shapes
Aspose.Cells FOSS for Java lets you add and manage drawing objects (auto shapes)
anchored to worksheet cells. Shapes are exposed through the ShapeCollection
accessible via Worksheet.getShapes(). Each shape has a geometry type, an
auto-shape type, a name, and a two-cell anchor position.
Overview
Shapes in a worksheet are managed through the ShapeCollection. You add a new
shape with ShapeCollection.add(autoShapeType, upperLeftRow, upperLeftRowOffset, upperLeftColumn, upperLeftColumnOffset, height, width),
which returns the zero-based index of the new shape. Pass an AutoShapeType
value to specify the preset geometry.
Once a shape is in the collection, you can read and write its name, geometry type, auto-shape type, and anchor cell positions.
Key Classes
| Class | Description |
|---|---|
Shape | Represents a drawing object (auto shape) with name, geometry, and cell-anchor position. |
ShapeCollection | Collection of all drawing shapes on a worksheet. Supports count, index access, add, and remove. |
AutoShapeType | Enum specifying the preset DrawingML geometry for a shape. |
ShapeModel | Internal serialization model for a shape. Exposes raw XML fields for advanced round-trip use. |
Adding a Shape
Use ShapeCollection.add(...) to insert a drawing object. The method accepts:
- An
AutoShapeTypevalue for the geometry preset. - The upper-left anchor (row, row offset, column, column offset) — all zero-based integers.
- The shape height and width in pixels.
The method returns the zero-based index of the new shape.
Reading Shape Properties
Given a Shape object, the following read methods are available:
Shape.getName()— the shape’s internal name string.Shape.getGeometryType()— the geometry type string.Shape.getAutoShapeType()— theAutoShapeTypeenum value.Shape.getUpperLeftRow()/Shape.getUpperLeftColumn()— the top-left anchor cell.Shape.getLowerRightRow()/Shape.getLowerRightColumn()— the bottom-right anchor cell.
Writing Shape Properties
Shape.setName(name)— rename the shape.Shape.setGeometryType(type)— change the geometry type string.Shape.setAutoShapeType(type)— change theAutoShapeTypepreset.Shape.setUpperLeftRow(row)/Shape.setUpperLeftColumn(col)— reposition.Shape.setLowerRightRow(row)/Shape.setLowerRightColumn(col)— resize anchor.
API Reference Summary
Shape
| Method | Return type | Description |
|---|---|---|
getName() | String | Internal name of the shape. |
setName(name) | void | Sets the internal name. |
getGeometryType() | String | Geometry type identifier string. |
setGeometryType(type) | void | Sets the geometry type string. |
getAutoShapeType() | AutoShapeType | Preset auto-shape geometry enum value. |
setAutoShapeType(type) | void | Sets the preset auto-shape geometry. |
getUpperLeftRow() | int | Row index of the top-left anchor cell. |
setUpperLeftRow(row) | void | Sets the top-left anchor row. |
getUpperLeftColumn() | int | Column index of the top-left anchor cell. |
setUpperLeftColumn(col) | void | Sets the top-left anchor column. |
getLowerRightRow() | int | Row index of the bottom-right anchor cell. |
setLowerRightRow(row) | void | Sets the bottom-right anchor row. |
getLowerRightColumn() | int | Column index of the bottom-right anchor cell. |
setLowerRightColumn(col) | void | Sets the bottom-right anchor column. |
ShapeCollection
| Method | Return type | Description |
|---|---|---|
getCount() | int | Number of shapes on the worksheet. |
get(index) | Shape | Returns the shape at the given zero-based index. |
add(...) | int | Adds a new shape; returns its collection index. |
removeAt(index) | void | Removes the shape at the given index. |