تنسيق نص العرض التقديمي في Java

نظرة عامة

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

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

نص العينة

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

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

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

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

import com.aspose.slides.*;
import java.awt.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.LIGHT_GRAY);

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

النتيجة:

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

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

import com.aspose.slides.*;
import java.awt.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.LIGHT_GRAY);
        }
    }

    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 java.awt.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(new Color(0, 0, 0, alpha));

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

النتيجة:

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

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

import com.aspose.slides.*;
import java.awt.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(new Color(0, 0, 0, alpha));
        }
    }

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

النتيجة:

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

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

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

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

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 لتعيين اتجاه نص مسبق داخل الشكل.

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