Replacing text in a textlayer, formatting is wrong when editing in PS

What I am doing is replacing texts that have styles or formats. These formats can come in the text of the texlayer or in the text that you want to replace with html tags. For example < font: Arial, color:red>text_1</>, text_1 should be replaced with the font arial and with red color, and this text text_1 could also have a < font-size:23pt> Hello text replaced</ >, which should apply a fontsize of 23.
The solution that I am developing is:
1- Replace the texts.
2. Give it the format according to the html tags.
So first, I do a replace removing the portions and add a new one with the new text, second, if the text has html tags, I do a split for each tag and create a portion for each string giving the format.
The problem arises in point 2 when formatting the text, when editing in PS, the second paragraph takes the style of the first.
“Power Play” has a size of 72 pt and has as its font Segoe SemiBold and the rest of the text a size of 48 pt and has as its font Segoe Regular:

beforeEdit.jpg (145.2 KB)

But when the psd is opened with PS, all the text takes the format of the first paragraph:

Editing.jpg (96.3 KB)

Wrongly all text has a size of 72 pt and has as its font Segoe SemiBold. What is the problem? Do you think I can do it another way?

Original PSD:
originalv5.zip (1.6 MB)

Code:

      void Test_Email_fifthMethod(){

       

        string sourceFilePSDEmail = Directory.GetCurrentDirectory() + "/files-input/inputv5.psd";    

        PsdImage imageTestEmail = (PsdImage)Aspose.PSD.Image.Load(sourceFilePSDEmail);

        var layers = imageTestEmail.Layers;

        var layersOnlyTextLayers = imageTestEmail.Layers.Where(x=>x.GetType()== typeof(TextLayer));
          
        foreach (var layerItem in layers)

        { 

            var isTextLayer = layerItem.GetType() == typeof(TextLayer)?true:false;

            if (isTextLayer)

            {  

                var layerTLToUpdateText = (TextLayer)layerItem;  

                //Buscar lsak text

                if (layerTLToUpdateText.Text.Contains("lsak"))  

                {  

                    var textDataTL = layerTLToUpdateText.TextData;     

                    if(textDataTL.Text.Contains("lsak") && textDataTL.Text.Contains("\r"))  

                    {

                         //Replace text

                        replaceLsakText(textDataTL);       

                        //Format text

                        formatStyleText(textDataTL); 

                    }    

                }

            }  

        }  

        outputFile = Directory.GetCurrentDirectory() +"TestEmailFifthMethod"+".psd";

        imageTestEmail.Save(outputFile); 

    }

       string ReplaceText(string lsak){  

        StringBuilder sb = new StringBuilder (lsak);


        sb.Replace("#lsak_007#", "Power play.");

        sb.Replace("#lsak_008#", "Πιο επεκτατικοί κόσμοι. Γρήγοροι χρόνοι φόρτωσης. Υψηλά ποσοστά καρέ και ευρύτερο φάσμα χρωμάτων. Ένας υπολογιστής με Windows ήταν πάντα η καλύτερη πλατφόρμα για παιχνίδια—και στα Windows 11, γίνεται ακόμα καλύτερος.");

        sb.Replace("#lsak_009#", "Contoso.com で新しい Windows 11 PC を見つけてください。");

        sb.Replace("#lsak_010#", "Buy now"); 

        sb.Replace("#lsak_011#", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, <font:Segoe UI Semibold> quis nostrud exercitation ullamco</font> laboris <font:Segoe UI Semibold>nisi ut aliquip</font> ex ea commodo consequat. <font:Segoe UI Semibold>Duis aute irure dolor in reprehenderit </font>in voluptate velit esse cillum dolore eu fugiat nulla pariatur.  <br/> Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");

        sb.Replace("\r","");

        return sb.ToString();

    }

    void replaceLsakText(IText textData)

    {    

        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);

        textData.AddPortion(newPortion);  

        textData.UpdateLayerData();

    }  

    void formatStyleText (IText textData){

    //validar si tiene tags

    Regex tagRegex = new Regex(@"<[^>]+>");

    bool hasTags = tagRegex.IsMatch(textData.Text);

    var countPortions = textData.Items.Count();

    ITextStyle defaultStyle = textData.Items[0].Style;

    ITextParagraph defaultParagraph = textData.Items[0].Paragraph;  

    if(hasTags){

        var tagRegex2 = @"[^<>]+|<\s*([^ >]+)[^>]*>.*?<\s*/\s*\1\s*>";

        var matchesImgSrc = Regex.Matches(textData.Text, tagRegex2, RegexOptions.IgnoreCase | RegexOptions.Singleline);

        var listlsaks = new List<string>();

        foreach (Match m in matchesImgSrc)

            {listlsaks.Add(m.Value);}

        string[] textSplit  = listlsaks.ToArray();

        for (int i = 0; i < (countPortions); i++)

        {

            textData.RemovePortion(0);

        }

        for (int i = 0; i < textSplit.Length; i++)

        {

            ITextPortion newPortion = textData.ProducePortion();  

            newPortion.Paragraph.Apply(defaultParagraph);

            newPortion.Style.Apply(defaultStyle);

            bool hasTagsIsaks = false;

            hasTagsIsaks = tagRegex.IsMatch(textSplit[i]);

            if(hasTagsIsaks){

                if (textSplit[i].Contains("font:")) {

                    newPortion = setFont(newPortion, textSplit[i]);

                };

                if (textSplit[i].Contains("pt")) {

                    newPortion = setSize(newPortion, textSplit[i]);

                };

                if (textSplit[i].Contains("color")) {

                    newPortion = setColor(newPortion, textSplit[i]);

                };  

                newPortion.Text = Regex.Replace(textSplit[i], "<.*?>", String.Empty).Replace("br/","//");

                textData.AddPortion(newPortion);  

            }

            else{

                newPortion.Text = Regex.Replace(textSplit[i], "<.*?>", String.Empty).Replace("br/","//");  

                textData.AddPortion(newPortion);

            }

        }  

       

    }else{

        var textToReplace= textData.Text;

        for (int i = 0; i < (countPortions); i++)

        {

            textData.RemovePortion(0);

        }

        ITextPortion newPortion = textData.ProducePortion();  

        newPortion.Paragraph.Apply(defaultParagraph);

        newPortion.Style.Apply(defaultStyle);

        newPortion.Text=ReplaceIsaksByExcelValuesFourthMethod(textToReplace).Replace("\r","").Replace("br/","//");

        textData.AddPortion(newPortion);  

    }

    textData.UpdateLayerData();

}

@cortega1234
It looks like this a bug of saving Text Layer Data.

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-1448

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.

Thanks for answering me, but we still have the same problem, any news?

@cortega1234 this issue is fixed also. The fix will be released in 23.3.

Is the problem resolved? I don’t find ticket PSDNET-1448 in release notes of 23.3 published today.

@cortega1234 the team fixed the reason of the issue that looked similar to this, but the solution wasn’t tested for this task. We’ll test it and then I’ll write you back.

Thank you, I have already checked it and the bug continues, I await the result of your test

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