Excel shape text font color is not getting modified correctly

I am trying to change the text font of shape in excel document from white to black, it is only changing the first word’s color to black. Please find the attached solution for reproducing this and also suggest how can we change the font color of entire sentence within shape.

shapeFont.zip (14.7 KB)

@nraj

To change the font color of the entire text within a shape in an Excel document using Aspose.Cells, you can utilize the TextBody.Format method. This method allows you to specify the range of text you want to format, along with the desired font settings.

Here’s a sample code snippet that demonstrates how to change the font color of all text within a shape:

using System;
using Aspose.Cells;
using Aspose.Cells.Drawing;
using System.Drawing;

namespace Aspose.Cells.Examples
{
    public class ChangeShapeFontColor
    {
        public static void Run()
        {
            // Instantiate a new Workbook
            Workbook workbook = new Workbook();
            
            // Get the first worksheet
            Worksheet worksheet = workbook.Worksheets[0];

            // Add a shape (e.g., a comment) to a cell
            var comment = worksheet.Comments[worksheet.Comments.Add("A1")];
            comment.Note = "This is my Comment Text. This is Test.";
            Shape shape = comment.CommentShape;

            // Set the shape's fill color
            shape.Fill.SolidFill.Color = Color.Black;

            // Create a font object and set the desired color
            Font font = shape.Font;
            font.Color = Color.Black; // Change to your desired color

            // Create a StyleFlag to specify which properties to apply
            StyleFlag styleFlag = new StyleFlag();
            styleFlag.FontColor = true;

            // Format the entire text in the shape
            shape.TextBody.Format(0, shape.Text.Length, font, styleFlag);

            // Save the Excel file
            workbook.Save("outputChangeShapeFontColor.xlsx");

            Console.WriteLine("Font color changed successfully.");
        }
    }
}

In this example, the TextBody.Format method is called with parameters that specify the start and end indices of the text to format, which in this case covers the entire text length of the shape. This ensures that the font color change applies to all words in the shape, not just the first one.

Make sure to adjust the color as needed for your specific requirements. This approach should resolve the issue of only the first word’s color being changed.

If you have any further questions or need additional assistance, feel free to ask!

@nraj
By examining the file, we found that the text in the shape is rich text. So when setting the font color, it is necessary to set the font color of all segmented parts in Shape.TextBody to black. Please refer to the following example code.

string inputPath = filePath + "input4.xlsx";
string outputPath = filePath + "output3.xlsx";

Workbook workbook = new Workbook(inputPath);
Worksheet sheet = workbook.Worksheets[0];

for (int i = sheet.Shapes.Count - 1; i >= 0; i--)
{
    Shape shape = sheet.Shapes[i];

    if (sheet.Shapes.Count > 0)
    {
        if (shape.Fill != null)
        {
            shape.Fill.FillType = FillType.Solid;
            shape.Fill.SolidFill.Color = Color.White; // White fill
        }
        if (shape.Line != null)
        {
            shape.Line.FillType = FillType.Solid;
            shape.Line.SolidFill.Color = Color.Black; // Black border
        }
        if (!string.IsNullOrEmpty(shape.Text))
        {
            shape.Font.Color = Color.Black; // Black text
            shape.TextOptions.Color = Color.Black;
            int count = shape.TextBody.Count;
            for (int s = 0; s < count; s++)
            {
                shape.TextBody[s].Font.Color = Color.Black;
            }
        }
    }
}

workbook.Save(outputPath);
Console.WriteLine($"✅ Not able to set font color to black.");

Hope helps a bit.

Which one is better in the given solutions

  1.   shape.Font.Color = Color.Black; // Black text
         shape.TextOptions.Color = Color.Black;
         int count = shape.TextBody.Count;
         for (int s = 0; s < count; s++)
         {
             shape.TextBody[s].Font.Color = Color.Black;
         }
    
  2.    Aspose.Cells.Font font = shape.Font;
        font.Color = Color.Black; // Change to your desired color
        styleFlag.FontColor = true;
    
       // Format the entire text in the shape
      shape.TextBody.Format(0, shape.Text.Length, font, styleFlag);
    

in approach “2” will it impact font style of other components as well like cells as we are using the below line of code as well

StyleFlag styleFlag = new StyleFlag { All = true };

// Apply the style to the used range of the worksheet
Aspose.Cells.Range usedRange = sheet.Cells.MaxDisplayRange;
sheet.Cells.ApplyStyle(blackAndWhiteStyle, styleFlag);

will it be applied with shape’s font style to cells?

Suggest me the best way as both worked.

Thanks in advance.

@nraj
Thank you for your feedback. The first solution is better. The second solution may override other styles settings in the rich text. The optimization direction of shape text is to unify shape text into rich text. Using the first solution is also beneficial for upgrading your subsequent versions. You can upgrade the version directly without modifying the codes.

Thanks for your quick and helpful response.

@nraj
You are welcome. If you have any questions, please feel free to contact us at any time.