How to get horizontal alignment

Hi,
to get and set the horizontal alignment of a shapes text i have to use the Para class. That’s what i read at following page: Working with Shapes Paragraph

But if i do so, all of my shapes have allways an empty Para array. I’ve attatched a sample visio file.

This is the code which tells me that the Para array is empty:

Diagram diagram = new Diagram("c://TextCharStyle.vsdx");
            Aspose.Diagram.Page page = diagram.Pages[0];

            foreach (Aspose.Diagram.Shape shape in page.Shapes)
            {
                if (shape != null)
                {
                    Console.WriteLine($"Shape name: {shape.Name} -> Paras: {shape.Paras.Count} ");
                }
            }

Do you have an idea how to get and set the horizontal alignment of a shape?

regards
Steve
TextCharStyle.zip (12,2 KB)

@sgruth

Cause

The shapes in your Visio file do not contain any paragraphs, which is why the Paras array is empty. This can occur if the shapes are not configured to have text or if the text is not formatted as paragraphs.

Solution

To set the horizontal alignment of a shape’s text, you must first ensure that the shape contains paragraphs. If it doesn’t, you will need to create a new paragraph and add it to the shape. Follow these steps:

  1. Check if the shape has any existing paragraphs.
  2. If none exist, create a new Para object and add it to the shape.
  3. Set the horizontal alignment using the HorzAlign property of the Para class.

Code

Here’s a sample code snippet that demonstrates how to add a paragraph to a shape and set its horizontal alignment:

using Aspose.Diagram;

Diagram diagram = new Diagram("c://TextCharStyle.vsdx");
Aspose.Diagram.Page page = diagram.Pages[0];

foreach (Aspose.Diagram.Shape shape in page.Shapes)
{
    if (shape != null)
    {
        Console.WriteLine($"Shape name: {shape.Name} -> Paras: {shape.Paras.Count} ");
        
        // Check if there are no paragraphs
        if (shape.Paras.Count == 0)
        {
            // Create a new paragraph
            Para newPara = new Para();
            newPara.Text = "Your text here"; // Set your desired text
            newPara.HorzAlign = Aspose.Diagram.Para.HorzAlign.Center; // Set horizontal alignment

            // Add the new paragraph to the shape
            shape.Paras.Add(newPara);
        }
    }
}

// Save the diagram if needed
diagram.Save("c://UpdatedTextCharStyle.vsdx", SaveFileFormat.VSDX);

Next Steps

  1. Run the code to verify that the paragraphs are added successfully.
  2. Adjust the text and alignment as necessary.
  3. Save the updated diagram and review the results in your Visio file.

If you have any further questions or need additional assistance, please let me know!

Paragraph is available in the shape sheet data. See attached screenshot. And as you can see there, horizontal alignment is also set there.
By using Microsoft.Office.Interop.Visio.Shape i can get the value of the horizontal allignment.

But how to get this by using Aspose.Diagram?

20250703_HorizontalAlignment.JPG (81,1 KB)

@sgruth
Thanks for the template file and screenshots.

Please try this sample code :

Console.WriteLine($"Shape name: {shape.Name} -> Paras: {shape.InheritParas.Count} ");

HorzAlignValue horzValue = shape.InheritParas[0].HorzAlign.Value;

Many thanks for your tip. It works. Unfortunately, this was described quite differently in your documentation.
Working with Shapes Paragraph

@sgruth
Thank you for your suggestions.
We will update the relevant documentation accordingly.