تنسيق نص العرض التقديمي في .NET
نظرة عامة
توضح هذه المقالة كيفية تنسيق النص في عروض PowerPoint وOpenDocument باستخدام Aspose.Slides for .NET. وتغطي تمييز النص، ألوان الخلفية، الشفافية، تباعد الأحرف، خصائص الخط، الدوران، تباعد الفقرات، سلوك الملائمة التلقائية، تثبيت النص، مواضع الفواصل، وإعدادات اللغة.
في الأمثلة أدناه، سنستخدم ملفًا باسم “sample.pptx”، يحتوي على مربع نص واحد في الشريحة الأولى بالنص التالي:

تمييز النص
استخدم طريقة ITextFrame.HighlightText عندما تحتاج إلى تمييز النص الذي يطابق عينة معينة داخل إطار نص. تُطبق الطريقة لونًا مميزًا على أجزاء النص المطابقة ويمكن استخدامها مع TextSearchOptions للتحكم في كيفية إجراء البحث، على سبيل المثال لمطابقة الكلمات الكاملة فقط.
الكود أدناه يميز جميع تداخلات الأحرف “try” ثم يميز كلمة “to” الكاملة فقط.
using (var presentation = new Presentation("sample.pptx"))
{
// الحصول على الشكل الأول من الشريحة الأولى.
var shape = (IAutoShape)presentation.Slides[0].Shapes[0];
// تمييز الكلمة "try" في الشكل.
shape.TextFrame.HighlightText("try", Color.LightBlue);
var searchOptions = new TextSearchOptions()
{
WholeWordsOnly = true
};
// تمييز الكلمة "to" في الشكل.
shape.TextFrame.HighlightText("to", Color.Violet, searchOptions, null);
presentation.Save("highlighted_text.pptx", SaveFormat.Pptx);
}
النتيجة:

تمييز النص باستخدام التعابير النمطية
طريقة ITextFrame.HighlightRegex تميز النصوص التي يتم العثور عليها عبر تعبير نمطي. في .NET، تُعرَّف هذه الواجهة على ITextFrame.
الكود أدناه يميز جميع الكلمات التي تحتوي على سبعة أحرف أو أكثر:
using (var presentation = new Presentation(folderPath + "sample.pptx"))
{
var shape = (IAutoShape)presentation.Slides[0].Shapes[0];
var regex = new Regex(@"\b[^\s]{7,}\b");
// تمييز جميع الكلمات التي تحتوي على سبعة أحرف أو أكثر.
shape.TextFrame.HighlightRegex(regex, Color.Yellow, null);
presentation.Save(folderPath + "highlighted_text_using_regex.pptx", SaveFormat.Pptx);
}
النتيجة:

تعيين لون خلفية النص
استخدم IParagraphFormat.DefaultPortionFormat لتعيين لون التمييز الافتراضي لفقرة، أو استخدم IPortionFormat.HighlightColor لأجزاء النص الفردية.
الكود التالي يوضح كيفية تعيين لون الخلفية لكامل الفقرة:
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
// تعيين لون التمييز للفقرة بأكملها.
paragraph.ParagraphFormat.DefaultPortionFormat.HighlightColor.Color = Color.LightGray;
presentation.Save("gray_paragraph.pptx", SaveFormat.Pptx);
}
النتيجة:

الكود أدناه يوضح كيفية تعيين لون الخلفية لأجزاء النص ذات الخط العريض:
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
foreach (var portion in paragraph.Portions)
{
if (portion.PortionFormat.GetEffective().FontBold)
{
// تعيين لون التمييز لجزء النص.
portion.PortionFormat.HighlightColor.Color = Color.LightGray;
}
}
presentation.Save("gray_text_portions.pptx", SaveFormat.Pptx);
}
النتيجة:

محاذاة فقرات النص
استخدم IParagraphFormat.Alignment لتعيين محاذاة الفقرة داخل إطار النص. يمكن أن تكون القيمة مركزية، محاذاة إلى اليسار، إلى اليمين، مبررة، وما إلى ذلك.
الكود التالي يوضح كيفية محاذاة الفقرة إلى المركز:
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
// تعيين محاذاة الفقرة إلى المركز.
paragraph.ParagraphFormat.Alignment = TextAlignment.Center;
presentation.Save("aligned_paragraph.pptx", SaveFormat.Pptx);
}
النتيجة:

تعيين الشفافية للنص
تُتحكم شفافية النص من خلال العنصر ألفا للون المعيّن إلى IPortionFormat.FillFormat. في الأمثلة أدناه، alpha = 50 هو قيمة قناة ألفا بصيغة ARGB على مقياس 0–255، وليس نسبة شفافية.
الكود التالي يوضح كيفية تطبيق الشفافية على كامل الفقرة:
int alpha = 50;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
// تعيين لون التعبئة للنص إلى اللون الشفاف.
paragraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
paragraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(alpha, Color.Black);
presentation.Save("transparent_paragraph.pptx", SaveFormat.Pptx);
}
النتيجة:

الكود التالي يوضح كيفية تطبيق الشفافية على أجزاء النص ذات الخط العريض:
int alpha = 50;
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
foreach (var portion in paragraph.Portions)
{
if (portion.PortionFormat.GetEffective().FontBold)
{
// تعيين شفافية جزء النص.
portion.PortionFormat.FillFormat.FillType = FillType.Solid;
portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(alpha, Color.Black);
}
}
presentation.Save("transparent_text_portions.pptx", SaveFormat.Pptx);
}
النتيجة:

تعيين تباعد الأحرف للنص
استخدم IBasePortionFormat.Spacing لتوسيع أو تقليل التباعد بين الأحرف في مربع النص.
الكود C# التالي يوضح كيفية توسيع تباعد الأحرف في كامل الفقرة:
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
// ملاحظة: استخدم القيم السالبة لتقليل تباعد الأحرف.
paragraph.ParagraphFormat.DefaultPortionFormat.Spacing = 3; // توسيع تباعد الأحرف.
presentation.Save("character_spacing_in_paragraph.pptx", SaveFormat.Pptx);
}
النتيجة:

الكود التالي يوضح كيفية توسيع تباعد الأحرف في أجزاء النص ذات الخط العريض:
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
foreach (var portion in paragraph.Portions)
{
if (portion.PortionFormat.GetEffective().FontBold)
{
// ملاحظة: استخدم القيم السالبة لتقليل تباعد الأحرف.
portion.PortionFormat.Spacing = 3; // توسيع تباعد الأحرف.
}
}
presentation.Save("character_spacing_in_text_portions.pptx", SaveFormat.Pptx);
}
النتيجة:

تعطيل التقريب (Kerning) لبعض الخطوط
في بعض الحالات، قد يبدو النص الذي تُخرجه Aspose.Slides أكثر ضيقًا قليلاً مقارنةً بالنص نفسه في PowerPoint. يمكن أن يحدث ذلك لأن PowerPoint قد يتجاهل بيانات التقريب لبعض الخطوط، حتى وإن كان الخط يحتوي على معلومات تقريب صالحة وكان التقريب مفعَّلًا في إعدادات PowerPoint.
لجعل النتيجة المُصدَّرة أقرب إلى ما في PowerPoint في هذه الحالات، يمكنك تعطيل التقريب لأجزاء النص التي تستخدم الخط المتأثر. اضبط IPortionFormat.KerningMinimalSize إلى قيمة أكبر بكثير من حجم الخط الفعلي:
using (var presentation = new Presentation("presentation.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var targetFont = "Roboto";
foreach (var paragraph in autoShape.TextFrame.Paragraphs)
{
foreach (var portion in paragraph.Portions)
{
if ((portion.PortionFormat.LatinFont != null &&
portion.PortionFormat.LatinFont.FontName == targetFont) ||
(portion.PortionFormat.EastAsianFont != null &&
portion.PortionFormat.EastAsianFont.FontName == targetFont) ||
(portion.PortionFormat.ComplexScriptFont != null &&
portion.PortionFormat.ComplexScriptFont.FontName == targetFont))
{
portion.PortionFormat.KerningMinimalSize = 100;
}
}
}
presentation.Save("output.pptx", SaveFormat.Pptx);
}
هذه الإعدادات تمنع تطبيق التقريب على أجزاء النص المطابقة ويمكن أن تساعد في توافق عرض Aspose.Slides مع المخرجات المرئية في PowerPoint للخطوط المتأثرة بهذا السلوك الخاص بـ PowerPoint.
إدارة خصائص خط النص
يمكن تعيين خصائص الخط على مستوى الفقرة عبر IParagraphFormat.DefaultPortionFormat أو على أجزاء منفردة عبر IPortionFormat.
الكود التالي يضبط الخط ونمط النص لكامل الفقرة: يطبق حجم الخط، العريض، المائل، خط سفلي منقط، وخط Times New Roman على جميع الأجزاء في الفقرة.
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
// تعيين خصائص الخط للفقرة.
paragraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 12;
paragraph.ParagraphFormat.DefaultPortionFormat.FontBold = NullableBool.True;
paragraph.ParagraphFormat.DefaultPortionFormat.FontItalic = NullableBool.True;
paragraph.ParagraphFormat.DefaultPortionFormat.FontUnderline = TextUnderlineType.Dotted;
paragraph.ParagraphFormat.DefaultPortionFormat.LatinFont = new FontData("Times New Roman");
presentation.Save("font_properties_for_paragraph.pptx", SaveFormat.Pptx);
}
النتيجة:

