Set Margins of Comment or Shape inside the Worksheet with C++

Possible Usage Scenarios

Aspose.Cells allows you to set the margins of any shape or comment using the Shape.GetTextAlignment() property. This property returns the object of Aspose.Cells.Drawing.Texts.ShapeTextAlignment class which has different properties e.g. GetTopMarginPt()GetLeftMarginPt(), GetBottomMarginPt(), GetRightMarginPt(), etc. that can be used to set the top, left, bottom, and right margins.

Set Margins of Comment or Shape inside the Worksheet

Please see the following sample code. It loads the sample Excel file that contains two shapes. The code accesses the shapes one by one and sets their top, left, bottom, and right margins. Please see the output Excel file generated by the code and a screenshot showing the effect of the code on the output Excel file.

todo:image_alt_text

Sample Code

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

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

    // For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C

    // Load the sample Excel file
    Workbook workbook(u"sampleSetMarginsOfCommentOrShapeInsideTheWorksheet.xlsx");

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

    // Iterate through each shape in the worksheet
    for (int32_t i = 0; i < ws.GetShapes().GetCount(); i++)
    {
        Shape sh = ws.GetShapes().Get(i);

        // Access the text alignment
        ShapeTextAlignment txtAlign = sh.GetTextBody().GetTextAlignment();

        // Set auto margin false
        txtAlign.SetIsAutoMargin(false);

        // Set the top, left, bottom and right margins
        txtAlign.SetTopMarginPt(10);
        txtAlign.SetLeftMarginPt(10);
        txtAlign.SetBottomMarginPt(10);
        txtAlign.SetRightMarginPt(10);
    }

    // Save the output Excel file
    workbook.Save(u"outputSetMarginsOfCommentOrShapeInsideTheWorksheet.xlsx");

    std::cout << "Margins set successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}