إدارة فقرات نص PowerPoint في JavaScript

نظرة عامة

Aspose.Slides for Node.js via Java يمثل النص كهرمية من إطارات النص، الفقرات، والجزء:

  • TextFrame يمثل حاوية النص داخل الشكل ويوفر وصولًا إلى مجموعة الفقرات الخاصة به.
  • Paragraph يمثل فقرة واحدة داخل إطار النص ويوفر وصولًا إلى أجزاءها وتنسيق المستوى الفقري.
  • Portion يمثل تشغيل نص داخل الفقرة. يمكن لكل جزء أن يمتلك نصه وتنسيق الأحرف الخاص به.

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

إنشاء وتنسيق الفقرات

إنشاء فقرات مع عدة أجزاء

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

  1. إنشاء نسخة من الفئة Presentation.
  2. الوصول إلى الشريحة ذات الصلة عبر الفهرس الخاص بها.
  3. إضافة AutoShape مستطيل إلى الشريحة.
  4. الوصول إلى TextFrame الخاص بالشكل.
  5. استخدام الفقرة الافتراضية وإضافة كائنين آخرين من الفئة Paragraph إلى إطار النص.
  6. إضافة عدد كافٍ من كائنات Portion لكل فقرة لتحتوي على ثلاثة أجزاء. الفقرة الافتراضية تحتوي بالفعل على جزء فارغ واحد.
  7. تعيين نص كل جزء.
  8. تطبيق تنسيق على مستوى الأحرف عبر Portion.getPortionFormat.
  9. حفظ العرض التقديمي المعدل.

