العمل مع طبقات النص
إضافة طبقة نص
يتيح لك Aspose.PSD لـ .NET إضافة طبقة نص في ملف PSD. تكوين خطوات إضافة طبقة نص في ملف PSD بسيط كما يلي:
- قم بتحميل ملف PSD كصورة باستخدام الطريقة الثابتة Load التي يتم الكشف عنها بواسطة فئة Image class
- اتصل بالأسلوب AddTextLayer الذي يقبل نصًا ومثيلًا من فئة Rectangle
- احفظ النتائج
يوضح مقطع الكود التالي كيفية إضافة طبقة نص في ملف PSD
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string outFileName = dataDir + "OneLayerWithAddedText.psd"; | |
using (PsdImage image = (PsdImage)Image.Load(sourceFileName)) | |
{ | |
image.AddTextLayer("Some text", new Rectangle(50, 50, 100, 100)); | |
PsdOptions options = new PsdOptions(image); | |
image.Save(outFileName, options); | |
} |
تعيين موقع الطبقة النصية
يتيح لك Aspose.PSD لـ .NET تحديد موقع طبقة نص في ملف PSD. تكوين خطوات تحديد موقع طبقة النص في ملف PSD بسيط كما يلي:
- قم بتحميل ملف PSD كصورة باستخدام الطريقة الثابتة Load التي تم الكشف عنها بواسطة فئة Image
- اتصل بالأسلوب AddTextLayer مع تحديد مواقع اليسار والأعلى لطبقة النص
- احفظ النتائج
يوضح مقطع الكود التالي كيفية تعيين موقع طبقة النص في ملف PSD
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string exportPath = dataDir + "OneLayer_Edited.psd"; | |
int leftPos = 99; | |
int topPos = 47; | |
var im = (PsdImage)Image.Load(sourceFileName); | |
using (im) | |
{ | |
im.AddTextLayer("Some text", new Rectangle(leftPos, topPos, 99, 47)); | |
TextLayer textLayer = (TextLayer)im.Layers[1]; | |
if (textLayer.Left != leftPos || textLayer.Top != topPos) | |
{ | |
throw new Exception("Was created incorrect Text Layer"); | |
} | |
im.Save(exportPath); | |
} |
الحصول على خصائص النص من طبقة نص
مع استخدام Aspose.PSD لـ .NET ، يمكنك الحصول على وتحديث خصائص النصوص المختلفة المتاحة في طبقة نص PSD. يوضح هذا المقال كيفية الحصول على فقرات وأنماط النص وخصائص النص من الطبقة النصية وتحديثها.
يوضح مقطع الكود أدناه كيفية تحقيق هذه المتطلبات.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
const double Tolerance = 0.0001; | |
var filePath = "ThreeColorsParagraphs.psd"; | |
var outputPath = "ThreeColorsParagraph_out.psd"; | |
using (var im = (PsdImage)Image.Load(filePath)) | |
{ | |
for (int i = 0; i < im.Layers.Length; i++) | |
{ | |
var layer = im.Layers[i] as TextLayer; | |
if (layer != null) | |
{ | |
var portions = layer.TextData.Items; | |
if (portions.Length != 4) | |
{ | |
throw new Exception(); | |
} | |
// Checking text of every portion | |
if (portions[0].Text != "Old " || | |
portions[1].Text != "color" || | |
portions[2].Text != " text\r" || | |
portions[3].Text != "Second paragraph\r") | |
{ | |
throw new Exception(); | |
} | |
// Checking paragraphs data | |
// Paragraphs have different justification | |
if ( | |
portions[0].Paragraph.Justification != 0 || | |
portions[1].Paragraph.Justification != 0 || | |
portions[2].Paragraph.Justification != 0 || | |
portions[3].Paragraph.Justification != 2) | |
{ | |
throw new Exception(); | |
} | |
// All other properties of first and second paragraph are equal | |
for (int j = 0; j < portions.Length; j++) | |
{ | |
var paragraph = portions[j].Paragraph; | |
if (Math.Abs(paragraph.AutoLeading - 1.2) > Tolerance || | |
paragraph.AutoHyphenate != false || | |
paragraph.Burasagari != false || | |
paragraph.ConsecutiveHyphens != 8 || | |
Math.Abs(paragraph.StartIndent) > Tolerance || | |
Math.Abs(paragraph.EndIndent) > Tolerance || | |
paragraph.EveryLineComposer != false || | |
Math.Abs(paragraph.FirstLineIndent) > Tolerance || | |
paragraph.GlyphSpacing.Length != 3 || | |
Math.Abs(paragraph.GlyphSpacing[0] - 1) > Tolerance || | |
Math.Abs(paragraph.GlyphSpacing[1] - 1) > Tolerance || | |
Math.Abs(paragraph.GlyphSpacing[2] - 1) > Tolerance || | |
paragraph.Hanging != false || | |
paragraph.HyphenatedWordSize != 6 || | |
paragraph.KinsokuOrder != 0 || | |
paragraph.LetterSpacing.Length != 3 || | |
Math.Abs(paragraph.LetterSpacing[0]) > Tolerance || | |
Math.Abs(paragraph.LetterSpacing[1]) > Tolerance || | |
Math.Abs(paragraph.LetterSpacing[2]) > Tolerance || | |
paragraph.LeadingType != LeadingMode.Auto || | |
paragraph.PreHyphen != 2 || | |
paragraph.PostHyphen != 2 || | |
Math.Abs(paragraph.SpaceBefore) > Tolerance || | |
Math.Abs(paragraph.SpaceAfter) > Tolerance || | |
paragraph.WordSpacing.Length != 3 || | |
Math.Abs(paragraph.WordSpacing[0] - 0.8) > Tolerance || | |
Math.Abs(paragraph.WordSpacing[1] - 1.0) > Tolerance || | |
Math.Abs(paragraph.WordSpacing[2] - 1.33) > Tolerance || | |
Math.Abs(paragraph.Zone - 36.0) > Tolerance) | |
{ | |
throw new Exception(); | |
} | |
} | |
// Checking style data | |
// Styles have different colors and font size | |
if (Math.Abs(portions[0].Style.FontSize - 12) > Tolerance || | |
Math.Abs(portions[1].Style.FontSize - 12) > Tolerance || | |
Math.Abs(portions[2].Style.FontSize - 12) > Tolerance || | |
Math.Abs(portions[3].Style.FontSize - 10) > Tolerance) | |
{ | |
throw new Exception(); | |
} | |
if (portions[0].Style.FillColor != Color.FromArgb(255, 145, 0, 0) || | |
portions[1].Style.FillColor != Color.FromArgb(255, 201, 128, 2) || | |
portions[2].Style.FillColor != Color.FromArgb(255, 18, 143, 4) || | |
portions[3].Style.FillColor != Color.FromArgb(255, 145, 42, 100)) | |
{ | |
throw new Exception(); | |
} | |
for (int j = 0; j < portions.Length; j++) | |
{ | |
var style = portions[j].Style; | |
if (style.AutoLeading != true || | |
style.HindiNumbers != false || | |
style.Kerning != 0 || | |
style.Leading != 0 || | |
style.StrokeColor != Color.FromArgb(255, 175, 90, 163) || | |
style.Tracking != 50) | |
{ | |
throw new Exception(); | |
} | |
} | |
// Example of text editing | |
portions[0].Text = "Hello "; | |
portions[1].Text = "World"; | |
// Example of text portions removing | |
layer.TextData.RemovePortion(3); | |
layer.TextData.RemovePortion(2); | |
// Example of adding new text portion | |
var createdPortion = layer.TextData.ProducePortion(); | |
createdPortion.Text = "!!!\r"; | |
layer.TextData.AddPortion(createdPortion); | |
portions = layer.TextData.Items; | |
// Example of paragraph and style editing for portions | |
// Set right justification | |
portions[0].Paragraph.Justification = 1; | |
portions[1].Paragraph.Justification = 1; | |
portions[2].Paragraph.Justification = 1; | |
// Different colors for each style. The will be changed, but rendering is not fully supported | |
portions[0].Style.FillColor = Color.Aquamarine; | |
portions[1].Style.FillColor = Color.Violet; | |
portions[2].Style.FillColor = Color.LightBlue; | |
// Different font. The will be changed, but rendering is not fully supported | |
portions[0].Style.FontSize = 6; | |
portions[1].Style.FontSize = 8; | |
portions[2].Style.FontSize = 10; | |
layer.TextData.UpdateLayerData(); | |
im.Save(outputPath, new PsdOptions(im)); | |
break; | |
} | |
} | |
} |
اليك مثالًا آخر يوضح كيف يمكن للمطور الحصول على تنسيق النصوص المختلفة من TextLayer باستخدام Aspose.PSD لـ .NET.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
{ | |
List<ITextPortion> regularText = new List<ITextPortion>(); | |
List<ITextPortion> boldText = new List<ITextPortion>(); | |
List<ITextPortion> italicText = new List<ITextPortion>(); | |
var layers = psdImage.Layers; | |
for (int index = 0; index < layers.Length; index++) | |
{ | |
var layer = layers[index]; | |
if (!(layer is TextLayer)) | |
{ | |
continue; | |
} | |
var textLayer = (TextLayer)layer; | |
// gets fonts that contains in text layer | |
var fonts = textLayer.GetFonts(); | |
var textPortions = textLayer.TextData.Items; | |
foreach (var textPortion in textPortions) | |
{ | |
TextFontInfo font = fonts[textPortion.Style.FontIndex]; | |
if (font != null) | |
{ | |
switch (font.Style) | |
{ | |
case FontStyle.Regular: | |
regularText.Add(textPortion); | |
break; | |
case FontStyle.Bold: | |
boldText.Add(textPortion); | |
break; | |
case FontStyle.Italic: | |
italicText.Add(textPortion); | |
break; | |
default: | |
throw new ArgumentOutOfRangeException(); | |
} | |
} | |
} | |
} | |
} |
تحديث طبقة النص في ملف PSD
تتيح لك Aspose.PSD لـ .NET تلاعب النص في طبقة النص في ملف PSD. يرجى استخدام فئة Aspose.PSD.FileFormats.Psd.Layers.TextLayer لتحديث النص في طبقة PSD. يحمل مقطع الكود التالي ملف PSD ، يصل إلى طبقة النص ، يحدث النص ويحفظ ملف PSD باسم جديد باستخدام الأسلوب Aspose.PSD.FileFormats.Psd.Layers.TextLayer.UpdateText.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "layers.psd")) | |
{ | |
foreach (var layer in psdImage.Layers) | |
{ | |
if (layer is TextLayer) | |
{ | |
TextLayer textLayer = layer as TextLayer; | |
textLayer.UpdateText("test update", new Point(0, 0), 15.0f, Color.Purple); | |
} | |
} | |
psdImage.Save(dataDir + "UpdateTextLayerInPSDFile_out.psd"); | |
} |
دعم طبقات النص على مدار الوقت التشغيلي
يوضح هذا المقال كيفية إضافة طبقات نص على الطير لصور PSD. تم توفير مقطع الكود أدناه.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Add color overlay layer effect at runtime | |
string sourceFileName = dataDir + "ThreeRegularLayers.psd"; | |
string exportPath = dataDir + "ThreeRegularLayersChanged.psd"; | |
var loadOptions = new PsdLoadOptions() | |
{ | |
LoadEffectsResource = true | |
}; | |
using (var im = (PsdImage)Image.Load(sourceFileName, loadOptions)) | |
{ | |
var effect = im.Layers[1].BlendingOptions.AddColorOverlay(); | |
effect.Color = Color.Green; | |
effect.Opacity = 128; | |
effect.BlendMode = BlendMode.Normal; | |
im.Save(exportPath); | |
} |