The text displays fine when opening the PSD in Photoshop, but when editing the text layer, both the text and formatting move, displaying incorrectly
Error:
beforeEdit.jpg (62.8 KB)
Editing.jpg (73.6 KB)
File:
Inputv2.zip (1.5 MB)
Code:
void public Main (){
string sourceFilePSDEmail = Directory.GetCurrentDirectory() + "/inputv2.psd";
PsdImage imageTestEmail = (PsdImage)Aspose.PSD.Image.Load(sourceFilePSDEmail);
var layers = imageTestEmail.Layers;
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"))
{
//Step: Replace text
replaceLsakFourthMethod(textDataTL);
System.Console.WriteLine("Replaced text " + textDataTL.Text);
//Step: Format text
formatLsakFourthMethod(textDataTL);
System.Console.WriteLine("Formated text " + textDataTL.Text);
//Step: Center textlayer
if(layerTLToUpdateText.DisplayName.Equals("cc")||layerTLToUpdateText.DisplayName.Equals("tl")||layerTLToUpdateText.DisplayName.Equals("cl")){
//old Values
var boundBoxOld = layerTLToUpdateText.TextBoundBox;
var wordSizeOld = layerTLToUpdateText.Size;
var OldY =layerTLToUpdateText.Top;
textDataTL.UpdateLayerData();
// new values
var wordSize = layerTLToUpdateText.Size;
var boundBox = layerTLToUpdateText.TextBoundBox;
var newSize = new Aspose.PSD.Size((int)Math.Ceiling(boundBoxOld.Width),wordSize.Height);
var beforePoint = CenterInRectangle(wordSize, new Aspose.PSD.RectangleF(layerTLToUpdateText.Left,layerTLToUpdateText.Top,layerTLToUpdateText.Width,boundBoxOld.Height));
layerTLToUpdateText.TextBoundBox = new Aspose.PSD.RectangleF(new Aspose.PSD.PointF(0,beforePoint.Y -OldY), newSize);
var newPoint = CenterInRectangle(wordSize, new Aspose.PSD.RectangleF(layerTLToUpdateText.Left,layerTLToUpdateText.Top,layerTLToUpdateText.Width,boundBoxOld.Height));
layerTLToUpdateText.Left = (int)newPoint.X;
layerTLToUpdateText.Top = (int)newPoint.Y;
textDataTL.UpdateLayerData();
System.Console.WriteLine("Center text");
}
}
}
}
}
outputFile = outputFile+"TestEmailFourthMethod"+".psd";
imageTestEmail.Save(outputFile);
}
Aspose.PSD.PointF CenterInRectangle( Aspose.PSD.Size Inner, Aspose.PSD.RectangleF Outer)
{
return new Aspose.PSD.PointF()
{
X = (Outer.X + (Outer.Width - Inner.Width) / 2),
Y = (Outer.Y + (Outer.Height - Inner.Height) / 2)
};
}
void replaceLsakFourthMethod(IText textData)
{
//validar si tiene tags
Regex tagRegex = new Regex(@"<[^>]+>");
var countPortions = textData.Items.Count();
ITextStyle defaultStyle = textData.Items[0].Style;
ITextParagraph defaultParagraph = textData.Items[0].Paragraph;
var textToReplace= textData.Text;
//Quitar portions antiguos sin formato
for (int i = 0; i < (countPortions); i++)
{
textData.RemovePortion(0);
}
ITextPortion newPortion = textData.ProducePortion();
newPortion.Paragraph.Apply(defaultParagraph);
newPortion.Style.Apply(defaultStyle);
newPortion.Text=ReplaceBarraNBarraRByExelValuesFourthMethodFirst(textToReplace);
textData.AddPortion(newPortion);
textData.UpdateLayerData();
}
void formatLsakFourthMethod (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);
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("br")) {
textSplit[i]= textSplit[i].Replace("br/","//");
};
newPortion.Text=textSplit[i];
bool hasTagsIsaks = false;
hasTagsIsaks = tagRegex.IsMatch(newPortion.Text);
if(hasTagsIsaks){
string delimeters = (@"<[^>]+>");
var partsIsaks = Regex.Split(newPortion.Text, string.Format(@"\s+(?={0})", string.Join("| ", delimeters)));
for (int j = 0; j < partsIsaks.Length; j++)
{
var item = partsIsaks[j];
ITextPortion newPortionFromIsak = textData.ProducePortion();
newPortionFromIsak.Paragraph.Apply(defaultParagraph);
newPortionFromIsak.Style.Apply(defaultStyle);
if (partsIsaks[j].Contains("font:")||partsIsaks[j].Contains("font :")) {
newPortionFromIsak = setFont(newPortionFromIsak, partsIsaks[j]);
};
if (partsIsaks[j].Contains("pt")) {
newPortionFromIsak = setSize(newPortionFromIsak, partsIsaks[j]);
};
if (partsIsaks[j].Contains("color")) {
newPortionFromIsak = setColor(newPortionFromIsak, partsIsaks[j]);
};
newPortionFromIsak.Text = Regex.Replace(partsIsaks[j], "<.*?>", String.Empty);
textData.AddPortion(newPortionFromIsak);
}
}
else{
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","");
textData.AddPortion(newPortion);
}
textData.UpdateLayerData();
}
string ReplaceIsaksByExcelValuesFourthMethod(string lsak){
StringBuilder sb = new StringBuilder (lsak);
sb.Replace("#lsak_011#", "[1] Terms and exclusions apply. Game catalog varies over time, by region, and by device. Requires Windows 11 (with updates); excludes S mode and ARM devices. See <font:Segoe UI Semibold>xbox.com/pcgamepass</font> and <font:Segoe UI Semibold>https://www.ea.com/ea-play/terms</font> and <font:Segoe UI Semibold>https://www.ea.com/ea-play</font> for details. <br/>[2] See https://www.ea.com/ea-play/terms and https://www.ea.com/ea-play for details. The EA logo and Battlefield are trademarks of Electronic Arts Inc. © FIFA is a copyright and/or trademark of FIFA. All rights reserved. Manufactured under license by Electronic Arts Inc. STAR WARS © & TM 2019 Lucasfilm Ltd. All rights reserved. ");
sb.Replace("#lsak_012#", "Privacy Statement");
sb.Replace("#lsak_013#", "Unsubscribe");
sb.Replace("#lsak_014#", "[Retailer. Add in your own Unsubscribe and Privacy Policy links here]");
sb.Replace("#lsak_015#", "Partner Company, Inc.");
sb.Replace("#lsak_016#", "XXX Streetname St.");
sb.Replace("#lsak_017#", "Cityname, State Zipcode");
sb.Replace("br/", "\\");
sb.Replace("\r","");
return sb.ToString();
}
string ReplaceBarraNBarraRByExelValuesFourthMethodFirst(string lsak){
StringBuilder sb = new StringBuilder (lsak);
sb.Replace("#lsak_011#", "[1] Terms and exclusions apply. Game catalog varies over time, by region, and by device. Requires Windows 11 (with updates); excludes S mode and ARM devices. See <font:Segoe UI Semibold>xbox.com/pcgamepass</font> and <font:Segoe UI Semibold>https://www.ea.com/ea-play/terms</font> and <font:Segoe UI Semibold>https://www.ea.com/ea-play</font> for details. <br/>[2] See https://www.ea.com/ea-play/terms and https://www.ea.com/ea-play for details. The EAC logo and Battlefield are trademarks of Electronic Arts Inc. © FIFAC is a copyright and/or trademark of FIFAC. All rights reserved. Manufactured under license by ElectroniX Srts Inc. SRA SAD © & TM 2019 LsdjmLtd. All rights reserved. ");
sb.Replace("#lsak_012#", "Privacy Statement");
sb.Replace("#lsak_013#", "Unsubscribe");
sb.Replace("#lsak_014#", "[Retailer. Add in your own Unsubscribe and Privacy Policy links here]");
sb.Replace("#lsak_015#", "Partner Company, Inc.");
sb.Replace("#lsak_016#", "XXX Streetname St.");
sb.Replace("#lsak_017#", "Cityname, State Zipcode");
sb.Replace("\r","");
return sb.ToString();
}