يُظهر المثال التالي بلغة JavaScript تنفيذ هذه الخطوات:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);
    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 50, 150, 300, 150);
    const textFrame = shape.getTextFrame();

    const firstParagraph = textFrame.getParagraphs().get_Item(0);
    firstParagraph.getPortions().add(new aspose.slides.Portion());
    firstParagraph.getPortions().add(new aspose.slides.Portion());

    const secondParagraph = new aspose.slides.Paragraph();
    secondParagraph.getPortions().add(new aspose.slides.Portion());
    secondParagraph.getPortions().add(new aspose.slides.Portion());
    secondParagraph.getPortions().add(new aspose.slides.Portion());
    textFrame.getParagraphs().add(secondParagraph);

    const thirdParagraph = new aspose.slides.Paragraph();
    thirdParagraph.getPortions().add(new aspose.slides.Portion());
    thirdParagraph.getPortions().add(new aspose.slides.Portion());
    thirdParagraph.getPortions().add(new aspose.slides.Portion());
    textFrame.getParagraphs().add(thirdParagraph);

    const paragraphCount = textFrame.getParagraphs().getCount();
    for (let paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++) {
        const paragraph = textFrame.getParagraphs().get_Item(paragraphIndex);
        const portionCount = paragraph.getPortions().getCount();
        for (let portionIndex = 0; portionIndex < portionCount; portionIndex++) {
            const portion = paragraph.getPortions().get_Item(portionIndex);
            portion.setText("Portion " + (paragraphIndex + 1) + "." + (portionIndex + 1));

            if (portionIndex === 0) {
                portion.getPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
                portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "RED"));
                portion.getPortionFormat().setFontBold(java.newByte(aspose.slides.NullableBool.True));
                portion.getPortionFormat().setFontHeight(15);
            } else if (portionIndex === 1) {
                portion.getPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
                portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLUE"));
                portion.getPortionFormat().setFontItalic(java.newByte(aspose.slides.NullableBool.True));
                portion.getPortionFormat().setFontHeight(18);
            }
        }
    }

    presentation.save("paragraphs_with_portions.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

إنشاء القوائم ذات النقاط والترقيم

إنشاء قائمة ذات نقاط أو ترقيم

تُسهل النقاط والترقيم مسح العناصر ذات الصلة. في Aspose.Slides يتم تعريف إعدادات القائمة عبر BulletFormat.

  1. إنشاء نسخة من الفئة Presentation.
  2. الوصول إلى الشريحة ذات الصلة عبر الفهرس الخاص بها.
  3. إضافة AutoShape إلى الشريحة المحددة.
  4. الوصول إلى TextFrame الخاص بالشكل.
  5. إزالة الفقرة الافتراضية من إطار النص.
  6. إنشاء Paragraph لنقطة الرمز.
  7. تعيين BulletFormat.setType إلى BulletType.Symbol وتحديد حرف الرمز.
  8. تعيين نص الفقرة، والمسافة البادئة، ولون الرمز، وارتفاعه.
  9. إضافة الفقرة إلى إطار النص.
  10. إنشاء فقرة ثانية وتعيين BulletFormat.setType إلى BulletType.Numbered.
  11. ضبط نمط الترقيم وإضافة الفقرة إلى إطار النص.
  12. حفظ العرض التقديمي.

يُظهر المثال التالي بلغة JavaScript إنشاء رمز نقطي ورقمي:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);
    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 200, 200, 400, 200);
    const textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    const symbolParagraph = new aspose.slides.Paragraph();
    symbolParagraph.setText("Welcome to Aspose.Slides");
    symbolParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
    symbolParagraph.getParagraphFormat().getBullet().setChar(java.newChar(0x2022));
    symbolParagraph.getParagraphFormat().setIndent(25);
    symbolParagraph.getParagraphFormat().getBullet().getColor().setColorType(aspose.slides.ColorType.RGB);
    symbolParagraph.getParagraphFormat().getBullet().getColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    symbolParagraph.getParagraphFormat().getBullet().setBulletHardColor(java.newByte(aspose.slides.NullableBool.True));
    symbolParagraph.getParagraphFormat().getBullet().setHeight(100);
    textFrame.getParagraphs().add(symbolParagraph);

    const numberedParagraph = new aspose.slides.Paragraph();
    numberedParagraph.setText("This is a numbered item");
    numberedParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Numbered));
    numberedParagraph.getParagraphFormat().getBullet().setNumberedBulletStyle(java.newByte(aspose.slides.NumberedBulletStyle.BulletCircleNumWDBlackPlain));
    numberedParagraph.getParagraphFormat().setIndent(25);
    numberedParagraph.getParagraphFormat().getBullet().getColor().setColorType(aspose.slides.ColorType.RGB);
    numberedParagraph.getParagraphFormat().getBullet().getColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    numberedParagraph.getParagraphFormat().getBullet().setBulletHardColor(java.newByte(aspose.slides.NullableBool.True));
    numberedParagraph.getParagraphFormat().getBullet().setHeight(100);
    textFrame.getParagraphs().add(numberedParagraph);

    presentation.save("bulleted_and_numbered_list.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

استخدام نقاط بصورة

تتيح نقاط الصورة استخدام صورة مخصصة بدلاً من رمز أو رقم.

  1. إنشاء نسخة من الفئة Presentation.
  2. الوصول إلى الشريحة ذات الصلة عبر الفهرس الخاص بها.
  3. إضافة AutoShape والوصول إلى TextFrame الخاص به.
  4. إزالة الفقرة الافتراضية من إطار النص.
  5. تحميل صورة النقطة وإضافتها إلى مجموعة صور العرض التقديمي ككائن PPImage.
  6. إنشاء Paragraph وتعيين نصه.
  7. تعيين BulletFormat.setType إلى BulletType.Picture.
  8. ربط الصورة عبر BulletFormat.getPicture وتعيين ارتفاع النقطة.
  9. إضافة الفقرة إلى إطار النص.
  10. حفظ العرض التقديمي المعدل.

يُظهر المثال التالي بلغة JavaScript إنشاء نقطة بصورة:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const bulletImage = aspose.slides.Images.fromFile("image.png");
    let presentationImage;
    try {
        presentationImage = presentation.getImages().addImage(bulletImage);
    } finally {
        bulletImage.dispose();
    }

    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 200, 200, 400, 200);
    const textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    const paragraph = new aspose.slides.Paragraph();
    paragraph.setText("Welcome to Aspose.Slides");
    paragraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Picture));
    paragraph.getParagraphFormat().getBullet().getPicture().setImage(presentationImage);
    paragraph.getParagraphFormat().getBullet().setHeight(100);
    textFrame.getParagraphs().add(paragraph);

    presentation.save("picture_bullet.pptx", aspose.slides.SaveFormat.Pptx);
    presentation.save("picture_bullet.ppt", aspose.slides.SaveFormat.Ppt);
} finally {
    presentation.dispose();
}

