Set fontsize by InheritChars

Hello,
i can set the Fontsize of a shape by using:

shape.Chars[0].Size.Value = 0.125

This is well described in the examples: ApplyFontOnText

Now I have the case that the shape’s chars array is empty.
But there are InheritChars. And I can also read the fontsize from InheritChars.

But how to change the fontsize by using InheritChars?
At first i’d tried to set value directly.

shape.InheritChars[0].Size.Value = 0.125

But the fontsize was not changed. The original value was still there.
Then I seem to remember that you can’t set the inherit values directly in the shape. Instead, you have to do this in the original shape. For example, in the master shape.

My problem now is that my shape has no MasterShape and no ParentShape (see attached screenshot).

How can I find the shape where I have to set the fontsize?
20250717_AsposeInheritChars.JPG (20,1 KB)

@sgruth

Cause

You are trying to set the font size of a shape’s text using the InheritChars property, but you are encountering issues because the shape does not have a master shape or parent shape. The InheritChars property is designed to reflect the inherited formatting from a master shape, and direct modifications to it may not yield the expected results.

Solution

To change the font size using InheritChars, you need to modify the original shape from which the current shape inherits its properties. Since your shape lacks a master or parent shape, you will need to create a new master shape or find an existing one that can be used as a template.

  1. Create or Identify a Master Shape: If you don’t have a master shape, create one that has the desired font size settings.
  2. Assign the Master Shape: Assign this master shape to your current shape.
  3. Modify the Font Size: Once the master shape is assigned, you can modify the font size in the InheritChars of the master shape.

Code Example

Here’s a code snippet to illustrate how to set the font size using a master shape:

// Load the diagram
Diagram diagram = new Diagram("path_to_your_diagram.vsdx");

// Create a new master shape if needed
Master masterShape = new Master();
masterShape.setName("CustomMaster");
masterShape.getInheritChars().add(new Char()); // Add a character to inherit
masterShape.getInheritChars().get(0).getSize().setValue(0.125); // Set desired font size

// Add the master shape to the diagram
diagram.getMasters().add(masterShape);

// Find your shape and assign the master shape
Shape shape = diagram.getPages().get(0).getShapes().getShape(1); // Adjust the index as needed
shape.setMaster(masterShape);

// Save the diagram
diagram.save("path_to_output.vsdx", SaveFileFormat.VSDX);

Next Steps

  1. Ensure that you have a master shape defined with the desired font settings.
  2. Assign this master shape to your current shape before modifying the font size.
  3. Test the changes by saving and opening the diagram to verify that the font size is applied correctly.

If you follow these steps, you should be able to successfully set the font size for your shape. Please let me know if you need further assistance!

Please suggest a solution that can also be implemented with the Aspose.Diagram API. The methods of the Master used by the AI do not exist.

In the meantime, I have found a workaround that works.

DoubleValue textSizeInPt = new DoubleValue(9, MeasureConst.PT);
if (_Shape_Aspose.Chars.Count == 0 && _Shape_Aspose.InheritChars.Count > 0)
{
	_Shape_Aspose.Chars.Add(_Shape_Aspose.InheritChars[0]);
	_Shape_Aspose.Chars[0].Size = textSizeInPt;
}

Of course it is very inconvenient to have to go this way every time just to change a FontSize. Especially as there are also various other InheritValues that you would have to change in the same way if there is no MasterShape or similar.

But maybe you have a more elegant solution.

@sgruth
Please try this sample code to add a new char to shape then set the size:

        rectangle.Chars.Add(new Aspose.Diagram.Char());
        rectangle.Chars[0].IX = 0;
        rectangle.Chars[0].Color.Value = "#FF0000";
        rectangle.Chars[0].Size.Value = 0.16667;

Thanks.