إدارة فقرات نص PowerPoint في .NET
نظرة عامة
Aspose.Slides for .NET تمثل النص كهرمية من إطارات النص، الفقرات، والجزءات:
- ITextFrame يمثل حاوية النص داخل الشكل ويوفر الوصول إلى مجموعة الفقرة الخاصة به.
- IParagraph يمثل فقرة واحدة داخل إطار النص ويوفر الوصول إلى الجزءات وتنسيق مستوى الفقرة.
- IPortion يمثل تشغيل نص داخل الفقرة. يمكن لكل جزء أن يحتوي على نصه الخاص وتنسيق مستوى الأحرف.
وبالتالي يمكن للفقرة أن تحتوي على نص بخطوط، ألوان، أحجام، وتنسيقات أخرى مختلفة باستخدام عدة جزءات.
إنشاء وتنسيق الفقرات
إنشاء فقرات داخل إطار النص الخاص بالشكل واستخدام الجزءات لتنسيق تشغيلات النص الفردية. يوضح هذا القسم كيفية دمج عدة جزءات داخل كل فقرة وتطبيق ألوان، أحجام خطوط، وأنماط مختلفة.
إنشاء فقرات مع عدة جزءات
المثال التالي بلغة C# ينشئ ثلاث فقرات، كل منها يحتوي على ثلاث تشغيلات نصية في مجموعة IParagraph.Portions. يعيد استخدام الفقرة الافتراضية والجزء الفارغ، ثم يضيف الفقرات والجزءات المتبقية. يتم تطبيق التنسيق عبر IPortion.PortionFormat: الجزء الأول في كل فقرة أحمر، غامق، وبحجم 15 نقطة؛ الثاني أزرق، مائل، وبحجم 18 نقطة. الجزء الثالث يحتفظ بالتنسيق الافتراضي:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 150, 300, 150);
var textFrame = shape.TextFrame;
var firstParagraph = textFrame.Paragraphs[0];
firstParagraph.Portions.Add(new Portion());
firstParagraph.Portions.Add(new Portion());
var secondParagraph = new Paragraph();
secondParagraph.Portions.Add(new Portion());
secondParagraph.Portions.Add(new Portion());
secondParagraph.Portions.Add(new Portion());
textFrame.Paragraphs.Add(secondParagraph);
var thirdParagraph = new Paragraph();
thirdParagraph.Portions.Add(new Portion());
thirdParagraph.Portions.Add(new Portion());
thirdParagraph.Portions.Add(new Portion());
textFrame.Paragraphs.Add(thirdParagraph);
var paragraphCount = textFrame.Paragraphs.Count;
for (var paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++)
{
var paragragaph = textFrame.Paragraphs[paragraphIndex];
var portionCount = paragragaph.Portions.Count;
for (var portionIndex = 0; portionIndex < portionCount; portionIndex++)
{
var portion = paragragaph.Portions[portionIndex];
portion.Text = $"Portion {paragraphIndex + 1}.{portionIndex + 1}";
if (portionIndex == 0)
{
portion.PortionFormat.FillFormat.FillType = FillType.Solid;
portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.Red;
portion.PortionFormat.FontBold = NullableBool.True;
portion.PortionFormat.FontHeight = 15;
}
else if (portionIndex == 1)
{
portion.PortionFormat.FillFormat.FillType = FillType.Solid;
portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.Blue;
portion.PortionFormat.FontItalic = NullableBool.True;
portion.PortionFormat.FontHeight = 18;
}
}
}
presentation.Save("paragraphs_with_portions.pptx", SaveFormat.Pptx);
إنشاء قوائم نقطية ومرقمة
تنظيم نص الفقرة في قوائم باستخدام رموز، أرقام، أو صور مخصصة. تغطي الأمثلة في هذا القسم تنسيق النقاط، القوائم المتعددة المستويات، وأعداد البدء المخصصة.
إنشاء قائمة نقطية أو مرقمة
تسهل النقاط والترقيم قراءة العناصر ذات الصلة. في Aspose.Slides يتم تعريف إعدادات القوائم عبر IBulletFormat.
يضيف هذا المثال بلغة C# نقطة رمزية وعنصر مرقم إلى نفس إطار النص. IBulletFormat.Type يحدد نوع النقطة، بينما IBulletFormat.Char يوفر الرمز وIBulletFormat.NumberedBulletStyle يحدد نمط الترقيم. يستخدم العنصران نقاطًا سوداء بارتفاع 100 بالمئة وتراجع فقرة 25 نقطة:
using System;
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var symbolParagraph = new Paragraph { Text = "Welcome to Aspose.Slides" };
symbolParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
symbolParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
symbolParagraph.ParagraphFormat.Indent = 25;
symbolParagraph.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
symbolParagraph.ParagraphFormat.Bullet.Color.Color = Color.Black;
symbolParagraph.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True;
symbolParagraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(symbolParagraph);
var numberedParagraph = new Paragraph { Text = "This is a numbered item" };
numberedParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
numberedParagraph.ParagraphFormat.Bullet.NumberedBulletStyle = NumberedBulletStyle.BulletCircleNumWDBlackPlain;
numberedParagraph.ParagraphFormat.Indent = 25;
numberedParagraph.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
numberedParagraph.ParagraphFormat.Bullet.Color.Color = Color.Black;
numberedParagraph.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True;
numberedParagraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(numberedParagraph);
presentation.Save("bulleted_and_numbered_list.pptx", SaveFormat.Pptx);
استخدام نقاط صورة
تتيح نقاط الصورة استخدام صورة مخصصة بدلاً من رمز أو رقم.
يضيف هذا المثال بلغة C# صورة إلى مجموعة صور العرض كـ IPPImage ويستخدمها كنقطة فقرة. يضبط IBulletFormat.Type لاستخدام نقطة صورة ويعين الصورة عبر IBulletFormat.Picture. يتم ضبط IBulletFormat.Height على 100 بالمئة لتحديد حجم النقطة نسبةً إلى نص الفقرة:
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
using var bulletImage = Images.FromFile("bullets.png");
var presentationImage = presentation.Images.AddImage(bulletImage);
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var paragraph = new Paragraph { Text = "Welcome to Aspose.Slides" };
paragraph.ParagraphFormat.Bullet.Type = BulletType.Picture;
paragraph.ParagraphFormat.Bullet.Picture.Image = presentationImage;
paragraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(paragraph);
presentation.Save("picture_bullet.pptx", SaveFormat.Pptx);
presentation.Save("picture_bullet.ppt", SaveFormat.Ppt);
إنشاء قائمة متعددة المستويات
اضبط IParagraphFormat.Depth لتحديد مستويات الفقرات داخل القائمة. المستوى العلوي له عمق 0.
يعين هذا المثال بلغة C# قيم IParagraphFormat.Depth من 0 إلى 3 لأربع فقرات، مكونًا هرماً داخل إطار نص واحد. تستخدم كل فقرة نقطة رمز، مع تبديل IBulletFormat.Char بين نقطة دائرية وواصلة لتمييز المستويات المتجاورة:
using System;
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "Content" };
firstParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
firstParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.Depth = 0;
var secondParagraph = new Paragraph { Text = "Second level" };
secondParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
secondParagraph.ParagraphFormat.Bullet.Char = '-';
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.Depth = 1;
var thirdParagraph = new Paragraph { Text = "Third level" };
thirdParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
thirdParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
thirdParagraph.ParagraphFormat.Depth = 2;
var fourthParagraph = new Paragraph { Text = "Fourth level" };
fourthParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
fourthParagraph.ParagraphFormat.Bullet.Char = '-';
fourthParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
fourthParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
fourthParagraph.ParagraphFormat.Depth = 3;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
textFrame.Paragraphs.Add(thirdParagraph);
textFrame.Paragraphs.Add(fourthParagraph);
presentation.Save("multilevel_list.pptx", SaveFormat.Pptx);
بدء عناصر القائمة المرقمة بقيم مخصصة
استخدم IBulletFormat.NumberedBulletStartWith لتحديد الرقم الأول المعروض لفقرة مرقمة.
هذا المثال بلغة C# يضبط IBulletFormat.Type لاستخدام الترقيم لثلاث فقرات ويعين قيم IBulletFormat.NumberedBulletStartWith 2، 3، و7. يتم تكوين القيمة الابتدائية بشكل منفصل لكل فقرة، مما يسمح بتخطي الأرقام المعروضة:
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "Start at 2" };
firstParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
firstParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 2;
textFrame.Paragraphs.Add(firstParagraph);
var secondParagraph = new Paragraph { Text = "Start at 3" };
secondParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
secondParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 3;
textFrame.Paragraphs.Add(secondParagraph);
var thirdParagraph = new Paragraph { Text = "Start at 7" };
thirdParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
thirdParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 7;
textFrame.Paragraphs.Add(thirdParagraph);
presentation.Save("custom_numbered_list.pptx", SaveFormat.Pptx);
التحكم في تخطيط الفقرة وخصائص النهاية
ضبط هوامش الفقرة وتراجعاتها للتحكم في موضع السطر الأول والأسطر المغلفة. يمكنك أيضًا تنسيق علامة نهاية الفقرة بشكل منفصل عن جزءات النص المرئية.
تعيين تراجع السطر الأول
استخدم الخاصية IParagraphFormat.Indent للتحكم في تراجع السطر الأول للفقرة. هذه الخاصية تحرك السطر الأول فقط بالنسبة لهامش الفقرة الأيسر. القيمة الموجبة تنقل السطر الأول إلى اليمين، بينما تبقى الأسطر المتبقية محاذاة إلى جسم الفقرة.
استخدم IParagraphFormat.MarginLeft عندما تحتاج إلى نقل الفقرة بالكامل. استخدم IParagraphFormat.Indent عندما تحتاج إلى نقل السطر الأول فقط.
يعطي المثال التالي ثلاث فقرات نفس قيمة IParagraphFormat.MarginLeft البالغة 20 نقطة وقيم IParagraphFormat.Indent 0، 20، و40 نقطة. تبدأ الأسطر الأولى عند إزاحات متزايدة تدريجيًا، بينما تحتفظ الأسطر المغلفة بنفس الهامش الأيسر:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
shape.FillFormat.FillType = FillType.NoFill;
shape.LineFormat.FillFormat.FillType = FillType.Solid;
shape.LineFormat.FillFormat.SolidFillColor.Color = Color.Gray;
var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "No first-line indent. Wrapped lines start at the same position as the first line." };
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.MarginLeft = 20;
firstParagraph.ParagraphFormat.Indent = 0;
var secondParagraph = new Paragraph { Text = "First-line indent of 20 points. The first line moves to the right, while wrapped lines remain aligned to the paragraph body." };
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.MarginLeft = 20;
secondParagraph.ParagraphFormat.Indent = 20;
var thirdParagraph = new Paragraph { Text = "First-line indent of 40 points. This paragraph shows a larger first-line offset to make the effect easier to see." };
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
thirdParagraph.ParagraphFormat.MarginLeft = 20;
thirdParagraph.ParagraphFormat.Indent = 40;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
textFrame.Paragraphs.Add(thirdParagraph);
presentation.Save("paragraph_indent.pptx", SaveFormat.Pptx);
النتيجة:

