[]
        
(Showing Draft Content)

Control Position of Overlapping Shapes

The order of overlapping shapes in a worksheet is decided by their z-order positions. GcExcel allows its users to set the z-order of shapes so that their positions can be controlled while creating flow charts or business diagrams etc.

The ZOrder method in GcExcel API can be used to move the specified shape in front of or behind the other shapes. It takes ZOrderType enum as a parameter to specify the position of a shape relative to the other shapes.

The ZOrderPosition property of the IShape interface can be used to retrieve the position of a specified shape in the z-order.

Note: If the z-order of a shape is changed, the index of the shape in Worksheet.Shapes collection is also changed.

Using Code

Refer to the below example code to add various shapes, change their z-order and get their positions in z-order in a worksheet.

// Initialize workbook
Workbook workbook = new Workbook();
// Fetch default worksheet
IWorksheet worksheet = workbook.Worksheets[0];

IShapes shapes = worksheet.Shapes;

//add shapes
IShape rectangle = shapes.AddShape(AutoShapeType.Rectangle, 20, 20, 100, 100);
rectangle.Fill.Color.RGB = System.Drawing.Color.Blue;

IShape oval = shapes.AddShape(AutoShapeType.Oval, 50, 50, 100, 100);
oval.Fill.Color.RGB = System.Drawing.Color.Green;

IShape pentagon = shapes.AddShape(AutoShapeType.Pentagon, 80, 80, 100, 100);
pentagon.Fill.Color.RGB = System.Drawing.Color.Red;

IShape triangle = shapes.AddShape(AutoShapeType.IsoscelesTriangle, 100, 100, 100, 100);
triangle.Fill.Color.RGB = System.Drawing.Color.Orange;

//set rectangle above oval
rectangle.ZOrder(ZOrderType.BringForward);

//get position of rectangle in z-order
Console.WriteLine("Z-Order rectangle: " + rectangle.ZOrderPosition);

//set triangle to bottom
triangle.ZOrder(ZOrderType.SendToBack);

//get position of triangle in z-order
Console.WriteLine("Z-Order triangle: " + triangle.ZOrderPosition);

//save to an excel file
workbook.Save("setshapezorder.xlsx");