إدارة فقرات نص PowerPoint في .NET

نظرة عامة

Aspose.Slides for .NET يمثل النص كهيكل هرمي من إطارات النص، الفقرات، والأقسام:

  • ITextFrame يمثل حاوية النص داخل الشكل ويوفر وصولاً إلى مجموعة الفقرات.
  • IParagraph يمثل فقرة واحدة داخل إطار النص ويوفر وصولاً إلى أقسامها وتنسيق الفقرة.
  • IPortion يمثل تشغيل نص داخل الفقرة. يمكن لكل قسم أن يحتوي على نصه الخاص وتنسيق مستوى الحرف.

وبالتالي يمكن للفقرة أن تحتوي على نص بخطوط، ألوان، أحجام، وتنسيقات أخرى مختلفة باستخدام أقسام متعددة.

إنشاء وتنسيق الفقرات

إنشاء فقرات مع أقسام متعددة

الخطوات التالية تنشئ إطار نص يحتوي على ثلاث فقرات، كل منها يحتوي على ثلاثة أقسام:

  1. إنشاء نسخة من الفئة Presentation.
  2. الوصول إلى المرجع الخاص بالشريحة المطلوبة عبر الفهرس.
  3. إضافة IAutoShape مستطيل إلى الشريحة.
  4. الوصول إلى ITextFrame الخاصة بالشكل.
  5. استخدام الفقرة الافتراضية وإضافة كائنين إضافيين من IParagraph إلى إطار النص.
  6. إضافة عدد كافي من كائنات IPortion لكل فقرة لتحتوي على ثلاثة أقسام. الفقرة الافتراضية تحتوي بالفعل على قسم فارغ واحد.
  7. ضبط نص كل قسم.
  8. تطبيق تنسيق على مستوى الحرف عبر IPortion.PortionFormat.
  9. حفظ العرض المعدل.

هذا مثال C# يطبق الخطوات:

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.

  1. إنشاء نسخة من الفئة Presentation.
  2. الوصول إلى المرجع الخاص بالشريحة المطلوبة عبر الفهرس.
  3. إضافة IAutoShape إلى الشريحة المختارة.
  4. الوصول إلى ITextFrame الخاص بالشكل.
  5. إزالة الفقرة الافتراضية من إطار النص.
  6. إنشاء Paragraph للنقطة الرمزية.
  7. ضبط IBulletFormat.Type إلى BulletType.Symbol وتحديد رمز النقطة.
  8. تعيين نص الفقرة، والمسافة البادئة، ولون النقطة، وارتفاع النقطة.
  9. إضافة الفقرة إلى إطار النص.
  10. إنشاء فقرة ثانية وضبط IBulletFormat.Type إلى BulletType.Numbered.
  11. تكوين نمط الترقيم وإضافة الفقرة إلى إطار النص.
  12. حفظ العرض.

هذا مثال C# ينشئ نقطة رمزية ونقطة مرقمة:

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);

استخدام نقاط صورة

تتيح لك نقاط الصورة استبدال الرمز أو الرقم بصورة مخصصة.

  1. إنشاء نسخة من الفئة Presentation.
  2. الوصول إلى المرجع الخاص بالشريحة المطلوبة عبر الفهرس.
  3. إضافة IAutoShape والوصول إلى ITextFrame الخاصة به.
  4. إزالة الفقرة الافتراضية من إطار النص.
  5. تحميل صورة النقطة وإضافتها إلى مجموعة صور العرض كـ IPPImage.
  6. إنشاء Paragraph وتعيين نصها.
  7. ضبط IBulletFormat.Type إلى BulletType.Picture.
  8. إسناد الصورة عبر IBulletFormat.Picture وضبط ارتفاع النقطة.
  9. إضافة الفقرة إلى إطار النص.
  10. حفظ العرض المعدل.

هذا مثال C# ينشئ نقطة بصورة:

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.

  1. إنشاء Presentation والوصول إلى شريحة.
  2. إضافة IAutoShape وإزالة الفقرة الافتراضية من إطار النص الخاص بها.
  3. إنشاء أربع فقرات وتكوين رموز نقطتها.
  4. ضبط قيم IParagraphFormat.Depth إلى 0، 1، 2، و3.
  5. إضافة الفقرات إلى إطار النص وحفظ العرض.

هذا مثال C# ينشئ قائمة نقطية بأربع مستويات:

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 لتحديد الرقم الأولي المعروض للفقرة المرقمة.

  1. إنشاء Presentation وإضافة IAutoShape إلى شريحة.
  2. مسح الفقرة الافتراضية من إطار النص الخاص بالشكل.
  3. إنشاء ثلاث فقرات مرقمة.
  4. ضبط IBulletFormat.NumberedBulletStartWith إلى 2، 3، و7 لكل فقرة على حدة.
  5. إضافة الفقرات إلى إطار النص وحفظ العرض.

هذا مثال C# يعيّن رقم بدء مخصص لكل فقرة:

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.Indent لتوضيح كيف يؤثر مسافة البادئة للسطر الأول على تخطيط الفقرة.

  1. إنشاء نسخة من الفئة Presentation.
  2. الوصول إلى الشريحة المستهدفة.
  3. إضافة IAutoShape مستطيل إلى الشريحة.
  4. الوصول إلى ITextFrame الخاص بالشكل وإزالة الفقرة الافتراضية.
  5. إنشاء عدة فقرات وضبط قيم مختلفة لـ Indent لها.
  6. إضافة الفقرات إلى إطار النص.
  7. حفظ العرض المعدل.