تعيين تراجع معلق
التراجع المعلق هو تخطيط فقرة يبدأ فيه السطر الأول إلى اليسار من الأسطر المتبقية. في Aspose.Slides يمكنك إنشاء هذا التأثير باستخدام الخاصية IParagraphFormat.Indent. اضبط Indent إلى قيمة سالبة لنقل السطر الأول إلى اليسار بالنسبة إلى جسم الفقرة.
في الواقع، IParagraphFormat.MarginLeft يحدد الموضع الأيسر لجسم الفقرة، وIParagraphFormat.Indent يحدد موضع السطر الأول نسبةً إلى ذلك الهامش. لإنشاء تراجع معلق، اضبط قيمة MarginLeft موجبة وقيمة Indent سالبة.
هذا التنسيق مفيد للمراجع، الفهارس، مداخل القاموس، وفقرات أخرى حيث يجب محاذاة الأسطر المغلفة تحت جسم الفقرة وليس تحت الحرف الأول للسطر.
يماثل المثال التالي بلغة C# قيم IParagraphFormat.MarginLeft 40 و60 نقطة مع قيم IParagraphFormat.Indent -20 و-30 نقطة. في كل فقرة يبدأ السطر الأول إلى اليسار من الأسطر المغلفة؛ الفقرة الثانية لها تراجع معلق أعمق:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
shape.FillFormat.FillType = FillType.NoFill;
shape.LineFormat.FillFormat.FillType = FillType.Solid;
shape.LineFormat.FillFormat.SolidFillColor.Color = Color.Gray;
var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "A hanging indent is created by combining a positive left margin with a negative indent. The first line starts to the left, while wrapped lines align with the paragraph body." };
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.MarginLeft = 40;
firstParagraph.ParagraphFormat.Indent = -20;
var secondParagraph = new Paragraph { Text = "This second example uses a deeper hanging indent so the difference between the first line and the wrapped lines is easier to compare." };
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.MarginLeft = 60;
secondParagraph.ParagraphFormat.Indent = -30;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
presentation.Save("hanging_indent.pptx", SaveFormat.Pptx);
النتيجة:

تعيين خصائص تشغيل نهاية الفقرة
تتحكم الخاصية IParagraph.EndParagraphPortionFormat في تنسيق علامة نهاية الفقرة. ينشئ المثال التالي PortionFormat مع ضبط IBasePortionFormat.FontHeight على 48 نقطة وIBasePortionFormat.LatinFont على Times New Roman. يطبّق هذا التنسيق على علامة النهاية للفقرة الثانية، دون تغيير الجزءات النصية المرئية:
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("Test.pptx");
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, 200, 250);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph();
firstParagraph.Portions.Add(new Portion("Sample text"));
var secondParagraph = new Paragraph();
secondParagraph.Portions.Add(new Portion("Sample text 2"));
var endParagraphFormat = new PortionFormat();
endParagraphFormat.FontHeight = 48;
endParagraphFormat.LatinFont = new FontData("Times New Roman");
secondParagraph.EndParagraphPortionFormat = endParagraphFormat;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
presentation.Save("end_paragraph_format.pptx", SaveFormat.Pptx);
عدد الأسطر التي يتم عرضها
للقواعد المتعلقة بتغليف النص التلقائي وعلامات الترقيم عند نهايات الأسطر، راجع Control Line Breaking وControl Hanging Punctuation.
استخدم IParagraph.GetLinesCount لحساب عدد الأسطر التي تحتلها الفقرة بعد تخطيط النص، بما في ذلك التغليف التلقائي. هذا مفيد عند فحص طول النص وتخطيط القوالب التقديمية.
الفقرة هي عنصر واحد في ITextFrame.Paragraphs، ويمكن أن تحتل عدة أسطر معروضة. يُحدث كسر سطر صريح داخل الفقرة سطرًا جديدًا دون إنشاء فقرة إضافية. يُنشئ التغليف التلقائي أسطرًا بناءً على العرض المتاح دون إدراج كسر سطر صريح في النص. وبالتالي، عد الفقرات أو الأحرف الخاصة بكسور السطر لا يعطي عدد الأسطر المعروضة.
ينشئ المثال التالي شكل نص، يحسب أسطره، يضيق الشكل، ثم يستبدل النص بسلسلة أقصر. يفعّل المثال ITextFrameFormat.WrapText ويعطل ITextFrameFormat.AutofitType بحيث يتحكم عرض الشكل في التغليف دون تصغير النص أو تغيير حجم الشكل تلقائيًا. كل قياس يستخدم IParagraph.GetLinesCount. أبعاد الشكل بالنقاط. أخيرًا، يضيف المثال فقرة أخرى ويجمع عدد الأسطر عبر إطار النص.
using System;
using Aspose.Slides;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 400, 200);
var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.WrapText = NullableBool.True;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.None;
var paragraph = textFrame.Paragraphs[0];
paragraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 20;
paragraph.Text = "This text demonstrates how automatic wrapping changes the number of rendered lines.";
Console.WriteLine($"Original width: {paragraph.GetLinesCount()}");
shape.Width = 150;
Console.WriteLine($"Narrower shape: {paragraph.GetLinesCount()}");
paragraph.Text = "Short text.";
Console.WriteLine($"Shorter text: {paragraph.GetLinesCount()}");
var secondParagraph = new Paragraph { Text = "Another paragraph." };
secondParagraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 20;
textFrame.Paragraphs.Add(secondParagraph);
var totalLineCount = 0;
foreach (var currentParagraph in textFrame.Paragraphs)
{
totalLineCount += currentParagraph.GetLinesCount();
}
Console.WriteLine($"Total lines in the text frame: {totalLineCount}");
مع هذا النص وهذه الأبعاد، يؤدي تضييق الشكل إلى زيادة عدد الأسطر، بينما يقلل استبدال النص بالسلسلة القصيرة منه. قد تختلف الأعداد الدقيقة تبعًا لتوافر الخطوط والاستبدال، حجم الخط، الهوامش، التراجعات، التغليف، وإعدادات الملاءمة التلقائية. استخدم الخطوط وإعدادات التخطيط المقصودة للبيئة المستهدفة عند فحص القالب.
لا يحدد عدد الأسطر وحده ما إذا كان النص سيتجاوز الحاوية. ارتفاع المتاح، ارتفاع الأسطر، تباعد الفقرات والأسطر، وسلوك الملاءمة التلقائية أيضًا مهم؛ حتى سطر واحد يمكن أن يتجاوز العرض المتاح عندما يكون التغليف معطلًا.
استيراد وتصدير محتوى الفقرة
استخدم HTML لإدخال النص إلى عرض تقديمي أو لتصدير الفقرات الحالية للاستخدام خارجها. يوضح هذا القسم أيضًا كيفية عرض الفقرات الفردية كصور، بما في ذلك الفقرات داخل خلايا الجداول.
استيراد نص HTML إلى الفقرات
استخدم ParagraphCollection.AddFromHtml لتحويل ترميز HTML إلى فقرات وجزءات داخل إطار النص.
يقرأ هذا المثال بلغة C# HTML من ملف ويمرّر الترميز إلى ParagraphCollection.AddFromHtml. يزيل الفقرة الافتراضية لإطار النص بحيث يحتوي الإطار فقط على المحتوى المستورد. يترك الشكل المستطيل هامشًا يبلغ 10 نقاط على كل جانب من الشريحة:
using System.IO;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shapeWidth = presentation.SlideSize.Size.Width - 20;
var shapeHeight = presentation.SlideSize.Size.Height - 20;
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, shapeWidth, shapeHeight);
shape.FillFormat.FillType = FillType.NoFill;
shape.TextFrame.Paragraphs.Clear();
using var reader = new StreamReader("file.html");
var html = reader.ReadToEnd();
shape.TextFrame.Paragraphs.AddFromHtml(html);
presentation.Save("html_text.pptx", SaveFormat.Pptx);
تصدير نص الفقرة إلى HTML
استخدم ParagraphCollection.ExportToHtml لتصدير مجموعة محددة من الفقرات كـ HTML. عند إعادة استخدام نص العرض على موقع ويب، قد ترغب في الحفاظ على التنسيق المحلي دون نقل خط العرض، الحجم، أو أنماط صندوق النص. اضبط TextToHtmlConversionOptions.TextInheritanceLimit للتحكم في مدى متابعة المُصدّر للوراثة التنسيقية.
حمّل العرض التمثيلي وضعه في دليل عمل التطبيق. يحتوي شريحة العرض الأولى على شكل نص يحتوي على النص “Inherited text; explicit red 18pt”. يستخدم الإدخال مستويات تنسيق متعددة عمدًا:
- النمط النصي الافتراضي للعرض يزود بـ Calibri وحجم 28 pt.
- نمط النص من المستوى الأول لإطار النص يزود بنص أزرق ومائل.
- الفقرة تحدد النص غامقًا صراحة.
- الجزء الثاني، “explicit red 18pt”، يحدد نصًا أحمر وحجم 18 pt صراحة.
يصدّر المثال الكامل التالي الفقرات نفسها ثلاث مرات. يمرّر TextToHtmlConversionOptions إلى طريقة التصدير ويكتب كل نتيجة كملف HTML UTF-8 منفصل:
using System.IO;
using System.Text;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("text-inheritance.pptx");
var textShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraphs = textShape.TextFrame.Paragraphs;
var modes = new[] { TextInheritanceLimit.All, TextInheritanceLimit.TextBox, TextInheritanceLimit.ParagraphOnly };
foreach (var mode in modes)
{
var options = new TextToHtmlConversionOptions { TextInheritanceLimit = mode };
var html = paragraphs.ExportToHtml(0, paragraphs.Count, options);
File.WriteAllText($"{mode}.html", html, Encoding.UTF8);
}
يحتوي HTML الناتج على CSS التالي للجزء الأول، “Inherited text; “. تُظهر الجدول التعليمات ذات الصلة؛ تم حذف الخصائص الأخرى لتوضيح الهدف.
| المخرجات | CSS ذات الصلة للجزء الأول | المظهر |
|---|---|---|
All.html |
font-family:'Calibri'; font-size:28pt; color:#0000ff; font-style:italic; font-weight:bold |
نص أزرق، غامق، مائل بحجم 28 pt بخط Calibri، إذا كان الخط متاحًا. |
TextBox.html |
color:#0000ff; font-style:italic; font-weight:bold |
نص أزرق، غامق، مائل؛ الصفحة أو المتصفح يحدد خط العائلة والحجم. |
ParagraphOnly.html |
font-weight:bold |
نص غامق؛ الصفحة أو المتصفح يحدد خط العائلة، الحجم، اللون، والنمط المائل. |
يحتفظ الجزء الثاني بـ color:#ff0000، font-size:18pt، وfont-weight:bold في جميع المخرجات الثلاث. كما يحتفظ بالمائل في All وTextBox، لكن ليس في ParagraphOnly. لذا فإن تقييد الوراثة لا يزيل التنسيق المحدد صراحةً للفقرة أو الجزء.
افتح الملفات في المتصفح نفسه للمقارنة. في الوضعين المقيدين، يتم حل خصائص CSS المحذوفة بواسطة أنماط صفحة الوجهة أو إعدادات المتصفح الافتراضية، لذا يعتمد المظهر النهائي على مكان استخدام HTML.
حفظ الفقرات المصدرة كـ HTML UTF-8
تحدد الخاصية ITextToHtmlConversionOptions.EncodingName مجموعة الأحرف المعلن عنها في سلسلة HTML التي تُعيدها ParagraphCollection.ExportToHtml. عند حفظ تلك السلسلة، يحدد الترميز الذي تمرره إلى File.WriteAllText البايتات الفعلية. استخدم نفس الترميز لكلا العملية حتى يتمكن المتصفح من فك تشفير النص متعدد اللغات بشكل صحيح.
using System.IO;
using System.Text;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 0, 0, 400, 100);
shape.TextFrame.Text = "Café | Ωω | 日本語 | 😀";
var options = new TextToHtmlConversionOptions
{
EncodingName = "utf-8",
AddClipboardFragmentHeader = false
};
var paragraphs = shape.TextFrame.Paragraphs;
var html = paragraphs.ExportToHtml(0, paragraphs.Count, options);
File.WriteAllText("paragraphs.html", html, new UTF8Encoding(false));
يعلن الملف المحفوظ عن charset=utf-8 ويحتوي على بايتات UTF-8. فتح paragraphs.html في المتصفح يحافظ على النص “Café | Ωω | 日本語 | 😀”.
إنشاء مقطع HTML للضغط على الحافظة
اضبط AddClipboardFragmentHeader على false للحصول على HTML عادي أو true لإضافة رأس الحافظة. كلا الاستدعاءين يعيدان سلسلة. يضيف الرأس إزاحات بايت UTF-8 للـ HTML المحيط (StartHTML وEndHTML) والمحتوى المراد لصقه (StartFragment وEndFragment).
يصدّر هذا المثال فقرتين منسقتين كـ HTML عادي (html) وكـ مقطع HTML للضغط على الحافظة (clipboardHtml). كتابة المقطع إلى الحافظة النظامية خطوة تطبيق منفصلة. تتطلب واجهة برمجة الحافظة التي تقبل بايتات HTML الخام UTF-8 بدون BOM.
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 0, 0, 400, 100);
var paragraphs = shape.TextFrame.Paragraphs;
paragraphs.Clear();
paragraphs.AddFromHtml("<p>Status: <b>Ready</b></p><p><i>Next steps</i></p>");
var options = new TextToHtmlConversionOptions { AddClipboardFragmentHeader = false };
var html = paragraphs.ExportToHtml(0, paragraphs.Count, options);
options.AddClipboardFragmentHeader = true;
var clipboardHtml = paragraphs.ExportToHtml(0, paragraphs.Count, options);
عرض الفقرة كصورة
يُعيد IParagraph.GetImage صورة للفقرة الفردية مباشرةً ويُعيد كائنًا من نوع IImage. احفظ النتيجة إلى ملف أو تدفق باستخدام IImage.Save. لا تحتاج إلى عرض الشكل المحتوي أو قص صورة يدوية.
يمكن أن يُعيد IParagraph.GetImage قيمة null إذا لم تُعثر الفقرة في مجموعتها الأصلية، أو لا توجد حدود عرض صالحة، أو لا يمكن عرضها. افحص النتيجة قبل حفظها وتخلص من الصورة المرتجعة بعد الاستخدام.
عرض الفقرة بالمقياس الافتراضي
يستخدم المثال عرضًا يحتوي شريحة واحدة، وشكلها الأول هو مربع نص يحتوي على ثلاث فقرات.

