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