كيفية تنسيق النص في .NET

Aspose.Slides FOSS for .NET provides fine-grained text formatting through the PortionFormat الفئة. A Portion هو أصغر وحدة مستقلة من النص؛ يطابق تشغيل تنسيق واحد داخل الفقرة. يوضح هذا الدليل كيفية تطبيق التنسيق الغامق، المائل، حجم الخط، وتنسيق اللون على النص في عرض تقديمي.

دليل خطوة بخطوة

الخطوة 1: تثبيت الحزمة

dotnet add package Aspose.Slides.Foss

الخطوة 2: إضافة شكل مع إطار نص

قبل تنسيق النص، يجب أن يحتوي الشكل على TextFrame. استخدم shape.AddTextFrame() لإنشاء واحدة.

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 500, 150);
var tf = shape.AddTextFrame("Default text: will be formatted");
prs.Save("output.pptx", SaveFormat.Pptx);

الخطوة 3: الوصول إلى TextFrame

shape.AddTextFrame() يعيد الـ TextFrame الكائن. يمكنك أيضًا استرجاعه لاحقًا عبر shape.TextFrame.

var tf = shape.TextFrame;          // if the frame already exists
var tf = shape.AddTextFrame("");   // creates a new frame

A TextFrame يحتوي على قائمة من Paragraph الكائنات (tf.Paragraphs). كل Paragraph يحتوي Portion كائنات (paragraph.Portions).


الخطوة 4: تطبيق تنسيق الغامق والمائل

استخدم PortionFormat.FontBold و PortionFormat.FontItalic. تقبل هذه الخصائص NullableBool.True, NullableBool.False,، أو NullableBool.NotDefined (وراثة من الرئيس).

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 500, 150);
var tf = shape.AddTextFrame("Bold and italic text");

var fmt = tf.Paragraphs[0].Portions[0].PortionFormat;
fmt.FontBold = NullableBool.True;
fmt.FontItalic = NullableBool.True;

prs.Save("bold-italic.pptx", SaveFormat.Pptx);

الخطوة 5: ضبط حجم الخط واللون

اضبط PortionFormat.FontHeight لتحديد الحجم (بالنقاط) واستخدام FillFormat للون.

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Drawing;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 500, 150);
var tf = shape.AddTextFrame("Large corporate-blue heading");

var fmt = tf.Paragraphs[0].Portions[0].PortionFormat;
fmt.FontHeight = 32;                          // 32pt font
fmt.FontBold = NullableBool.True;
fmt.FillFormat.FillType = FillType.Solid;
fmt.FillFormat.SolidFillColor.Color = Color.FromArgb(255, 0, 70, 127);

prs.Save("colored-text.pptx", SaveFormat.Pptx);

Color.FromArgb(alpha, red, green, blue) يقبل القيم من 0 إلى 255 لكل قناة.


الخطوة 6: عدة أجزاء في فقرة واحدة

يمكن لفقرة واحدة أن تحتوي على عدة أجزاء بتنسيق مختلف. أضف جديدًا Portion إلى فقرة Portions المجموعة:

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Drawing;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 600, 100);
var tf = shape.AddTextFrame("");  // start with empty frame

var paragraph = tf.Paragraphs[0];

// First portion: normal text
var portion1 = paragraph.Portions[0];
portion1.Text = "Normal text followed by ";
portion1.PortionFormat.FontHeight = 20;

// Second portion: bold red text
var portion2 = new Portion();
portion2.Text = "bold red text";
portion2.PortionFormat.FontHeight = 20;
portion2.PortionFormat.FontBold = NullableBool.True;
portion2.PortionFormat.FillFormat.FillType = FillType.Solid;
portion2.PortionFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(255, 200, 0, 0);
paragraph.Portions.Add(portion2);

prs.Save("mixed-format.pptx", SaveFormat.Pptx);

المشكلات الشائعة والحلول

النص يظهر باللون الأسود حتى بعد ضبط اللون

تأكد FillFormat.FillType = FillType.Solid يجب أن يتم تعيينه قبل تعيين اللون. بدون تعيين نوع التعبئة، قد لا يكون لتغيير اللون أي تأثير.

NullableBool.True مقابل true

PortionFormat.FontBold يتوقع NullableBool.True, ليس C# true. تعيين C# true لن يتم تجميعه لأن الأنواع غير متوافقة.

الخط لا يظهر في الملف المحفوظ

ال LatinFont تحدد الخاصية عائلة الخط اللاتيني. إذا لم يتم تعيينها، يُستخدم خط سمة العرض. يجب تضمين الخطوط المخصصة أو أن تكون متوفرة على جهاز العرض.


الأسئلة المتكررة

كيف يمكنني تغيير عائلة الخط؟?

تعيين PortionFormat.LatinFont:

fmt.LatinFont = new FontData("Arial");

FontData تقبل اسم عائلة الخط كسلسلة نصية.

كيف يمكنني ضبط محاذاة الفقرة؟?

استخدم ParagraphFormat.Alignment:

tf.Paragraphs[0].ParagraphFormat.Alignment = TextAlignment.Center;

القيم المدعومة: Left, Center, Right, Justify.

كيف يمكنني ضبط تباعد الأسطر؟?

استخدم ParagraphFormat.SpaceBefore (نقاط قبل الفقرة) أو ParagraphFormat.SpaceAfter (نقاط بعد الفقرة):

tf.Paragraphs[0].ParagraphFormat.SpaceBefore = 12;   // 12pt before
tf.Paragraphs[0].ParagraphFormat.SpaceAfter = 6;     // 6pt after

انظر أيضًا

 العربية