الكود التالي يطبق خصائص مشابهة على أجزاء النص ذات الخط العريض:
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
foreach (var portion in paragraph.Portions)
{
if (portion.PortionFormat.GetEffective().FontBold)
{
// تعيين خصائص الخط لجزء النص.
portion.PortionFormat.FontHeight = 13;
portion.PortionFormat.FontItalic = NullableBool.True;
portion.PortionFormat.FontUnderline = TextUnderlineType.Dotted;
portion.PortionFormat.LatinFont = new FontData("Times New Roman");
}
}
presentation.Save("font_properties_for_text_portions.pptx", SaveFormat.Pptx);
}
النتيجة:

تعيين دوران النص
استخدم ITextFrameFormat.TextVerticalType لتعيين توجيه نص مسبق داخل الشكل.
الكود التالي يضبط توجيه النص داخل الشكل إلى Vertical270، وهو ما يدور النص 90 درجة عكس عقارب الساعة:
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
autoShape.TextFrame.TextFrameFormat.TextVerticalType = TextVerticalType.Vertical270;
presentation.Save("text_rotation.pptx", SaveFormat.Pptx);
}
النتيجة:

تعيين دوران مخصص لإطارات النص
استخدم ITextFrameFormat.RotationAngle لتعيين زاوية دوران مخصصة لـ ITextFrame.
الكود التالي يدير إطار النص بزاوية 3 درجات مع اتجاه عقارب الساعة داخل الشكل:
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
autoShape.TextFrame.TextFrameFormat.RotationAngle = 3;
presentation.Save("custom_text_rotation.pptx", SaveFormat.Pptx);
}
النتيجة:

تعيين تباعد الأسطر للفقرات
توفر Aspose.Slides الخصائص IParagraphFormat.SpaceAfter، IParagraphFormat.SpaceBefore، وIParagraphFormat.SpaceWithin للتحكم في تباعد الفقرات. تُستعمل هذه الخصائص كما يلي:
- استخدم قيمة موجبة لتحديد تباعد السطر كنسبة مئوية من ارتفاع السطر.
- استخدم قيمة سالبة لتحديد تباعد السطر بالنقاط.
الكود التالي يوضح كيفية تحديد تباعد السطر داخل الفقرة:
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
paragraph.ParagraphFormat.SpaceWithin = 200;
presentation.Save("line_spacing.pptx", SaveFormat.Pptx);
}
النتيجة:

تعيين نوع الملائمة التلقائية لإطارات النص
ITextFrameFormat.AutofitType يحدد كيفية تصرف النص عندما يتجاوز حدود حاويته. استخدمه للتحكم فيما إذا كان النص ينكمش، يتجاوز، أو يعيد تحجيم الشكل تلقائيًا.
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
autoShape.TextFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;
presentation.Save("autofit_type.pptx", SaveFormat.Pptx);
}
تعيين تثبيت إطارات النص
ITextFrameFormat.AnchoringType يحدد كيفية تواؤم النص عموديًا داخل الشكل، مثلًا في الأعلى، الوسط، أو الأسفل.
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
autoShape.TextFrame.TextFrameFormat.AnchoringType = TextAnchorType.Bottom;
presentation.Save("text_anchor.pptx", SaveFormat.Pptx);
}
تعيين جدولة علامات التبويب للنص
استخدم IParagraphFormat.DefaultTabSize وIParagraphFormat.Tabs لتكوين مواضع علامات التبويب في الفقرة.
using (var presentation = new Presentation("sample.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
paragraph.ParagraphFormat.DefaultTabSize = 100;
paragraph.ParagraphFormat.Tabs.Add(30, TabAlignment.Left);
presentation.Save("paragraph_tabs.pptx", SaveFormat.Pptx);
}
النتيجة:

تعيين لغة التدقيق
توفر Aspose.Slides الخاصية IPortionFormat.LanguageId، والتي تسمح لك بتعيين لغة التدقيق لجزء النص. تحدد لغة التدقيق اللغة المستخدمة لتصحيح الإملاء والنحو في PowerPoint.
الكود التالي يوضح كيفية تعيين لغة التدقيق لجزء نص:
using (var presentation = new Presentation("presentation.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraph = autoShape.TextFrame.Paragraphs[0];
paragraph.Portions.Clear();
var font = new FontData("SimSun");
var textPortion = new Portion();
textPortion.PortionFormat.ComplexScriptFont = font;
textPortion.PortionFormat.EastAsianFont = font;
textPortion.PortionFormat.LatinFont = font;
// تعيين معرف لغة التدقيق.
textPortion.PortionFormat.LanguageId = "zh-CN";
textPortion.Text = "1。";
paragraph.Portions.Add(textPortion);
presentation.Save("proofing_language.pptx", SaveFormat.Pptx);
}
تعيين اللغة الافتراضية
استخدم LoadOptions.DefaultTextLanguage لتحديد اللغة الافتراضية للنص الذي يتم إنشاؤه أثناء تحميل أو إنشاء عرض تقديمي.
var loadOptions = new LoadOptions();
loadOptions.DefaultTextLanguage = "en-US";
using (var presentation = new Presentation(loadOptions))
{
var slide = presentation.Slides[0];
// إضافة شكل مستطيل جديد يحتوي على نص.
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 150, 50);
shape.TextFrame.Text = "Sample text";
// فحص لغة الجزء الأول.
var portion = shape.TextFrame.Paragraphs[0].Portions[0];
Console.WriteLine(portion.PortionFormat.LanguageId);
}
تعيين نمط النص الافتراضي
لتطبيق تنسيق نص افتراضي على مستوى العرض التقديمي، استخدم IPresentation.DefaultTextStyle.
الكود التالي يوضح كيفية تعيين خط عريض بحجم 14 نقطة كنمط نص افتراضي لكل النص عبر الشرائح في عرض تقديمي جديد.
using (var presentation = new Presentation())
{
// احصل على تنسيق الفقرة من المستوى الأعلى.
var paragraphFormat = presentation.DefaultTextStyle.GetLevel(0);
if (paragraphFormat != null)
{
paragraphFormat.DefaultPortionFormat.FontHeight = 14;
paragraphFormat.DefaultPortionFormat.FontBold = NullableBool.True;
}
presentation.Save("default_text_style.pptx", SaveFormat.Pptx);
}
استخراج النص مع تأثير الحروف الكبيرة الكاملة
في PowerPoint، تطبيق تأثير All Caps يجعل النص يظهر بأحرف كبيرة على الشريحة حتى لو تم كتابة النص أصلاً بأحرف صغيرة. عند استرجاع مثل هذا الجزء باستخدام Aspose.Slides، تُعيد المكتبة النص كما تم إدخاله. لمطابقة النص المعروض، تحقق من TextCapType وحوِّل السلسلة المسترجعة إلى أحرف كبيرة عندما تكون القيمة All.
لنفترض أن لدينا مربع النص التالي في الشريحة الأولى من ملف sample2.pptx.

الكود التالي يوضح كيفية استخراج النص مع تطبيق تأثير All Caps:
using (var presentation = new Presentation("sample2.pptx"))
{
var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var textPortion = autoShape.TextFrame.Paragraphs[0].Portions[0];
Console.WriteLine($"Original text: {textPortion.Text}");
var textFormat = textPortion.PortionFormat.GetEffective();
if (textFormat.TextCapType == TextCapType.All)
{
var text = textPortion.Text.ToUpper();
Console.WriteLine($"All-Caps effect: {text}");
}
}
المخرجات:
Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!
الأسئلة المتكررة
كيف يمكن تعديل النص في جدول داخل شريحة؟
لتعديل النص في جدول داخل شريحة، استخدم ITable. استعرض الخلايا وقم بتحديث كل خلية عبر ICell.TextFrame وتنسيق الفقرة عبر IParagraph.ParagraphFormat.
كيف يمكن تطبيق لون متدرج للنص في شريحة PowerPoint؟
لتطبيق لون متدرج للنص، استخدم IPortionFormat.FillFormat. اضبط IFillFormat.FillType على FillType.Gradient وملّف خيارات التدرج، الاتجاه، والشفافية.