إنشاء قائمة متعددة المستويات

اضبط ParagraphFormat.setDepth لوضع الفقرات على مستويات مختلفة من القائمة. المستوى الأعلى له عمق 0.

  1. إنشاء Presentation والوصول إلى شريحة.
  2. إضافة AutoShape وإزالة الفقرة الافتراضية من إطار النص الخاص به.
  3. إنشاء أربع فقرات وضبط رموز النقاط الخاصة بها.
  4. تعيين قيم ParagraphFormat.setDepth إلى 0، 1، 2، و3.
  5. إضافة الفقرات إلى إطار النص وحفظ العرض التقديمي.

يُظهر المثال التالي بلغة JavaScript إنشاء قائمة نقطية ذات أربعة مستويات:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);
    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 200, 200, 400, 200);
    const textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    const firstParagraph = new aspose.slides.Paragraph();
    firstParagraph.setText("Content");
    firstParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
    firstParagraph.getParagraphFormat().getBullet().setChar(java.newChar(0x2022));
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    firstParagraph.getParagraphFormat().setDepth(java.newShort(0));

    const secondParagraph = new aspose.slides.Paragraph();
    secondParagraph.setText("Second level");
    secondParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
    secondParagraph.getParagraphFormat().getBullet().setChar(java.newChar(45));
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    secondParagraph.getParagraphFormat().setDepth(java.newShort(1));

    const thirdParagraph = new aspose.slides.Paragraph();
    thirdParagraph.setText("Third level");
    thirdParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
    thirdParagraph.getParagraphFormat().getBullet().setChar(java.newChar(0x2022));
    thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    thirdParagraph.getParagraphFormat().setDepth(java.newShort(2));

    const fourthParagraph = new aspose.slides.Paragraph();
    fourthParagraph.setText("Fourth level");
    fourthParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
    fourthParagraph.getParagraphFormat().getBullet().setChar(java.newChar(45));
    fourthParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    fourthParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    fourthParagraph.getParagraphFormat().setDepth(java.newShort(3));

    textFrame.getParagraphs().add(firstParagraph);
    textFrame.getParagraphs().add(secondParagraph);
    textFrame.getParagraphs().add(thirdParagraph);
    textFrame.getParagraphs().add(fourthParagraph);

    presentation.save("multilevel_list.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

بدء ترقيم العناصر بأرقام مخصصة

استخدم BulletFormat.setNumberedBulletStartWith لتعيين الرقم الأولي المعروض لفقرة مُرقَّمة.

  1. إنشاء Presentation وإضافة AutoShape إلى شريحة.
  2. مسح الفقرة الافتراضية من إطار النص الخاص بالشكل.
  3. إنشاء ثلاث فقرات مُرقَّمة.
  4. تعيين BulletFormat.setNumberedBulletStartWith إلى 2، 3، و7 لكل فقرة على التوالي.
  5. إضافة الفقرات إلى إطار النص وحفظ العرض التقديمي.

يُظهر المثال التالي بلغة JavaScript تعيين رقم بدء مخصص لكل فقرة:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);
    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 200, 200, 400, 200);
    const textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    const firstParagraph = new aspose.slides.Paragraph();
    firstParagraph.setText("Start at 2");
    firstParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Numbered));
    firstParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(java.newShort(2));
    textFrame.getParagraphs().add(firstParagraph);

    const secondParagraph = new aspose.slides.Paragraph();
    secondParagraph.setText("Start at 3");
    secondParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Numbered));
    secondParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(java.newShort(3));
    textFrame.getParagraphs().add(secondParagraph);

    const thirdParagraph = new aspose.slides.Paragraph();
    thirdParagraph.setText("Start at 7");
    thirdParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Numbered));
    thirdParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(java.newShort(7));
    textFrame.getParagraphs().add(thirdParagraph);

    presentation.save("custom_numbered_list.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

التحكم في تخطيط الفقرة وخصائص النهاية

تعيين إزاحة السطر الأول

استخدم ParagraphFormat.setIndent للتحكم في إزاحة السطر الأول من الفقرة. هذه الطريقة تحرك السطر الأول فقط بالنسبة للهوامش اليسرى للفقرة. القيمة الإيجابية تحرك السطر الأول إلى اليمين، بينما تبقى الأسطر المتبقية محاذاة إلى جسم الفقرة.

استخدم ParagraphFormat.setMarginLeft عندما تحتاج إلى تحريك الفقرة بأكملها. استخدم ParagraphFormat.setIndent عندما تحتاج إلى تحريك السطر الأول فقط.

المثال أدناه ينشئ عدة فقرات ويطبق قيم مختلفة من ParagraphFormat.setIndent لتوضيح تأثير إزاحة السطر الأول على تخطيط الفقرة.

  1. إنشاء نسخة من الفئة Presentation.
  2. الوصول إلى الشريحة الهدف.
  3. إضافة AutoShape مستطيل إلى الشريحة.
  4. الوصول إلى TextFrame الخاص بالشكل وإزالة الفقرة الافتراضية.
  5. إنشاء عدة فقرات وتعيين قيم مختلفة من ParagraphFormat.setIndent لها.
  6. إضافة الفقرات إلى إطار النص.
  7. حفظ العرض التقديمي المعدل.

يعرض هذا الشيفرة كيفية تعيين إزاحة الفقرة:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 50, 50, 420, 220);
    shape.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
    shape.getLineFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "GRAY"));

    const textFrame = shape.getTextFrame();
    textFrame.getTextFrameFormat().setAutofitType(java.newByte(aspose.slides.TextAutofitType.Shape));
    textFrame.getParagraphs().clear();

    const firstParagraph = new aspose.slides.Paragraph();
    firstParagraph.setText("No first-line indent. Wrapped lines start at the same position as the first line.");
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    firstParagraph.getParagraphFormat().setMarginLeft(20);
    firstParagraph.getParagraphFormat().setIndent(0);

    const secondParagraph = new aspose.slides.Paragraph();
    secondParagraph.setText("First-line indent of 20 points. The first line moves to the right, while wrapped lines remain aligned to the paragraph body.");
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    secondParagraph.getParagraphFormat().setMarginLeft(20);
    secondParagraph.getParagraphFormat().setIndent(20);

    const thirdParagraph = new aspose.slides.Paragraph();
    thirdParagraph.setText("First-line indent of 40 points. This paragraph shows a larger first-line offset to make the effect easier to see.");
    thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    thirdParagraph.getParagraphFormat().setMarginLeft(20);
    thirdParagraph.getParagraphFormat().setIndent(40);

    textFrame.getParagraphs().add(firstParagraph);
    textFrame.getParagraphs().add(secondParagraph);
    textFrame.getParagraphs().add(thirdParagraph);

    presentation.save("paragraph_indent.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

النتيجة:

إزاحة السطر الأول للفقرات

تعيين إزاحة معلقة

الإزاحة المعلقة هي تخطيط فقرة يبدأ فيه السطر الأول إلى اليسار من الأسطر المتبقية. في Aspose.Slides، يمكنك إنشاء هذا التأثير باستخدام ParagraphFormat.setIndent. مرّر قيمة سالبة لتحريك السطر الأول إلى اليسار بالنسبة لجسم الفقرة.

عمليًا، يحدد ParagraphFormat.setMarginLeft الموضع الأيسر لجسم الفقرة، ويحدد ParagraphFormat.setIndent موضع السطر الأول بالنسبة لهذا الهوامش. لإنشاء إزاحة معلقة، مرّر قيمة إيجابية إلى setMarginLeft وقيمة سلبية إلى setIndent.

هذا التنسيق مفيد للمراجع، الفهارس، مداخل القواميس، وغيرها من الفقرات التي يجب أن تكون الأسطر الملفوفة محاذية تحت جسم الفقرة وليس تحت حرف السطر الأول.

  1. إنشاء نسخة من الفئة Presentation.
  2. الوصول إلى الشريحة الهدف.
  3. إضافة AutoShape مستطيل إلى الشريحة.
  4. الوصول إلى TextFrame الخاص بالشكل وإزالة الفقرة الافتراضية.
  5. إنشاء فقرات وتمرير قيمة إيجابية إلى ParagraphFormat.setMarginLeft لكل فقرة.
  6. تمرير قيمة سلبية إلى ParagraphFormat.setIndent لإنشاء تأثير الإزاحة المعلقة.
  7. إضافة الفقرات إلى إطار النص.
  8. حفظ العرض التقديمي المعدل.

يعرض هذا الشيفرة كيفية تعيين إزاحة معلقة لفقرة:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 50, 50, 420, 220);
    shape.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
    shape.getLineFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "GRAY"));

    const textFrame = shape.getTextFrame();
    textFrame.getTextFrameFormat().setAutofitType(java.newByte(aspose.slides.TextAutofitType.Shape));
    textFrame.getParagraphs().clear();

    const firstParagraph = new aspose.slides.Paragraph();
    firstParagraph.setText("A hanging indent is created by combining a positive left margin with a negative indent. The first line starts to the left, while wrapped lines align with the paragraph body.");
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    firstParagraph.getParagraphFormat().setMarginLeft(40);
    firstParagraph.getParagraphFormat().setIndent(-20);

    const secondParagraph = new aspose.slides.Paragraph();
    secondParagraph.setText("This second example uses a deeper hanging indent so the difference between the first line and the wrapped lines is easier to compare.");
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    secondParagraph.getParagraphFormat().setMarginLeft(60);
    secondParagraph.getParagraphFormat().setIndent(-30);

    textFrame.getParagraphs().add(firstParagraph);
    textFrame.getParagraphs().add(secondParagraph);

    presentation.save("hanging_indent.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

النتيجة:

إزاحة معلقة للفقرات

تعيين خصائص النهاية للفقرة

Paragraph.setEndParagraphPortionFormat يتحكم في تنسيق علامة النهاية للفقرة. المثال التالي يعيّن حجم الخط والخط اللاتيني لعلامة النهاية للفقرة الثانية:

  1. إنشاء أو تحميل Presentation والوصول إلى شريحة.
  2. إضافة AutoShape ومسح الفقرة الافتراضية الخاصة به.
  3. إنشاء فقراتين وإضافة أجزاء نصية إليهما.
  4. إنشاء PortionFormat لعلامة النهاية للفقرة الثانية.
  5. تعيين BasePortionFormat.setFontHeight وBasePortionFormat.setLatinFont.
  6. ربط التنسيق باستخدام Paragraph.setEndParagraphPortionFormat وحفظ العرض التقديمي.
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);
    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 10, 10, 200, 250);
    const textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    const firstParagraph = new aspose.slides.Paragraph();
    firstParagraph.getPortions().add(new aspose.slides.Portion("Sample text"));

    const secondParagraph = new aspose.slides.Paragraph();
    secondParagraph.getPortions().add(new aspose.slides.Portion("Sample text 2"));

    const endParagraphFormat = new aspose.slides.PortionFormat();
    endParagraphFormat.setFontHeight(48);
    endParagraphFormat.setLatinFont(new aspose.slides.FontData("Times New Roman"));
    secondParagraph.setEndParagraphPortionFormat(endParagraphFormat);

    textFrame.getParagraphs().add(firstParagraph);
    textFrame.getParagraphs().add(secondParagraph);

    presentation.save("end_paragraph_format.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

عدد الأسطر المُرسَمة

للقواعد التي تؤثر على التفاف النص التلقائي وعلامات الترقيم في نهايات الأسطر، راجع Control Line Breaking وControl Hanging Punctuation.

استخدم Paragraph.getLinesCount لحساب عدد الأسطر التي يشغلها نص الفقرة بعد ترتيب النص، بما في ذلك التفاف النص التلقائي. هذا مفيد عند فحص طول النص وتخطيطه في قوالب العروض التقديمية.

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

المثال التالي ينشئ شكل نصي، يحسب أسطره، يضيق الشكل، ثم يستبدل النص بسلسلة أقصر. يتم تمكين التفاف النص وتعطيل الملاءمة التلقائية بحيث يتحكم عرض الشكل في التفاف النص دون تقليصه تلقائيًا أو تغيير حجم الشكل. أبعاد الشكل بوحدات النقاط. أخيرًا، يضيف المثال فقرة أخرى ويجمع عدد الأسطر عبر إطار النص.

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 50, 50, 400, 200);
    const textFrame = shape.getTextFrame();
    textFrame.getTextFrameFormat().setWrapText(java.newByte(aspose.slides.NullableBool.True));
    textFrame.getTextFrameFormat().setAutofitType(java.newByte(aspose.slides.TextAutofitType.None));

    const paragraph = textFrame.getParagraphs().get_Item(0);
    paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(20);
    paragraph.setText("This text demonstrates how automatic wrapping changes the number of rendered lines.");
    console.log("Original width: " + paragraph.getLinesCount());

    shape.setWidth(150);
    console.log("Narrower shape: " + paragraph.getLinesCount());

    paragraph.setText("Short text.");
    console.log("Shorter text: " + paragraph.getLinesCount());

    const secondParagraph = new aspose.slides.Paragraph();
    secondParagraph.setText("Another paragraph.");
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(20);
    textFrame.getParagraphs().add(secondParagraph);

    let totalLineCount = 0;
    for (let i = 0; i < textFrame.getParagraphs().getCount(); i++) {
        const currentParagraph = textFrame.getParagraphs().get_Item(i);
        totalLineCount += currentParagraph.getLinesCount();
    }
    console.log("Total lines in the text frame: " + totalLineCount);
} finally {
    presentation.dispose();
}

