Documents for Excel, Java Edition Documentation
Features / Shapes And Pictures / Control Position of Overlapping Shapes
In This Topic
    Control Position of Overlapping Shapes
    In This Topic

    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 getZOrderPosition method 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.

    Copy Code
    // Create a new workbook
    Workbook workbook = new Workbook();
    // Fetch default worksheet
    IWorksheet worksheet = workbook.getActiveSheet();
    IShapes shapes = worksheet.getShapes();
    // Add shapes
    IShape rectangle = shapes.addShape(AutoShapeType.Rectangle, 20, 20, 100, 100);
    IShape oval = shapes.addShape(AutoShapeType.Oval, 50, 50, 100, 100);
    IShape pentagon = shapes.addShape(AutoShapeType.Pentagon, 80, 80, 100, 100);
    IShape triangle = shapes.addShape(AutoShapeType.IsoscelesTriangle, 100, 100, 100, 100);
    // Set rectangle above oval
    // Get position of rectangle in z-order
    System.out.println("Z-Order rectangle: " + rectangle.getZOrderPosition());
    // Set triangle to bottom
    // Get position of triangle in z-order
    System.out.println("Z-Order triangle: " + triangle.getZOrderPosition());
    // Save to an excel file"SetShapeZOrder.xlsx");