يختار المثال الفقرة الثانية بالمؤشر 1 ويستدعي IParagraph.GetImage بدون معامل مقياس. يتحقق من أن الشكل يحتوي على الفقرة المطلوبة وأن العرض أعاد صورة قبل استدعاء IImage.Save لحفظها بتنسيق PNG. يقوم بيان using بالتخلص من الصورة بعد الاستخدام.
using System;
using Aspose.Slides;
using var presentation = new Presentation("sample.pptx");
var shape = presentation.Slides[0].Shapes[0];
if (shape is IAutoShape textShape &&
textShape.TextFrame != null &&
textShape.TextFrame.Paragraphs.Count > 1)
{
var paragraph = textShape.TextFrame.Paragraphs[1];
using var paragraphImage = paragraph.GetImage();
if (paragraphImage != null)
{
paragraphImage.Save("paragraph.png", ImageFormat.Png);
}
else
{
Console.WriteLine("The paragraph could not be rendered.");
}
}
else
{
Console.WriteLine("The expected text shape or paragraph was not found.");
}
النتيجة:

عرض الفقرة في خلية جدول مع المقياس
استخدم التحميل الزائد لـ IParagraph.GetImage الذي يتقبل معاملات float scaleX و float scaleY لتحديد عوامل المقياس الأفقي والرأسي. ينشئ المثال جدولًا، ويعرض الفقرة في خليةه الأولى بعرض وارتفاع مضاعفين، ثم يحفظ النتيجة كصورة PNG.
using System;
using Aspose.Slides;
var scaleX = 2f;
var scaleY = 2f;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var table = slide.Shapes.AddTable(50, 50, new[] { 300d }, new[] { 80d });
var paragraph = table[0, 0].TextFrame.Paragraphs[0];
paragraph.Text = "Text in a table cell";
using var paragraphImage = paragraph.GetImage(scaleX, scaleY);
if (paragraphImage != null)
{
paragraphImage.Save("table_paragraph.png", ImageFormat.Png);
}
else
{
Console.WriteLine("The paragraph could not be rendered.");
}
عامل المقياس 1 يحافظ على البكسل الافتراضي لهذا المحور. على سبيل المثال، 2 لكل العاملين ينتج صورة بعرض وارتفاع تقريبًا ضعف الأبعاد الافتراضية، أي أربعة أضعاف عدد البكسلات. العوامل الأكبر عمومًا تُنتج نصًا أكثر حدة للتكبير أو الإخراج عالي الدقة، لكنّها تزيد أيضًا من استهلاك الذاكرة وحجم الملف. العوامل الأقل من 1 تنتج صورًا أصغر بتفاصيل أقل. استخدم عوامل مساوية للحفاظ على نسبة أبعاد الفقرة؛ العوامل الأفقية والرأسية المختلفة تقوم بتمديد الناتج بشكل مستقل.
ما يزال عرض الشكل بالكامل باستخدام IShape.GetImage مفيدًا عندما يجب أن يتضمن الإخراج تعبئة الشكل، حدوده، أو سياقه البصري الآخر. بالنسبة لصورة تحتوي على الفقرة فقط، استخدم IParagraph.GetImage.
الأسئلة المتكررة
هل يمكن تعطيل التغليف داخل إطار النص تمامًا؟
نعم. اضبط ITextFrameFormat.WrapText لتعطيل التغليف بحيث لا تنكسر الأسطر عند حواف إطار النص.
كيف يمكن الحصول على حدود الفقرة المحددة داخل الشريحة؟
استخدم IParagraph.GetRect لاسترداد المستطيل المحيط بالفقرة. يوفر IPortion.GetRect حدود الجزء الفردي.
أين يتم التحكم في محاذاة الفقرة (يسار، يمين، وسط أو ضبط)؟
IParagraphFormat.Alignment هو إعداد على مستوى الفقرة ويطبق على الفقرة بأكملها بغض النظر عن تنسيق الجزءات الفردية.
لضبط محاذاة الخطوط ذات الأحجام المختلفة داخل كل سطر، راجع Align Fonts Within a Line.
هل يمكن تعيين لغة التدقيق لجزء من الفقرة؟
نعم. اضبط IBasePortionFormat.LanguageId للجزءات الفردية، بحيث يمكن لفقرة واحدة احتواء نصوص بعدة لغات.