Document Solutions for Excel, Java Edition | Document Solutions
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. DsExcel 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 DsExcel 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.

    Java
    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);
    rectangle.getFill().getColor().setRGB(Color.GetBlue());
    
    IShape oval = shapes.addShape(AutoShapeType.Oval, 50, 50, 100, 100);
    oval.getFill().getColor().setRGB(Color.GetGreen());
    
    IShape pentagon = shapes.addShape(AutoShapeType.Pentagon, 80, 80, 100, 100);
    pentagon.getFill().getColor().setRGB(Color.GetRed());
    
    IShape triangle = shapes.addShape(AutoShapeType.IsoscelesTriangle, 100, 100, 100, 100);
    triangle.getFill().getColor().setRGB(Color.GetOrange());
    
    // Set rectangle above oval
    rectangle.zOrder(ZOrderType.BringForward);
    
    // Get position of rectangle in z-order
    System.out.println("Z-Order rectangle: " + rectangle.getZOrderPosition());
    
    // Set triangle to bottom
    triangle.zOrder(ZOrderType.SendToBack);
    
    // Get position of triangle in z-order
    System.out.println("Z-Order triangle: " + triangle.getZOrderPosition());
    
    // Save to an excel file
    workbook.save("SetShapeZOrder.xlsx");