مع هذا النص وهذه الأبعاد، تضييق الشكل يزيد عدد الأسطر، بينما استبدال النص بالسلسلة القصيرة يقلله. قد تختلف الأعداد الدقيقة باختلاف توافر الخطوط والاستبدال، حجم الخط، الهوامش، الإزاحة، التفاف النص، وإعدادات الملاءمة التلقائية. استخدم الخطوط وإعدادات التخطيط المتوقعة للبيئة المستهدفة عند فحص القالب.

عدد الأسطر وحده لا يحدد ما إذا كان النص سيتجاوز حاويته. الارتفاع المتاح، ارتفاع الأسطر، مسافات الفقرات والأسطر، وسلوك الملاءمة التلقائية مهم أيضًا؛ حتى سطر واحد قد يتجاوز العرض المتاح عندما يكون التفاف النص معطلاً.

استيراد وتصدير محتوى الفقرة

استيراد نص HTML إلى الفقرات

استخدم ParagraphCollection.addFromHtml لتحويل ترميز HTML إلى فقرات وأجزاء داخل إطار النص.

  1. إنشاء نسخة من الفئة Presentation.
  2. الوصول إلى شريحة وإضافة AutoShape.
  3. الوصول إلى TextFrame الخاص بالشكل ومسح الفقرة الافتراضية.
  4. تعريف أو قراءة سلسلة HTML المصدر.
  5. تمرير سلسلة HTML إلى ParagraphCollection.addFromHtml.
  6. حفظ العرض التقديمي المعدل.

