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

ClassDescription
ShapeRepresents a drawing object (auto shape) with name, geometry, and cell-anchor position.
ShapeCollectionCollection of all drawing shapes on a worksheet. Supports count, index access, add, and remove.
AutoShapeTypeEnum specifying the preset DrawingML geometry for a shape.
ShapeModelInternal 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 AutoShapeType value 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() — the AutoShapeType enum 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 the AutoShapeType preset.
  • Shape.setUpperLeftRow(row) / Shape.setUpperLeftColumn(col) — reposition.
  • Shape.setLowerRightRow(row) / Shape.setLowerRightColumn(col) — resize anchor.

API Reference Summary

Shape

MethodReturn typeDescription
getName()StringInternal name of the shape.
setName(name)voidSets the internal name.
getGeometryType()StringGeometry type identifier string.
setGeometryType(type)voidSets the geometry type string.
getAutoShapeType()AutoShapeTypePreset auto-shape geometry enum value.
setAutoShapeType(type)voidSets the preset auto-shape geometry.
getUpperLeftRow()intRow index of the top-left anchor cell.
setUpperLeftRow(row)voidSets the top-left anchor row.
getUpperLeftColumn()intColumn index of the top-left anchor cell.
setUpperLeftColumn(col)voidSets the top-left anchor column.
getLowerRightRow()intRow index of the bottom-right anchor cell.
setLowerRightRow(row)voidSets the bottom-right anchor row.
getLowerRightColumn()intColumn index of the bottom-right anchor cell.
setLowerRightColumn(col)voidSets the bottom-right anchor column.

ShapeCollection

MethodReturn typeDescription
getCount()intNumber of shapes on the worksheet.
get(index)ShapeReturns the shape at the given zero-based index.
add(...)intAdds a new shape; returns its collection index.
removeAt(index)voidRemoves the shape at the given index.

See Also