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

إبراز النص
استخدم الطريقة ITextFrame.highlightText عندما تحتاج إلى إبراز النص الذي يطابق عينة معينة داخل إطار النص. تطبق الطريقة لون إبراز على مقاطع النص المطابقة ويمكن استخدامها مع TextSearchOptions للتحكم في طريقة البحث، على سبيل المثال لمطابقة الكلمات الكاملة فقط.
مثال الشيفرة أدناه يبرز جميع تكرارات الأحرف “try” ثم يبرز الكلمة الكاملة “to” فقط.
Presentation presentation = new Presentation("sample.pptx");
try {
// احصل على الشكل الأول من الشريحة الأولى.
IAutoShape shape = (IAutoShape)presentation.getSlides().get_Item(0).getShapes().get_Item(0);
// إبراز الكلمة "try" في الشكل.
shape.getTextFrame().highlightText("try", Color.LIGHT_GRAY);
TextSearchOptions searchOptions = new TextSearchOptions();
searchOptions.setWholeWordsOnly(true);
// إبراز الكلمة "to" في الشكل.
shape.getTextFrame().highlightText("to", Color.MAGENTA, searchOptions, null);
presentation.save("highlighted_text.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
النتيجة:

إبراز النص باستخدام التعبيرات النمطية
الطريقة ITextFrame.highlightRegex تبرز التطابقات التي تجدها التعبيرات النمطية. في Java، يُعرَض هذا الـ API على ITextFrame.
مثال الشيفرة أدناه يبرز جميع الكلمات التي تحتوي على سبعة أحرف أو أكثر:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape shape = (IAutoShape)presentation.getSlides().get_Item(0).getShapes().get_Item(0);
java.util.regex.Pattern regex = java.util.regex.Pattern.compile("\\b[^\\s]{7,}\\b");
// إبراز جميع الكلمات التي تحتوي على سبعة أحرف أو أكثر.
shape.getTextFrame().highlightRegex(regex, Color.YELLOW, null);
presentation.save("highlighted_text_using_regex.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
النتيجة:

تعيين لون خلفية النص
استخدم IParagraphFormat.getDefaultPortionFormat لتعيين لون الإبراز الافتراضي لفقرة، أو استخدم IBasePortionFormat.getHighlightColor لأقسام النص الفردية.
مثال الشيفرة التالي يوضح كيفية تعيين لون الخلفية للـ فقرة كاملة:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).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();
}
النتيجة:

مثال الشيفرة أدناه يوضح كيفية تعيين لون الخلفية لـ أقسام النص ذات الخط الغامق:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).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 لتعيين محاذاة الفقرة داخل إطار النص. يمكن أن تكون القيم متمركزة، محاذاة إلى اليسار، إلى اليمين، مبررة، وما إلى ذلك.
مثال الشيفرة التالي يوضح كيفية محاذاة الفقرة إلى الوسط:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).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، وليس نسبة شفافية.
مثال الشيفرة أدناه يوضح كيفية تطبيق الشفافية على الفقرة الكاملة:
int alpha = 50;
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).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();
}
النتيجة:

مثال الشيفرة التالي يوضح كيفية تطبيق الشفافية على أقسام النص ذات الخط الغامق:
int alpha = 50;
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).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 لتوسيع أو تقليل التباعد بين الأحرف داخل صندوق النص.
مثال الشيفرة التالي يوضح كيفية توسيع تباعد الأحرف في الفقرة الكاملة:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).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();
}
النتيجة:

مثال الشيفرة أدناه يوضح كيفية توسيع تباعد الأحرف في أقسام النص ذات الخط الغامق:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).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 إلى قيمة أكبر بكثير من حجم الخط الفعلي:
Presentation presentation = new Presentation("presentation.pptx");
try {
IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).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 على جميع الأقسام في الفقرة.
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).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();
}
النتيجة:

مثال الشيفرة أدناه يطبق خصائص مشابهة على أقسام النص ذات الخط الغامق:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).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 درجة عكس عقرب الساعة:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setTextVerticalType(TextVerticalType.Vertical270);
presentation.save("text_rotation.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
النتيجة:

تعيين دوران مخصص لإطارات النص
استخدم ITextFrameFormat.setRotationAngle لتعيين زاوية دوران مخصصة لإطار نص ITextFrame.
مثال الشيفرة أدناه يدور إطار النص 3 درجات باتجاه عقرب الساعة داخل الشكل:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).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 للتحكم في تباعد الفقرات. تُستَخدم هذه الخصائص كما يلي:
- استخدم قيمة موجبة لتحديد تباعد السطر كنسبة مئوية من ارتفاع السطر.
- استخدم قيمة سالبة لتحديد تباعد السطر بالنقاط.
مثال الشيفرة التالي يوضح كيفية تحديد تباعد السطر داخل الفقرة:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).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 تحدد كيف يتصرف النص عندما يتجاوز حدود حاويته. استخدمها للتحكم فيما إذا كان سيصغر، يتجاوز أو يعيد تحجيم الشكل تلقائيًا.
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setAutofitType(TextAutofitType.Shape);
presentation.save("autofit_type.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
تعيين تثبيت إطارات النص
الطريقة ITextFrameFormat.setAnchoringType تحدد كيف يُموضع النص عموديًا داخل الشكل، مثلًا في الأعلى، الوسط أو الأسفل.
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Bottom);
presentation.save("text_anchor.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
تعيين تبويب النص
استخدم IParagraphFormat.setDefaultTabSize وIParagraphFormat.getTabs لتكوين نقاط التبويب في الفقرة.
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).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.
مثال الشيفرة التالي يوضح كيفية تعيين لغة التدقيق لقسم النص:
Presentation presentation = new Presentation("presentation.pptx");
try {
IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).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 لتعريف اللغة الافتراضية للنص الذي يُنشأ أثناء تحميل أو إنشاء عرض تقديمي.
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 نقطة لجميع النصوص عبر الشرائح في عرض تقديمي جديد.
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:
Presentation presentation = new Presentation("sample2.pptx");
try {
IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).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 وقم بتكوين نقاط التدرج، الاتجاه، والشفافية.