يُظهر المثال التالي بلغة JavaScript استيراد HTML إلى إطار النص:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);
    const shapeWidth = presentation.getSlideSize().getSize().getWidth() - 20;
    const shapeHeight = presentation.getSlideSize().getSize().getHeight() - 20;
    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 10, 10, shapeWidth, shapeHeight);
    shape.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
    shape.getTextFrame().getParagraphs().clear();

    const html = "<p><b>Aspose.Slides</b> imports HTML text into presentation paragraphs.</p>";
    shape.getTextFrame().getParagraphs().addFromHtml(html);
    presentation.save("html_text.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

تصدير نص الفقرة إلى HTML

استخدم ParagraphCollection.exportToHtml لتصدير نطاق محدد من الفقرات كـ HTML.

  1. إنشاء أو تحميل نسخة من الفئة Presentation.
  2. الوصول إلى الشريحة والعثور على AutoShape الذي يحتوي على النص.
  3. الوصول إلى TextFrame الخاص بالشكل.
  4. استدعاء ParagraphCollection.exportToHtml مع فهرس الفقرة الابتدائي وعدد الفقرات المراد تصديرها.
  5. كتابة سلسلة HTML المُرجعة إلى ملف.

يُظهر المثال التالي بلغة JavaScript إنشاء شكل نصي وتصدير جميع فقراته:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const fs = require("fs");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);
    const sourceShape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 20, 20, 400, 100);
    const sourceTextFrame = sourceShape.getTextFrame();
    sourceTextFrame.getParagraphs().clear();
    for (const text of ["First paragraph", "Second paragraph", "Third paragraph"]) {
        const sourceParagraph = new aspose.slides.Paragraph();
        sourceParagraph.setText(text);
        sourceTextFrame.getParagraphs().add(sourceParagraph);
    }
    const shape = slide.getShapes().get_Item(0);

    if (java.instanceOf(shape, "com.aspose.slides.IAutoShape")) {
        const textFrame = shape.getTextFrame();
        if (textFrame !== null) {
            const paragraphs = textFrame.getParagraphs();
            const html = paragraphs.exportToHtml(0, paragraphs.getCount(), null);
            fs.writeFileSync("paragraphs.html", html, "utf8");
        } else {
            console.log("The first shape does not contain a text frame.");
        }
    } else {
        console.log("The first shape is not a text shape.");
    }
} finally {
    presentation.dispose();
}

