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