We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Align Text to left/right/center/justify

Hi,

Is there any way I could choose the alignment type of my text?

Thank you

@dragos.petrescu,
There is no direct way to set the horizontal alignment of the shape’s text. However, you can formulate TxtPinX and TxtLocPinX values. Please refer to code snippet below:

[C#]

Diagram diagram = new Diagram(@"C:\Diagram\test2164\Drawing1.vsdx");
Aspose.Diagram.Shape shape = diagram.Pages[0].Shapes.GetShape(1);
// set vertical alignment
shape.TextBlock.VerticalAlign.Value = VerticalAlignValue.Top;
// formulate horizontal alignment
shape.TextXForm.TxtPinX.Ufe.F = "Width*0"; 
shape.TextXForm.TxtLocPinX.Ufe.F = "TxtWidth*0.3";

Best Regards,
Imran Rafique

Hi,

I am sorry but I think that you might have misinterpreted my question. I didn’t mean align to horizontally or vertical alignment, but the alignment of text from Visio, that can be left, center, right and justify.The text box has to be unmoved, but rather the text itself has to be aligned. This Visio setting can be found in Home -> Paragraph -> General -> Alignment

I attached a picture of the expected result and a the Visio diagram.

AlignFeature.zip (112.4 KB)

@dragos.petrescu,
Thank you for the details. You can align text paragraphs of the shape as below:

[C#]

// load a Visio drawing
Diagram diagram = new Diagram(@"C:\Diagram\test2170\AlignFeature.vsdx");
// get page by name
Aspose.Diagram.Page page = diagram.Pages.GetPage("Page-1");
// get shape by ID
Aspose.Diagram.Shape shape = page.Shapes.GetShape(1);
// get paragraph by index
shape.Paras[0].HorzAlign.Value = HorzAlignValue.RightAlign;
// save Visio drawing
diagram.Save(@"C:\Diagram\test2170\AlignFeature_Out.vsdx",  SaveFileFormat.VSDX); 

This is the output VSDX drawing: AlignFeature_Out_VSDX.zip (101.5 KB)
Kindly let us know in case of any further assistance or questions.

Best Regards,
Imran Rafique

Hi,

Your example works perfectly for a single Char text but when I add two Chars of text, the whole text overlaps.
It behaves the same no matter if I try aligning it to left, center or right…

I attached my code snippet, the result I got and what I expected.
AlignMultilineTextBug.zip (117.9 KB)

@dragos.petrescu,
We managed to replicate the problem of not being able to set a HAlign property of shape. It has been logged under the ticket ID DIAGRAMJAVA-50539 in our bug tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates. We are sorry for the inconvenience caused.

Best Regards,
Imran Rafique

@dragos.petrescu,
In reference to the ticket ID DIAGRAMJAVA-50539, please try the following code:

[Java]

// and add in the new diagram 
String visioStencil = "C:\\Diagram\\test2180\\Basic Shapes.vss"; 
// Names of the masters present in the stencil 
String rectangleMaster = "Rectangle"; 
String connectorMaster = "Dynamic connector"; 
int pageNumber = 0; 
double width = 4, height = 3.0; 
double pinX = 5, pinY = 6; 
// Create a new diagram 
Diagram diagram = new Diagram(visioStencil); 
long rectangleId = diagram.addShape(pinX, pinY, width, height, rectangleMaster, pageNumber); 
Shape shape = diagram.getPages().getPage(pageNumber).getShapes().getShape(rectangleId); 
shape.getText().getValue().clear(); 
shape.getChars().clear(); 

shape.getParas().add(new Para()); 
shape.getParas().getPara(0).getHorzAlign().setValue(HorzAlignValue.LEFT_ALIGN); 
//set index for para
shape.getParas().getPara(0).setIX(0);
// mark character run and add text 
shape.getText().getValue().add(new Cp(0)); 
shape.getText().getValue().add(new Txt("All this\n")); 
shape.getText().getValue().add(new Cp(1)); 
shape.getText().getValue().add(new Txt("text has to\n")); 
shape.getText().getValue().add(new Cp(2)); 
shape.getText().getValue().add(new Txt("be perfectly aligned\n")); 
shape.getText().getValue().add(new Cp(3)); 
shape.getText().getValue().add(new Txt("in the left side\n")); 

// add formatting characters 
shape.getChars().add(new Char()); 
shape.getChars().add(new Char()); 
shape.getChars().add(new Char()); 
shape.getChars().add(new Char()); 

//set properties e.g. color, font, size and style etc. 
shape.getChars().get(0).setIX(0); 
//set properties e.g. color, font, size and style etc. 
shape.getChars().get(1).setIX(0); 
//set properties e.g. color, font, size and style etc. 
shape.getChars().get(2).setIX(0); 
//set properties e.g. color, font, size and style etc. 
shape.getChars().get(3).setIX(0); 

diagram.save("C:\\Diagram\\test2180\\AlignBugMultiChar.vsdx", SaveFileFormat.VSDX);

This is the output VSDX: AlignBugMultiCharVSDX.zip (76.4 KB)

PS: The ticket ID DIAGRAMJAVA-50539 has been closed.

Best Regards,
Imran Rafique