The text is shown first in one line but after updating in photoshop, a word goes to a second line.
Before replacing the text:
BeforeoReplaceText.png (27.4 KB)
Before updating:
BeforeUpdateInPS.png (60.3 KB)
After updating:
AfterUpdateInPS.png (4.6 KB)
I need to know or how I could do so that the text is displayed with the 2 lines from the beginning without updating because I need the size, width and height of the text box to center it.
What I do is delete the portion and create a new one with the text, I take the width and height to center it but since after updating in Photoshop the text has 2 lines it is cut off because by code I took the height of 1 single line.
Code explain:
What I am doing is replacing the text of a textlayer according to the content loaded in a text file that is taken as input parameter to the program. After replacing the text, the textlayer is centered, if it is cl, centered to the left, in this process I obtain the height to center it but when updating in PS the word remains in 2 lines, being cut off, only one line appears as it looked like before updating in PS
PSD example:
Psd_lsakv2.zip (1.5 MB)
Input text:
input_text.7z (245 Bytes)
Code:
public void main(){
var strings1 = "C:\\input_text.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: 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("cl")){
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.UpdateLayerData();
}
}
}