مربع النص

في Aspose.Slides، يتم تمثيل مربع النص بواسطة AutoShape. يمكن لأي شكل تقريبًا أن يحتوي على نص، ولكن مربع النص النموذجي لا يحتوي على تعبئة أو حدود ويعرض النص فقط.

هذا الدليل يوضح كيفية إضافة، الوصول إلى، وإزالة مربعات النص برمجيًا.

إضافة مربع نص

مربع النص هو ببساطة AutoShape بدون تعبئة أو حدود وبعض النص المنسق. إليك كيفية إنشائه:

public static void Add_TextBox()
{
    using var pres = new Presentation();

    // Create a rectangle shape (defaults to filled with border and no text)
    var textBox = pres.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, x: 50, y: 75, width: 150, height: 100);

    // Remove fill and border to make it look like a typical text box
    textBox.FillFormat.FillType = FillType.NoFill;
    textBox.LineFormat.FillFormat.FillType = FillType.NoFill;

    // Set text formatting
    textBox.TextFrame.Paragraphs[0].ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
    textBox.TextFrame.Paragraphs[0].ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;

    // Assign the actual text content
    textBox.TextFrame.Text = "Some text...";
}

💡 ملاحظة: يمكن لأي AutoShape يحتوي على TextFrame غير فارغ أن يعمل كمربع نص.

الوصول إلى مربعات النص حسب المحتوى

للعثور على جميع مربعات النص التي تحتوي على كلمة مفتاحية معينة (مثل “Slide”)، قم بالتكرار عبر الأشكال وتحقق من نصها:

public static void Access_TextBox()
{
    using var pres = new Presentation();

    foreach (var shape in pres.Slides[0].Shapes)
    {
        // Only AutoShapes can contain editable text
        if (shape is AutoShape autoShape)
        {
            if (autoShape.TextFrame.Text.Contains("Slide"))
            {
                // Do something with the matching text box
            }
        }
    }
}

إزالة مربعات النص حسب المحتوى

هذا المثال يحدد ويحذف جميع مربعات النص في الشريحة الأولى التي تحتوي على كلمة مفتاحية معينة:

public static void Remove_TextBox()
{
    using var pres = new Presentation();

    var shapesToRemove = pres.Slides[0].Shapes
        .Where(s => s is AutoShape autoShape && autoShape.TextFrame.Text.Contains("Slide"))
        .ToList();

    shapesToRemove.ForEach(shape => pres.Slides[0].Shapes.Remove(shape));
}

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