تنسيق نص العرض التقديمي على Android

نظرة عامة

هذه المقالة تُظهر كيفية تنسيق النص في عروض PowerPoint وOpenDocument باستخدام Aspose.Slides لنظام Android عبر Java. تغطي ألوان الخلفية، الشفافية، تباعد الأحرف، خصائص الخط، التدوير، تباعد الفقرات، سلوك الملاءمة التلقائية، تثبيت النص، مواضع علامات التبويب، وإعدادات اللغة.

في الأمثلة أدناه، سنستخدم ملفًا اسمه “sample.pptx” يحتوي على مربع نص واحد في الشريحة الأولى بالنص التالي:

نص عينة

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

تعيين لون خلفية النص

استخدم IParagraphFormat.getDefaultPortionFormat لتعيين لون التحديد الافتراضي لفقرة، أو استخدم IBasePortionFormat.getHighlightColor لتحديد لون التحديد لأجزاء النص الفردية.

يوضح المثال البرمجي التالي كيفية تعيين لون الخلفية للفقرة كاملة:

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation("sample.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    // عيّن لون التحديد للفقرة بأكملها.
    paragraph.getParagraphFormat().getDefaultPortionFormat().getHighlightColor().setColor(Color.LTGRAY);

    presentation.save("gray_paragraph.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

النتيجة:

الفقرة الرمادية

يوضح المثال البرمجي أدناه كيفية تعيين لون الخلفية لأجزاء النص ذات الخط الغامق:

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation("sample.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    for (IPortion portion : paragraph.getPortions()) {
        if (portion.getPortionFormat().getEffective().getFontBold()) {
            // عيّن لون التحديد لجزء النص.
            portion.getPortionFormat().getHighlightColor().setColor(Color.LTGRAY);
        }
    }

    presentation.save("gray_text_portions.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

النتيجة:

أجزاء النص الرمادية

محاذاة فقرات النص

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

يوضح المثال البرمجي التالي كيفية محاذاة الفقرة إلى المركز:

import com.aspose.slides.*;

Presentation presentation = new Presentation("sample.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    // عيّن محاذاة الفقرة إلى المركز.
    paragraph.getParagraphFormat().setAlignment(TextAlignment.Center);

    presentation.save("aligned_paragraph.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

النتيجة:

الفقرة المحاذاة

تعيين الشفافية للنص

تُتحكم شفافية النص من خلال مكوّن ألفا للون المعين إلى IBasePortionFormat.getFillFormat. في الأمثلة أدناه، alpha = 50 هو قيمة قناة ألفا بنظام ARGB على مقياس 0–255، وليس نسبة شفافية.

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

import com.aspose.slides.*;
import android.graphics.Color;

int alpha = 50;

Presentation presentation = new Presentation("sample.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    // عيّن لون تعبئة النص إلى لون شفاف.
    paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
    paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.argb(alpha, 0, 0, 0));

    presentation.save("transparent_paragraph.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

النتيجة:

الفقرة الشفافة

يوضح المثال البرمجي التالي كيفية تطبيق الشفافية على أجزاء النص ذات الخط الغامق:

import com.aspose.slides.*;
import android.graphics.Color;

int alpha = 50;

Presentation presentation = new Presentation("sample.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    for (IPortion portion : paragraph.getPortions()) {
        if (portion.getPortionFormat().getEffective().getFontBold()) {
            // عيّن شفافية جزء النص.
            portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
            portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.argb(alpha, 0, 0, 0));
        }
    }

    presentation.save("transparent_text_portions.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

النتيجة:

أجزاء النص الشفافة

تعيين تباعد الأحرف للنص

استخدم IBasePortionFormat.setSpacing لتوسيع أو تضييق التباعد بين الأحرف في إطار نص.

يوضح كود Java التالي كيفية توسيع تباعد الأحرف في فقرة كاملة:

import com.aspose.slides.*;

Presentation presentation = new Presentation("sample.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    // ملاحظة: استخدم قيمًا سالبة لضغط تباعد الأحرف.
    paragraph.getParagraphFormat().getDefaultPortionFormat().setSpacing(3); // وسع تباعد الأحرف.

    presentation.save("character_spacing_in_paragraph.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

النتيجة:

تباعد الأحرف في الفقرة

يوضح المثال البرمجي أدناه كيفية توسيع تباعد الأحرف في أجزاء النص ذات الخط الغامق:

import com.aspose.slides.*;

Presentation presentation = new Presentation("sample.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    for (IPortion portion : paragraph.getPortions()) {
        if (portion.getPortionFormat().getEffective().getFontBold()) {
            // ملاحظة: استخدم قيمًا سالبة لضغط تباعد الأحرف.
            portion.getPortionFormat().setSpacing(3); // وسّع تباعد الأحرف.
        }
    }

    presentation.save("character_spacing_in_text_portions.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

النتيجة:

تباعد الأحرف في أجزاء النص

تعطيل التآزر للخطوط المحددة

في بعض الحالات، قد يبدو النص المُظهر بواسطة Aspose.Slides أكثر تضييقًا قليلًا مقارنةً بالنص نفسه في PowerPoint. قد يحدث هذا لأن PowerPoint قد يتجاهل بيانات التآزر لبعض الخطوط، حتى عندما يحتوي الخط على معلومات تآزر صالحة وتكون خاصية التآزر مفعلة في إعدادات PowerPoint.

لجعل الناتج المُظهر أقرب إلى ما يعرضه PowerPoint في مثل هذه الحالات، يمكنك تعطيل التآزر لأجزاء النص التي تستخدم الخط المتأثر. اضبط IBasePortionFormat.setKerningMinimalSize إلى قيمة أكبر بكثير من حجم الخط الفعلي:

import com.aspose.slides.*;

Presentation presentation = new Presentation("presentation.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    String targetFont = "Roboto";

    for (IParagraph paragraph : autoShape.getTextFrame().getParagraphs()) {
        for (IPortion portion : paragraph.getPortions()) {
            IPortionFormat portionFormat = portion.getPortionFormat();

            if ((portionFormat.getLatinFont() != null &&
                 portionFormat.getLatinFont().getFontName().equals(targetFont)) ||
                (portionFormat.getEastAsianFont() != null &&
                 portionFormat.getEastAsianFont().getFontName().equals(targetFont)) ||
                (portionFormat.getComplexScriptFont() != null &&
                 portionFormat.getComplexScriptFont().getFontName().equals(targetFont))) {
                portionFormat.setKerningMinimalSize(100);
            }
        }
    }

    presentation.save("output.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

هذه الإعدادات تمنع تطبيق التآزر على أجزاء النص المطابقة ويمكن أن تساعد في توافق عرض Aspose.Slides مع مظهر PowerPoint للخطوط التي تتأثر بهذا السلوك الخاص بـ PowerPoint.

إدارة خصائص خط النص

يمكن تعيين خصائص الخط على مستوى الفقرة عبر IParagraphFormat.getDefaultPortionFormat أو على أجزاء فردية عبر IPortionFormat.

يوضح الكود التالي كيفية تعيين الخط ونمط النص للفقرة كاملة: يطبق حجم الخط، الخط الغامق، المائل، التسطير المنقط، وخط Times New Roman على جميع الأجزاء داخل الفقرة.

import com.aspose.slides.*;

Presentation presentation = new Presentation("sample.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    // عيّن خصائص الخط للفقرة.
    paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(12);
    paragraph.getParagraphFormat().getDefaultPortionFormat().setFontBold(NullableBool.True);
    paragraph.getParagraphFormat().getDefaultPortionFormat().setFontItalic(NullableBool.True);
    paragraph.getParagraphFormat().getDefaultPortionFormat().setFontUnderline(TextUnderlineType.Dotted);
    paragraph.getParagraphFormat().getDefaultPortionFormat().setLatinFont(new FontData("Times New Roman"));

    presentation.save("font_properties_for_paragraph.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

النتيجة:

خصائص الخط للفقرة

يوضح المثال البرمجي أدناه تطبيق خصائص مماثلة على أجزاء النص ذات الخط الغامق:

import com.aspose.slides.*;

Presentation presentation = new Presentation("sample.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    for (IPortion portion : paragraph.getPortions()) {
        if (portion.getPortionFormat().getEffective().getFontBold()) {
            // عيّن خصائص الخط لجزء النص.
            portion.getPortionFormat().setFontHeight(13);
            portion.getPortionFormat().setFontItalic(NullableBool.True);
            portion.getPortionFormat().setFontUnderline(TextUnderlineType.Dotted);
            portion.getPortionFormat().setLatinFont(new FontData("Times New Roman"));
        }
    }

    presentation.save("font_properties_for_text_portions.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

النتيجة:

خصائص الخط لأجزاء النص

تعيين تدوير النص

استخدم ITextFrameFormat.setTextVerticalType لتعيين اتجاه نص مُعرّف مسبقًا داخل الشكل.

يوضح المثال البرمجي التالي كيفية تعيين اتجاه النص في الشكل إلى TextVerticalType.Vertical270، مما يدور النص 90 درجة عكس عقارب الساعة:

import com.aspose.slides.*;

Presentation presentation = new Presentation("sample.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);

    autoShape.getTextFrame().getTextFrameFormat().setTextVerticalType(TextVerticalType.Vertical270);

    presentation.save("text_rotation.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

النتيجة:

تدوير النص

تعيين تدوير مخصص لإطارات النص

استخدم ITextFrameFormat.setRotationAngle لتعيين زاوية دوران مخصصة لإطار نص ITextFrame.

يقوم المثال البرمجي أدناه بتدوير إطار النص بزاوية 3 درجات باتجاه عقارب الساعة داخل الشكل:

import com.aspose.slides.*;

Presentation presentation = new Presentation("sample.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);

    autoShape.getTextFrame().getTextFrameFormat().setRotationAngle(3);

    presentation.save("custom_text_rotation.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

النتيجة:

التدوير المخصص للنص

تعيين تباعد الأسطر للفقرات

توفر Aspose.Slides الطرق IParagraphFormat.setSpaceAfter، IParagraphFormat.setSpaceBefore، وIParagraphFormat.setSpaceWithin للتحكم في تباعد الفقرات. تُستَخدم هذه الخصائص كما يلي:

  • استخدم قيمة موجبة لتحديد تباعد سطر كنسبة مئوية من ارتفاع السطر.
  • استخدم قيمة سالبة لتحديد تباعد السطر بالنقاط.

يوضح المثال البرمجي التالي كيفية تحديد تباعد الأسطر داخل الفقرة:

import com.aspose.slides.*;

Presentation presentation = new Presentation("sample.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    paragraph.getParagraphFormat().setSpaceWithin(200);

    presentation.save("line_spacing.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

النتيجة:

تباعد الأسطر داخل الفقرة

تعيين نوع الملاءمة التلقائية لإطارات النص

يحدد ITextFrameFormat.setAutofitType سلوك النص عندما يتجاوز حدود حاويته. استخدمه للتحكم فيما إذا كان النص ينكمش، يفيض، أو يعيد تحجيم الشكل تلقائيًا.

import com.aspose.slides.*;

Presentation presentation = new Presentation("sample.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);

    autoShape.getTextFrame().getTextFrameFormat().setAutofitType(TextAutofitType.Shape);

    presentation.save("autofit_type.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

لحساب عدد الأسطر بعد الالتفاف التلقائي ورؤية كيف يتغير عرض النص أو الشكل نتيجةً لذلك، راجع عدد الأسطر المُرسَمة. عدد الأسطر وحده لا يُظهر ما إذا كان النص يفيض عن حاويته.

تعيين تثبيت إطارات النص

يُعرّف ITextFrameFormat.setAnchoringType كيفية تموضع النص عموديًا داخل الشكل، مثلاً في الأعلى، الوسط، أو الأسفل.

import com.aspose.slides.*;

Presentation presentation = new Presentation("sample.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);

    autoShape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Bottom);

    presentation.save("text_anchor.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

تعيين مسافات التبويب للنص

استخدم IParagraphFormat.setDefaultTabSize وIParagraphFormat.getTabs لتكوين مواضع علامات التبويب في الفقرة.

import com.aspose.slides.*;

Presentation presentation = new Presentation("sample.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    paragraph.getParagraphFormat().setDefaultTabSize(100);
    paragraph.getParagraphFormat().getTabs().add(30, TabAlignment.Left);

    presentation.save("paragraph_tabs.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

النتيجة:

علامات تبويب الفقرة

تعيين لغة التدقيق

توفر Aspose.Slides الطريقة IBasePortionFormat.setLanguageId التي تسمح بتعيين لغة التدقيق لجزء النص. تحدد لغة التدقيق اللغة المستخدمة لتدقيق الإملاء والقواعد في PowerPoint.

يوضح المثال البرمجي التالي كيفية تعيين لغة التدقيق لجزء نص:

import com.aspose.slides.*;

Presentation presentation = new Presentation("presentation.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);

    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
    paragraph.getPortions().clear();

    FontData font = new FontData("SimSun");

    Portion textPortion = new Portion();
    textPortion.getPortionFormat().setComplexScriptFont(font);
    textPortion.getPortionFormat().setEastAsianFont(font);
    textPortion.getPortionFormat().setLatinFont(font);

    // عيّن معرّف لغة التدقيق.
    textPortion.getPortionFormat().setLanguageId("zh-CN");

    textPortion.setText("1。");
    paragraph.getPortions().add(textPortion);

    presentation.save("proofing_language.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

تعيين اللغة الافتراضية

استخدم LoadOptions.setDefaultTextLanguage لتحديد اللغة الافتراضية للنص المُنشأ أثناء تحميل أو إنشاء عرض تقديمي.

import com.aspose.slides.*;

LoadOptions loadOptions = new LoadOptions();
loadOptions.setDefaultTextLanguage("en-US");

Presentation presentation = new Presentation(loadOptions);
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    // أضف شكلاً مستطيلاً جديدًا مع نص.
    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 20, 150, 50);
    shape.getTextFrame().setText("Sample text");

    // تحقق من لغة الجزء الأول.
    IPortion portion = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
    System.out.println(portion.getPortionFormat().getLanguageId());
} finally {
    presentation.dispose();
}

تعيين نمط النص الافتراضي

لتطبيق تنسيق نص افتراضي على مستوى العرض التقديمي، استخدم IPresentation.getDefaultTextStyle.

يوضح المثال البرمجي التالي كيفية تعيين خط غامق افتراضي بحجم 14 نقطة لجميع النصوص عبر الشرائح في عرض تقديمي جديد.

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    // احصل على تنسيق الفقرة من المستوى الأعلى.
    IParagraphFormat paragraphFormat = presentation.getDefaultTextStyle().getLevel(0);

    if (paragraphFormat != null) {
        paragraphFormat.getDefaultPortionFormat().setFontHeight(14);
        paragraphFormat.getDefaultPortionFormat().setFontBold(NullableBool.True);
    }

    presentation.save("default_text_style.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

استخراج النص بتأثير الأحرف الكبيرة

في PowerPoint، يؤدي تطبيق تأثير All Caps إلى ظهور النص بأحرف كبيرة على الشريحة حتى لو كان مكتوبًا أصلاً بأحرف صغيرة. عند استرداد مثل هذا الجزء من النص باستخدام Aspose.Slides، تُعيد المكتبة النص كما تم إدخاله تمامًا. لمطابقة النص المعروض، حوّل السلسلة المسترجعة إلى أحرف كبيرة عندما تكون القيمة TextCapType.All.

لنفترض أن لدينا مربع النص التالي في الشريحة الأولى من ملف sample2.pptx.

تأثير الأحرف الكبيرة

يوضح المثال البرمجي أدناه كيفية استخراج النص مع تطبيق تأثير All Caps:

import com.aspose.slides.*;

Presentation presentation = new Presentation("sample2.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IPortion textPortion = autoShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);

    System.out.println("Original text: " + textPortion.getText());

    IPortionFormatEffectiveData textFormat = textPortion.getPortionFormat().getEffective();
    if (textFormat.getTextCapType() == TextCapType.All) {
        String text = textPortion.getText().toUpperCase();
        System.out.println("All-Caps effect: " + text);
    }
} finally {
    presentation.dispose();
}

الإخراج:

Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!

الأسئلة الشائعة

كيف تعديل النص في جدول على الشريحة؟

لتعديل النص في جدول على الشريحة، استخدم ITable. استعرض الخلايا وقم بتحديث كل خلية عبر ICell.getTextFrame وتنسيق الفقرات عبر IParagraph.getParagraphFormat.

كيف تطبيق لون متدرج للنص في شريحة PowerPoint؟

لتطبيق لون متدرج على النص، استخدم IBasePortionFormat.getFillFormat. اضبط IFillFormat.setFillType إلى FillType.Gradient وقم بتكوين نقاط التدرج، الاتجاه، والشفافية.