تصيير الفقرة كصورة

Paragraph.getImage يصوّر فقرة فردية مباشرة ويُعيد كائن IImage. احفظ النتيجة إلى ملف باستخدام IImage.save. لا تحتاج إلى تصيير الشكل المحتوي أو قص صورة يدوية.

Paragraph.getImage قد تُعيد null إذا تعذّر العثور على الفقرة في المجموعة الأصلية، أو لا توجد حدود رسم صالحة، أو لا يمكن تصييرها. تحقق من النتيجة قبل حفظها وتخلص من الصورة المُرجعة بعد الاستخدام.

تصيير الفقرة بالمقياس الافتراضي

صندوق النص التالي يحتوي على ثلاث فقرات:

صندوق النص مع ثلاث فقرات

المثال التالي يصوّر الفقرة الثانية داخل شكل نص عادي بالمقياس الافتراضي ويحفظ الصورة المُرجعة بصيغة PNG. يضمن القسم finally التخلص من الصورة بشكل صحيح.

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);
    const sourceShape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 20, 20, 400, 100);
    const sourceTextFrame = sourceShape.getTextFrame();
    sourceTextFrame.getParagraphs().clear();
    for (const text of ["First paragraph", "Second paragraph", "Third paragraph"]) {
        const sourceParagraph = new aspose.slides.Paragraph();
        sourceParagraph.setText(text);
        sourceTextFrame.getParagraphs().add(sourceParagraph);
    }
    const shape = slide.getShapes().get_Item(0);

    if (java.instanceOf(shape, "com.aspose.slides.IAutoShape")) {
        const textFrame = shape.getTextFrame();
        if (textFrame !== null && textFrame.getParagraphs().getCount() > 1) {
            const paragraph = textFrame.getParagraphs().get_Item(1);
            const paragraphImage = paragraph.getImage();

            if (paragraphImage !== null) {
                try {
                    paragraphImage.save("paragraph.png", aspose.slides.ImageFormat.Png);
                } finally {
                    paragraphImage.dispose();
                }
            } else {
                console.log("The paragraph could not be rendered.");
            }
        } else {
            console.log("The expected paragraph was not found.");
        }
    } else {
        console.log("The first shape is not a text shape.");
    }
} finally {
    presentation.dispose();
}

