Send Shape Front or Back inside the Worksheet with C++

Possible Usage Scenarios

When there are multiple shapes present in the same location, their visibility is determined by their z-order positions. Aspose.Cells provides the Shape.ToFrontOrBack() method, which changes the z-order position of the shape. If you want to send a shape to the back, you will use a negative number like -1, -2, -3, etc. If you want to bring a shape to the front, you will use a positive number like 1, 2, 3, etc.

Send Shape Front or Back inside the Worksheet

The following sample code demonstrates the usage of the Shape.ToFrontOrBack() method. Please see the sample Excel file used in the code and the output Excel file generated by it. The screenshot shows the effect of the code on the sample Excel file upon execution.

todo:image_alt_text

Sample Code

#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Output directory path
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    // Load source Excel file
    Workbook wb(srcDir + u"sampleToFrontOrBack.xlsx");

    // Access first worksheet
    Worksheet ws = wb.GetWorksheets().Get(0);

    // Access first and fourth shape
    Shape sh1 = ws.GetShapes().Get(0);
    Shape sh4 = ws.GetShapes().Get(3);

    // Print the Z-Order position of the shape
    std::cout << "Z-Order Shape 1: " << sh1.GetZOrderPosition() << std::endl;

    // Send this shape to front
    sh1.ToFrontOrBack(2);

    // Print the Z-Order position of the shape
    std::cout << "Z-Order Shape 4: " << sh4.GetZOrderPosition() << std::endl;

    // Send this shape to back
    sh4.ToFrontOrBack(-2);

    // Save the output Excel file
    wb.Save(outDir + u"outputToFrontOrBack.xlsx");

    std::cout << "Shapes reordered successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}