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

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

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

تعيين لون خلفية النص
استخدم ParagraphFormat.getDefaultPortionFormat لتعيين لون التمييز الافتراضي للفقرة، أو استخدم PortionFormat.getHighlightColor لأجزاء النص الفردية.
الكود التالي يُظهر كيفية تعيين لون الخلفية للـ فقرة كاملة:
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// تعيين لون التمييز للفقرة بأكملها.
paragraph.getParagraphFormat().getDefaultPortionFormat().getHighlightColor().setColor(java.getStaticFieldValue("java.awt.Color", "LIGHT_GRAY"));
presentation.save("gray_paragraph.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
النتيجة:

الكود التالي يوضح كيفية تعيين لون الخلفية لـ أجزاء النص ذات الخط العريض:
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const portions = paragraph.getPortions();
const portionCount = portions.getCount();
for (let portionIndex = 0; portionIndex < portionCount; portionIndex++) {
const portion = portions.get_Item(portionIndex);
if (portion.getPortionFormat().getEffective().getFontBold()) {
// تعيين لون التمييز لجزء النص.
portion.getPortionFormat().getHighlightColor().setColor(java.getStaticFieldValue("java.awt.Color", "LIGHT_GRAY"));
}
}
presentation.save("gray_text_portions.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
النتيجة:

محاذاة فقرات النص
استخدم ParagraphFormat.setAlignment لضبط محاذاة الفقرة داخل إطار النص. يمكن أن تكون القيمة مركزة، محاذاة إلى اليسار، محاذاة إلى اليمين، مبررة، وغيرها.
الكود التالي يوضح كيفية محاذاة الفقرة إلى الوسط:
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// تعيين محاذاة الفقرة إلى الوسط.
paragraph.getParagraphFormat().setAlignment(aspose.slides.TextAlignment.Center);
presentation.save("aligned_paragraph.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
النتيجة:

تعيين الشفافية للنص
تُتحكم شفافية النص من خلال المكوّن alfa للون المعين إلى PortionFormat.getFillFormat. في الأمثلة أدناه، alpha = 50 هو قيمة قناة alfa بنظام ARGB على مقياس 0‑255، وليس نسبة شفافية.
الكود التالي يوضح كيفية تطبيق الشفافية على الفقرة كاملة:
const alpha = 50;
const transparentBlack = java.newInstanceSync("java.awt.Color", 0, 0, 0, alpha);
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const fillFormat = paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat();
// تعيين لون ملء النص إلى لون شفاف.
fillFormat.setFillType(java.newByte(aspose.slides.FillType.Solid));
fillFormat.getSolidFillColor().setColor(transparentBlack);
presentation.save("transparent_paragraph.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
النتيجة:

الكود التالي يوضح كيفية تطبيق الشفافية على أجزاء النص ذات الخط العريض:
const alpha = 50;
const transparentBlack = java.newInstanceSync("java.awt.Color", 0, 0, 0, alpha);
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const portions = paragraph.getPortions();
const portionCount = portions.getCount();
for (let portionIndex = 0; portionIndex < portionCount; portionIndex++) {
const portion = portions.get_Item(portionIndex);
if (portion.getPortionFormat().getEffective().getFontBold()) {
const fillFormat = portion.getPortionFormat().getFillFormat();
// تعيين شفافية جزء النص.
fillFormat.setFillType(java.newByte(aspose.slides.FillType.Solid));
fillFormat.getSolidFillColor().setColor(transparentBlack);
}
}
presentation.save("transparent_text_portions.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
النتيجة:

تعيين تباعد الأحرف للنص
استخدم BasePortionFormat.setSpacing لتكبير أو تصغير التباعد بين الأحرف في صندوق النص.
الكود التالي يُظهر كيفية تكبير تباعد الأحرف في الفقرة كاملة:
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// ملاحظة: استخدم القيم السلبية لضغط تباعد الأحرف.
paragraph.getParagraphFormat().getDefaultPortionFormat().setSpacing(3); // توسيع تباعد الأحرف.
presentation.save("character_spacing_in_paragraph.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
النتيجة:

الكود التالي يوضح كيفية تكبير تباعد الأحرف في أجزاء النص ذات الخط العريض:
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const portions = paragraph.getPortions();
const portionCount = portions.getCount();
for (let portionIndex = 0; portionIndex < portionCount; portionIndex++) {
const portion = portions.get_Item(portionIndex);
if (portion.getPortionFormat().getEffective().getFontBold()) {
// ملاحظة: استخدم القيم السلبية لضغط تباعد الأحرف.
portion.getPortionFormat().setSpacing(3); // توسيع تباعد الأحرف.
}
}
presentation.save("character_spacing_in_text_portions.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
النتيجة:

تعطيل التقارب (Kerning) للخطوط المحددة
في بعض الحالات قد يظهر النص المُنشأ بواسطة Aspose.Slides أكثر ضيقًا قليلًا مقارنة بالنص المعروض في PowerPoint. يُمكن أن يحدث ذلك لأن PowerPoint قد يتجاهل بيانات الـ kerning لبعض الخطوط، حتى عندما يحتوي الخط على معلومات kerning صالحة ويتم تمكينها في إعدادات PowerPoint.
لجعل الإخراج المُنشأ أقرب إلى ما يولده PowerPoint في هذه الحالات، يمكنك تعطيل الـ kerning لأجزاء النص التي تستخدم الخط المتأثر. اضبط BasePortionFormat.setKerningMinimalSize إلى قيمة أكبر بكثير من حجم الخط الفعلي:
const presentation = new aspose.slides.Presentation("presentation.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraphs = autoShape.getTextFrame().getParagraphs();
const paragraphCount = paragraphs.getCount();
const targetFont = "Roboto";
for (let paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++) {
const portions = paragraphs.get_Item(paragraphIndex).getPortions();
const portionCount = portions.getCount();
for (let portionIndex = 0; portionIndex < portionCount; portionIndex++) {
const portion = portions.get_Item(portionIndex);
const portionFormat = portion.getPortionFormat();
const latinFont = portionFormat.getLatinFont();
const eastAsianFont = portionFormat.getEastAsianFont();
const complexScriptFont = portionFormat.getComplexScriptFont();
if ((latinFont !== null && latinFont.getFontName() === targetFont) ||
(eastAsianFont !== null && eastAsianFont.getFontName() === targetFont) ||
(complexScriptFont !== null && complexScriptFont.getFontName() === targetFont)) {
portionFormat.setKerningMinimalSize(100);
}
}
}
presentation.save("output.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
هذا الإعداد يمنع تطبيق الـ kerning على أجزاء النص المطابقة ويمكن أن يساعد في مطابقة مظهر Aspose.Slides مع مظهر PowerPoint للخطوط المتأثرة بهذا السلوك المحدد لبرنامج PowerPoint.
إدارة خصائص خط النص
يمكن تعيين خصائص الخط على مستوى الفقرة عبر ParagraphFormat.getDefaultPortionFormat أو على أجزاء منفردة عبر PortionFormat.
الكود التالي يعيّن الخط ونمط النص للفقرة بأكملها: يطبق حجم الخط، العريض، المائل، خط سفلي منقط، وخط Times New Roman على جميع الأجزاء في الفقرة.
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const defaultPortionFormat = paragraph.getParagraphFormat().getDefaultPortionFormat();
// تعيين خصائص الخط للفقرة.
defaultPortionFormat.setFontHeight(12);
defaultPortionFormat.setFontBold(java.newByte(aspose.slides.NullableBool.True));
defaultPortionFormat.setFontItalic(java.newByte(aspose.slides.NullableBool.True));
defaultPortionFormat.setFontUnderline(java.newByte(aspose.slides.TextUnderlineType.Dotted));
defaultPortionFormat.setLatinFont(new aspose.slides.FontData("Times New Roman"));
presentation.save("font_properties_for_paragraph.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
النتيجة:

الكود التالي يطبق خصائص مشابهة على أجزاء النص ذات الخط العريض:
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const portions = paragraph.getPortions();
const portionCount = portions.getCount();
for (let portionIndex = 0; portionIndex < portionCount; portionIndex++) {
const portion = portions.get_Item(portionIndex);
if (portion.getPortionFormat().getEffective().getFontBold()) {
const portionFormat = portion.getPortionFormat();
// تعيين خصائص الخط لجزء النص.
portionFormat.setFontHeight(13);
portionFormat.setFontItalic(java.newByte(aspose.slides.NullableBool.True));
portionFormat.setFontUnderline(java.newByte(aspose.slides.TextUnderlineType.Dotted));
portionFormat.setLatinFont(new aspose.slides.FontData("Times New Roman"));
}
}
presentation.save("font_properties_for_text_portions.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
النتيجة:

تعيين دوران النص
استخدم TextFrameFormat.setTextVerticalType لتعيين اتجاه نص مسبقًا داخل الشكل.
الكود التالي يعيّن اتجاه النص في الشكل إلى Vertical270، مما يدور النص 90 درجة عكس عقارب الساعة:
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setTextVerticalType(java.newByte(aspose.slides.TextVerticalType.Vertical270));
presentation.save("text_rotation.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
النتيجة:

تعيين دوران مخصص لإطارات النص
استخدم TextFrameFormat.setRotationAngle لتعيين زاوية دوران مخصصة لـ TextFrame.
الكود التالي يدور إطار النص بمقدار 3 درجات باتجاه عقارب الساعة داخل الشكل:
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setRotationAngle(3);
presentation.save("custom_text_rotation.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
النتيجة:

تعيين تباعد الأسطر للفقرات
توفر Aspose.Slides الطرق ParagraphFormat.setSpaceAfter، ParagraphFormat.setSpaceBefore، وParagraphFormat.setSpaceWithin للتحكم في تباعد الفقرات. تُستعمل هذه الخصائص كما يلي:
- استخدم قيمة موجبة لتحديد تباعد الأسطر كنسبة مئوية من ارتفاع السطر.
- استخدم قيمة سالبة لتحديد تباعد الأسطر بالنقاط.
الكود التالي يوضح كيفية تحديد تباعد الأسطر داخل الفقرة:
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getParagraphFormat().setSpaceWithin(200);
presentation.save("line_spacing.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
النتيجة:

تعيين نوع الملاءمة الذاتية لإطارات النص
تحدد الطريقة TextFrameFormat.setAutofitType كيفية تصرف النص عندما يتجاوز حدود حاويته. استخدمها للتحكم فيما إذا كان النص سيصغر، سيتخطى، أو سيعيد تحجيم الشكل تلقائيًا.
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setAutofitType(java.newByte(aspose.slides.TextAutofitType.Shape));
presentation.save("autofit_type.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
تعيين تثبيت إطارات النص
تحدد الطريقة TextFrameFormat.setAnchoringType كيفية وضع النص عموديًا داخل الشكل، مثلًا في الأعلى، الوسط، أو الأسفل.
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setAnchoringType(java.newByte(aspose.slides.TextAnchorType.Bottom));
presentation.save("text_anchor.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
تعيين تبويب النص
استخدم ParagraphFormat.setDefaultTabSize وParagraphFormat.getTabs لتكوين مسافات التبويب في الفقرة.
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getParagraphFormat().setDefaultTabSize(100);
paragraph.getParagraphFormat().getTabs().add(30, java.newByte(aspose.slides.TabAlignment.Left));
presentation.save("paragraph_tabs.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
النتيجة:

تعيين لغة التدقيق
توفر Aspose.Slides الطريقة PortionFormat.setLanguageId التي تسمح لك بتعيين لغة التدقيق لجزء النص. تحدد لغة التدقيق اللغة المستخدمة في فحص الإملاء والقواعد في PowerPoint.
الكود التالي يوضح كيفية تعيين لغة التدقيق لجزء النص:
const presentation = new aspose.slides.Presentation("presentation.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getPortions().clear();
const font = new aspose.slides.FontData("SimSun");
const textPortion = new aspose.slides.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", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
تعيين اللغة الافتراضية
استخدم LoadOptions.setDefaultTextLanguage لتحديد اللغة الافتراضية للنص الذي يُنشأ أثناء تحميل أو إنشاء عرض تقديمي.
const loadOptions = new aspose.slides.LoadOptions();
loadOptions.setDefaultTextLanguage("en-US");
const presentation = new aspose.slides.Presentation(loadOptions);
try {
const slide = presentation.getSlides().get_Item(0);
// إضافة شكل مستطيل جديد مع نص.
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 20, 20, 150, 50);
shape.getTextFrame().setText("Sample text");
// التحقق من لغة الجزء الأول.
const portion = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
console.log(portion.getPortionFormat().getLanguageId());
} finally {
presentation.dispose();
}
تعيين نمط النص الافتراضي
لتطبيق تنسيق نص افتراضي على مستوى العرض، استخدم Presentation.getDefaultTextStyle.
الكود التالي يوضح كيفية تعيين خط عريض بحجم 14 نقطة كخط افتراضي لكل النصوص عبر الشرائح في عرض تقديمي جديد.
const presentation = new aspose.slides.Presentation();
try {
// الحصول على تنسيق الفقرة في المستوى الأعلى.
const paragraphFormat = presentation.getDefaultTextStyle().getLevel(0);
if (paragraphFormat !== null) {
paragraphFormat.getDefaultPortionFormat().setFontHeight(14);
paragraphFormat.getDefaultPortionFormat().setFontBold(java.newByte(aspose.slides.NullableBool.True));
}
presentation.save("default_text_style.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
استخراج النص مع تأثير الحروف الكبيرة كليًا (All‑Caps)
في PowerPoint، تطبيق تأثير All Caps يجعل النص يظهر بأحرف كبيرة على الشريحة حتى لو تم كتابته أصلاً بأحرف صغيرة. عند استرجاع مثل هذا الجزء من النص باستخدام Aspose.Slides، تُعيد المكتبة النص كما تم إدخاله. لمطابقة النص المعروض، افحص TextCapType وحول السلسلة المرجعة إلى أحرف كبيرة عندما تكون القيمة All.
لنفترض أن لدينا صندوق النص التالي في الشريحة الأولى من ملف sample2.pptx.

الكود التالي يوضح كيفية استخراج النص مع تطبيق تأثير All Caps:
const presentation = new aspose.slides.Presentation("sample2.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const textPortion = autoShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
console.log("Original text: " + textPortion.getText());
const textFormat = textPortion.getPortionFormat().getEffective();
if (textFormat.getTextCapType() === aspose.slides.TextCapType.All) {
const text = textPortion.getText().toUpperCase();
console.log("All-Caps effect: " + text);
}
} finally {
presentation.dispose();
}
الإخراج:
Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!
الأسئلة الشائعة
كيف يمكن تعديل النص داخل جدول في شريحة؟
لتعديل النص داخل جدول في شريحة، استخدم Table. قم بالتكرار عبر الخلايا وحدث كل خلية عبر Cell.getTextFrame وتنسيق الفقرات عبر Paragraph.getParagraphFormat.
كيف يمكن تطبيق تدرج لوني على النص في شريحة PowerPoint؟
لتطبيق تدرج لوني على النص، استخدم PortionFormat.getFillFormat. عيّن FillFormat.setFillType إلى FillType.Gradient وقم بتكوين نقاط التدرج، الاتجاه، والشفافية.