النتيجة:

صورة الفقرة

تصيير الفقرة داخل خلية جدول مع تعديل المقياس

استخدم نسخة Paragraph.getImage التي تقبل معايير scaleX وscaleY لتحديد عوامل المقياس الأفقي والر​أسي. المثال التالي ينشئ جدولًا، يصوّر الفقرة في خليةه الأولى بمعدل عرض وارتفاع ضعف الحجم الافتراضي، ويحفظ النتيجة كصورة PNG.

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const scaleX = 2;
const scaleY = 2;

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);
    const columnWidths = java.newArray("double", [300]);
    const rowHeights = java.newArray("double", [80]);
    const table = slide.getShapes().addTable(50, 50, columnWidths, rowHeights);
    const paragraph = table.get_Item(0, 0).getTextFrame().getParagraphs().get_Item(0);
    paragraph.setText("Text in a table cell");

    const paragraphImage = paragraph.getImage(scaleX, scaleY);
    if (paragraphImage !== null) {
        try {
            paragraphImage.save("table_paragraph.png", aspose.slides.ImageFormat.Png);
        } finally {
            paragraphImage.dispose();
        }
    } else {
        console.log("The paragraph could not be rendered.");
    }
} finally {
    presentation.dispose();
}

عامل المقياس 1 يحافظ على البكسل الافتراضي للمحور. على سبيل المثال، 2 لكلا العاملين ينتج صورة عرضها وارتفاعها تقريبًا ضعف الأبعاد الافتراضية، أي أربعة أضعاف عدد البكسلات. العوامل الأكبر عادةً ما تُنتج نصًا أكثر وضوحًا للتكبير أو مخرجات عالية الدقة، لكنها تزيد أيضًا من استهلاك الذاكرة وحجم الملف. العوامل أقل من 1 تُنتج صورًا أصغر بتفاصيل أقل. استخدم عوامل متماثلة للحفاظ على نسبة أبعاد الفقرة؛ العوامل الأفقي والعمودي المختلفة تمدد المخرجات بشكل مستقل.

