The text displays fine when opening the PSD in Photoshop, but when editing the text layer, the formatting move, displaying incorrectly
- Opening PS Before of edition
beforeUpdate(1).jpg (74.9 KB) - After of edition
Editing(1).jpg (148.8 KB)
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:
- Replace the texts.
Removing the portions and add a new one with the new text, second,
- Give it the format according to the html tags.
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 this point when formatting the text, when editing in PS.
Input Files:
Text file:
input-lsaks-strings1 - forTicket.zip (404 Bytes)
Input PSD:
input_for_ticket.zip (1.5 MB)
Code:
public void main(){
var strings1 = "C:\\input-lsaks-strings1 - forTicket.txt";
var psdInput = "C:\\input_for_ticket.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: Format and Center
foreach (var layerItem in image.Layers)
{
if (layerItem.GetType() != typeof(TextLayer)) continue;
var layerToExtractText = (TextLayer)layerItem;
formatStyleText(layerToExtractText.TextData, lsaksDictionary);
}
image.Save("c:\\output.psd");
}
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();
}
void formatStyleText (IText textData, IDictionary<string,string> lsaksToReplace){
//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 tagRegex1 = @"[^<>]+|<\s*([^ >]+)[^>]*>.*?<\s*/\s*\1\s*>";
var matchesImgSrc = Regex.Matches(textData.Text, tagRegex1, 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);
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]);
};
if (textSplit[i].Contains("<sup")) {
newPortion = setSupText(newPortion, textSplit[i]);
};
if (textSplit[i].Contains("br/")) {
// textSplit[i] = "//";
textSplit[i] = "\r";
};
// newPortion.Text = Regex.Replace(textSplit[i], "<.*?>", String.Empty).Replace("br/","//");
newPortion.Text = Regex.Replace(textSplit[i], "<.*?>", String.Empty);
textData.AddPortion(newPortion);
}
textData.UpdateLayerData();
}
}