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?
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:
Check if the shape has any existing paragraphs.
If none exist, create a new Para object and add it to the shape.
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
Run the code to verify that the paragraphs are added successfully.
Adjust the text and alignment as necessary.
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.