تصيير الشكل كاملًا باستخدام Shape.getImage يظل مفيدًا عندما يجب أن يتضمن الناتج تعبئة الشكل أو حدوده أو سياقه البصري الآخر. للحصول على صورة للفقرة فقط، استخدم Paragraph.getImage.

الأسئلة المتكررة

هل يمكنني تعطيل التفاف السطر داخل إطار النص تمامًا؟

نعم. اضبط TextFrameFormat.setWrapText لتعطيل التفاف النص بحيث لا تنكسر الأسطر عند حواف إطار النص.

كيف يمكنني الحصول على أبعاد الفقرة المحددة داخل الشريحة بدقة؟

استخدم Paragraph.getRect لاسترجاع المستطيل المحيط بالفقرة. توفر Portion.getRect أبعاد الجزء الفردي.

أين يتم التحكم في محاذاة الفقرة (يسار، يمين، وسط، أو ضبط)؟

ParagraphFormat.setAlignment هو إعداد على مستوى الفقرة ويُطبق على كامل الفقرة بغض النظر عن تنسيق الأجزاء الفردية.

هل يمكنني تعيين لغة التدقيق لجزء من الفقرة؟

نعم. اضبط BasePortionFormat.setLanguageId للأجزاء الفردية، بحيث يمكن لفقرة واحدة أن تحتوي نصًا بعدة لغات.