هذا الكود يوضح كيفية ضبط مسافة بادئة الفقرة:

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 إلى قيمة سالبة.

هذا التنسيق مفيد للمراجع، الببليوجرافيا، مداخل القاموس، وغيرها من الفقرات التي تحتاج إلى محاذاة الأسطر الملتفة تحت جسم الفقرة بدلاً من الحرف الأول للسطر الأول.

  1. إنشاء نسخة من الفئة Presentation.
  2. الوصول إلى الشريحة المستهدفة.
  3. إضافة IAutoShape مستطيل إلى الشريحة.
  4. الوصول إلى ITextFrame الخاص بالشكل وإزالة الفقرة الافتراضية.
  5. إنشاء فقرات وضبط قيمة موجبة لـ MarginLeft لكل فقرة.
  6. ضبط قيمة سالبة لـ Indent لإنشاء تأثير البادئة المتدلية.
  7. إضافة الفقرات إلى إطار النص.
  8. حفظ العرض المعدل.

هذا الكود يوضح كيفية ضبط البادئة المتدلية لفقرة:

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 تتحكم في تنسيق علامة نهاية الفقرة. المثال التالي يعيّن حجم الخط والخط اللاتيني لعلامة نهاية الفقرة الثانية:

  1. تحميل Presentation والوصول إلى شريحة.
  2. إضافة IAutoShape ومسح الفقرة الافتراضية الخاصة به.
  3. إنشاء فقرتين وإضافة أقسام نصية إليهما.
  4. إنشاء PortionFormat لعلامة نهاية الفقرة الثانية.
  5. ضبط IBasePortionFormat.FontHeight وIBasePortionFormat.LatinFont.
  6. إسناد التنسيق إلى IParagraph.EndParagraphPortionFormat وحفظ العرض.
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، ويمكنها أن تشغل عدة أسطر معروضة. كسر السطر الصريح داخل الفقرة يُجبر إنشاء سطر جديد دون إنشاء فقرة جديدة. الالتفاف التلقائي يُنشئ أسطرًا بناءً على العرض المتاح دون إدخال كسر سطر صريح في النص. لذلك، العدّ بناءً على الفقرات أو أحرف كسر السطر لا يعطي عدد الأسطر المعروضة.

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

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 إلى الفقرات

استخدم ParagraphCollection.AddFromHtml لتحويل ترميز HTML إلى فقرات وأقسام داخل إطار النص.

  1. إنشاء نسخة من الفئة Presentation.
  2. الوصول إلى شريحة وإضافة IAutoShape.
  3. الوصول إلى ITextFrame الخاص بالشكل ومسح الفقرة الافتراضية.
  4. قراءة ملف HTML المصدر.
  5. تمرير سلسلة HTML إلى ParagraphCollection.AddFromHtml.
  6. حفظ العرض المعدل.

هذا مثال C# يستورد HTML إلى إطار نص:

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.

  1. إنشاء نسخة من الفئة Presentation وتحميل العرض المطلوب.
  2. الوصول إلى الشريحة والبحث عن IAutoShape الذي يحتوي على النص.
  3. الوصول إلى ITextFrame الخاص بالشكل.
  4. استدعاء ParagraphCollection.ExportToHtml مع مؤشر الفقرة البداية وعدد الفقرات المراد تصديرها.
  5. كتابة سلسلة HTML المسترجعة إلى ملف.

هذا مثال C# يصدر جميع الفقرات من أول شكل نص:

using System;
using System.IO;
using System.Text;
using Aspose.Slides;

using var presentation = new Presentation("ExportingHTMLText.pptx");
var shape = presentation.Slides[0].Shapes[0];

if (shape is IAutoShape textShape && textShape.TextFrame != null)
{
    var paragraphs = textShape.TextFrame.Paragraphs;
    var html = paragraphs.ExportToHtml(0, paragraphs.Count, null);
    using var writer = new StreamWriter("paragraphs.html", false, Encoding.UTF8);
    writer.Write(html);
}
else
{
    Console.WriteLine("The first shape is not a text shape.");
}

إخراج فقرة كصورة

IParagraph.GetImage يُخرج فقرة فردية مباشرةً ويُعيد كائن IImage. احفظ النتيجة إلى ملف أو تدفق باستخدام IImage.Save. لا تحتاج إلى إخراج الشكل الحاوي أو قص صورة bitmap يدويًا.

IParagraph.GetImage قد يُعيد null إذا تعذر العثور على الفقرة في مجموعة الأصل، أو لا تملك حدود عرض صالحة، أو لا يمكن عرضها. تحقق من النتيجة قبل حفظها وتحرير الصورة المسترجعة بعد الاستخدام.

إخراج فقرة بالمقياس الافتراضي

لنفترض أن لدينا ملف عرض اسمه sample.pptx يحتوي على شريحة واحدة، حيث الشكل الأول هو صندوق نص يحتوي على ثلاث فقرات.

صندوق النص مع ثلاث فقرات

المثال التالي يخرج الفقرة الثانية داخل شكل نص عادي بالمقياس الافتراضي ويحفظ الصورة المسترجعة بصيغة 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 هو إعداد على مستوى الفقرة وينطبق على الفقرة بالكامل بغض النظر عن تنسيق الأقسام الفردية.

هل يمكنني تعيين لغة التدقيق لجزء من الفقرة؟

نعم. اضبط IBasePortionFormat.LanguageId للأقسام الفردية، بحيث يمكن أن تحتوي الفقرة على نصوص بمختلف اللغات.