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

للعثور على نص حرفي أو مطابقة تعبيرٍ نمطي وتظليله، انظر بحث واستبدال النص.
تعيين لون خلفية النص
استخدم ParagraphFormat.getDefaultPortionFormat لتعيين لون التظليل الافتراضي لفقرة، أو استخدم BasePortionFormat.getHighlightColor لأجزاء النص الفردية.
يوضح مثال الشيفرة التالي كيفية تعيين لون الخلفية للفقرة كاملة:
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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 aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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 aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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();
}
النتيجة:

تعيين شفافية النص
يتم التحكم في شفافية النص من خلال المكوّن ألفا للون المعين إلى BasePortionFormat.getFillFormat. في الأمثلة أدناه، alpha = 50 هو قيمة قناة ألفا ARGB على مقياس 0–255، وليس نسبة شفافية.
يوضح مثال الشيفرة أدناه كيفية تطبيق الشفافية للفقرة كاملة:
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const alpha = 50;
const transparentBlack = java.newInstanceSync("java.awt.Color", 0, 0, 0, alpha);
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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 aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const alpha = 50;
const transparentBlack = java.newInstanceSync("java.awt.Color", 0, 0, 0, alpha);
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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 لتوسيع أو تضييق التباعد بين الأحرف في صندوق نص.
يُظهر كود JavaScript التالي كيفية توسيع تباعد الأحرف في الفقرة كاملة:
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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 aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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();
}
النتيجة:

تعطيل الكيرنينغ للخطوط المحددة
في بعض الحالات، قد يظهر النص المُعالج بواسطة Aspose.Slides ضيقًا قليلًا مقارنةً بالنص نفسه في PowerPoint. يمكن أن يحدث هذا لأن PowerPoint قد يتجاهل بيانات الكيرنينغ لبعض الخطوط، حتى وإن كان الخط يحتوي على معلومات كيرنينغ صالحة وكان الكيرنينغ مُفعّلاً في إعدادات PowerPoint.
لجعل الناتج المُعالج أقرب إلى PowerPoint في هذه الحالات، يمكنك تعطيل الكيرنينغ لأجزاء النص التي تستخدم الخط المتأثر. اضبط BasePortionFormat.setKerningMinimalSize إلى قيمة أكبر بكثير من حجم الخط الفعلي:
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("presentation.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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();
}
هذا الإعداد يمنع تطبيق الكيرنينغ على الأجزاء المتطابقة ويمكن أن يساعد في مواءمة عرض Aspose.Slides مع المخرجات البصرية لـ PowerPoint للخطوط المتأثرة بهذا السلوك الخاص بـ PowerPoint.
إدارة خصائص خط النص
يمكن تعيين خصائص الخط على مستوى الفقرة عبر ParagraphFormat.getDefaultPortionFormat أو على الأجزاء الفردية عبر PortionFormat.
يقوم الكود التالي بتعيين الخط ونمط النص للفقرة كاملة: يطبق حجم الخط، العريض، المائل، الخط السفلي المنقط، وخط Times New Roman على جميع الأجزاء في الفقرة.
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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 aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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 aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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 aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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 aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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 aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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 aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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 aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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 BasePortionFormat.setLanguageId، التي تتيح لك تعيين لغة التدقيق لجزء نص. تحدد لغة التدقيق اللغة المستخدمة في تدقيق الإملاء والقواعد في PowerPoint.
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("presentation.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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 aspose = { slides: require("aspose.slides.via.java") };
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 aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
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();
}
استخراج النص مع تأثير الحروف الكبيرة
في PowerPoint، يجعل تطبيق تأثير All Caps على الخط النص يظهر بأحرف كبيرة على الشريحة حتى لو كان مكتوبًا أصلاً بأحرف صغيرة. عند استرجاع مثل هذا الجزء النصي باستخدام Aspose.Slides، تُعيد المكتبة النص كما تم إدخاله. لتطابق النص المعروض، افحص TextCapType وحوّل السلسلة المسترجعة إلى أحرف كبيرة عندما تكون القيمة All.
لنفترض أن لدينا صندوق النص التالي في الشريحة الأولى من ملف sample2.pptx.

يوضح مثال الشيفرة التالي كيفية استخراج النص مع تطبيق تأثير All Caps:
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("sample2.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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؟
لتطبيق لون متدرج على النص، استخدم BasePortionFormat.getFillFormat. اضبط FillFormat.setFillType إلى FillType.Gradient وقم بتكوين نقاط التدرج، الاتجاه، والشفافية.