Manage Shape Text Options with C++

Managing Shape Text Options

Aspose.Cells allows you to customize the text within shapes in Excel files. The Shape class provides methods and properties to manage text options such as alignment, orientation, and formatting.

Setting Text Alignment

You can set the horizontal and vertical alignment of text within a shape using the GetTextHorizontalAlignment() and GetTextVerticalAlignment() properties.

#include <Aspose.Cells.h>

using namespace Aspose::Cells;
using namespace Aspose::Cells::Drawing;

void SetTextAlignment() {
    // Load the Excel file
    Workbook workbook("example.xlsx");
    Worksheet worksheet = workbook.GetWorksheets().Get(0);

    // Access the shape
    Shape shape = worksheet.GetShapes().Get(0);

    // Set text alignment
    shape.SetTextHorizontalAlignment(TextAlignmentType::Center);
    shape.SetTextVerticalAlignment(TextAlignmentType::Center);

    // Save the workbook
    workbook.Save("output.xlsx");
}

Setting Text Orientation

You can also set the orientation of the text within a shape using the TextOrientationType property.

#include <Aspose.Cells.h>

using namespace Aspose::Cells;
using namespace Aspose::Cells::Drawing;

void SetTextOrientation() {
    Workbook workbook("example.xlsx");
    Worksheet worksheet = workbook.GetWorksheets().Get(0);

    TextBox textbox = worksheet.GetTextBoxes().Get(0);
    textbox.SetTextOrientationType(TextOrientationType::ClockWise);

    workbook.Save("output.xlsx");
}

Formatting Text

You can format the text within a shape using the Font class. This allows you to set properties such as font size, color, and style.

#include <Aspose.Cells.h>

using namespace Aspose::Cells;
using namespace Aspose::Cells::Drawing;

void FormatText() {
    // Load the Excel file
    Workbook workbook("example.xlsx");
    Worksheet worksheet = workbook.GetWorksheets().Get(0);

    // Access the shape
    Shape shape = worksheet.GetShapes().Get(0);

    // Access the font of the shape's text
    Font font = shape.GetTextBody().GetParagraphEnumerator().GetCurrent().GetFont();

    // Set font properties
    font.SetSize(14);
    font.SetColor(Color::Red());
    font.SetIsBold(true);

    // Save the workbook
    workbook.Save("output.xlsx");
}

Conclusion

Aspose.Cells for C++ provides a comprehensive set of tools to manage shape text options in Excel files. By using the Shape class, you can easily customize text alignment, orientation, and formatting to meet your specific requirements.