Problem when aligning to center text after updating in Photoshop

Center alignment of text is lost after updating in PS.
By code, portions are created with center justified text, and the text contained in the textlayer is replaced. The problem occurs after opening in the phoshotp and editing it
Before:
Before.png (93.2 KB)
After:
After.png (72.2 KB)
Code explain:
First, we proceed to replace the text of a text layer according to the content loaded in a text file that is taken as input parameter to the program. After replacing the text, the text layer is centered, if cc, centered in the middle, with the text justification in the center
PSD example:
input-test.zip (1.5 MB)
Input text:
input-text.zip (1.1 KB)

Code:

 public void main(){
	var strings1 = "C:\\input-text.txt";
	var psdInput = "C:\\input-test.psd";
	PsdImage image = (PsdImage)Aspose.PSD.Image.Load(psdInput);  
	IEnumerable<string> linesLsaks = File.ReadAllLines(strings1); 
	var lsaksDictionary = linesLsaks
						.Where(line => !string.IsNullOrWhiteSpace(line))
						.Select(line => line.Split('='))
						.Select(items => new {
							key = items[0].Trim(),
							value    = items[1].Trim()  
						}) 
						.GroupBy(p => p.key)
						.ToDictionary(x => x.First().key, x => x.Last().value);

	//Step: Replace text

	foreach (var layerItem in image.Layers)
	{    
		if (layerItem.GetType() != typeof(TextLayer)) continue; 
		var layerToExtractText = (TextLayer)layerItem;   
		Regex pRegex = new Regex("#lsak[^#]+#", RegexOptions.IgnoreCase); 
		var resultLsakValue = pRegex.Match(layerToExtractText.Text).Value.Trim(); 
		
		if (string.IsNullOrWhiteSpace(resultLsakValue)) continue;

		replaceLsakText(layerToExtractText.TextData, lsaksDictionary);           
	}    
	//Step: Center text
	foreach (var layerItem in image.Layers)
	{    
		if (layerItem.GetType() != typeof(TextLayer)) continue; 
		var layerToExtractText = (TextLayer)layerItem;     
		
		centerTextLayers(layerToExtractText);   
	}    
	image.Save("c:\\output.psd"); 
	
}
private  string ReplaceText(string lsak, IDictionary<string,string> lsaksToReplace){  
	StringBuilder sb = new StringBuilder (lsak.Trim());
	foreach (var kvp in lsaksToReplace){ 
		sb.Replace(kvp.Key, kvp.Value); 
	}
	sb.Replace("\r","");
	sb.Replace("#","");
	return sb.ToString(); 
}
private void replaceLsakText(IText textData, IDictionary<string,string> lsaksDictionary)
{
    var countPortions = textData.Items.Count(); 
    ITextStyle defaultStyle = textData.Items[0].Style; 
    ITextParagraph defaultParagraph = textData.Items[0].Paragraph;   
    var textToReplace= textData.Text;
    //remove old portions 
    for (int i = 0; i < (countPortions); i++)
    {
        textData.RemovePortion(0); 
    } 
    ITextPortion newPortion = textData.ProducePortion();  
    newPortion.Paragraph.Apply(defaultParagraph);
    newPortion.Style.Apply(defaultStyle); 
    newPortion.Text=ReplaceText(textToReplace, lsaksDictionary); 
    textData.AddPortion(newPortion);  
    textData.UpdateLayerData();
} 
 private void centerTextLayers(TextLayer layerToAlignText)
{
    var boundBoxOld = layerToAlignText.TextBoundBox;
    var OldY = layerToAlignText.Top;
    layerToAlignText.TextData.UpdateLayerData();   
    var wordSize = layerToAlignText.Size; 
    var boundBox = layerToAlignText.TextBoundBox;
    var newSize = new Aspose.PSD.Size((int)Math.Ceiling(boundBoxOld.Width),wordSize.Height);

     if(layerToAlignText.DisplayName.Trim().ToLower().Equals("cc")){ 

        var beforePoint = CenterInRectangle(wordSize, new Aspose.PSD.RectangleF(layerToAlignText.Left,layerToAlignText.Top,layerToAlignText.Width,boundBoxOld.Height));
        layerToAlignText.TextBoundBox = new Aspose.PSD.RectangleF(new Aspose.PSD.PointF(0,beforePoint.Y -OldY), newSize); 
        
        var newPoint = CenterInRectangle(wordSize, new Aspose.PSD.RectangleF(layerToAlignText.Left,layerToAlignText.Top,layerToAlignText.Width,boundBoxOld.Height));
        layerToAlignText.Left =  (int)newPoint.X;
        layerToAlignText.Top = (int)newPoint.Y;  

        layerToAlignText.TextData.Items.ToList().ForEach(i => i.Paragraph.Justification = JustificationMode.Center);  
         
        layerToAlignText.TextData.UpdateLayerData();    
    } 
}

}

@cristianortegaethofy
I ran the code that you provided, except missed method ‘CenterInRectangle’, on 23.4 v. and the result was successful. In the output PSD file, TextLayer has Center justification before and after opening in edit mode.

Please check the attached output file:
output-test 23.4.psd.zip (1.7 MB)

Maybe the problem is in some other code part, or we need some specific version of Photoshop.
What version of Photoshop do you use?

thanks for answerme, yes, a part of the code is missing and from what you can see it is where the problem comes from because the problem occurs when adding this function and also missing method ‘CenterInRectangle’. What this function called formatStyleText does is subdivide the text to create new portions with text that have different font styles, text sizes, etc.
the complete code is:
code.zip (1.7 KB)

@cristianortegaethofy
Thanks for the new code, now I see the issue and created a task for resolving it.
You will be notified when it’s done.

@cristianortegaethofy
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PSDNET-1546

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

The issues you have found earlier (filed as PSDNET-1546) have been fixed in this update. This message was posted using Bugs notification tool by yaroslav.lisovskyi