مدیریت پاراگراف‌های متن PowerPoint در Android

مرور کلی

Aspose.Slides برای Android از طریق Java متن را به عنوان سلسله‌مراتبی از فریم‌های متن، پاراگراف‌ها و بخش‌ها (Portion) نمایش می‌دهد:

  • ITextFrame نمایانگر محفظهٔ متن در یک شکل است و دسترسی به مجموعهٔ پاراگراف‌های آن را فراهم می‌کند.
  • IParagraph نمایانگر یک پاراگراف در فریم متن است و دسترسی به بخش‌ها و قالب‌بندی سطح پاراگراف را فراهم می‌کند.
  • IPortion نمایانگر یک بخش متن درون یک پاراگراف است. هر بخش می‌تواند متن و قالب‌بندی سطح کاراکتر مخصوص به خود را داشته باشد.

بنابراین یک پاراگراف می‌تواند متن با فونت‌ها، رنگ‌ها، اندازه‌ها و قالب‌بندی‌های مختلف را با استفاده از چندین بخش (Portion) شامل شود.

ایجاد و قالب‌بندی پاراگراف‌ها

ایجاد پاراگراف‌ها با بخش‌های چندگانه

مراحل زیر یک فریم متنی با سه پاراگراف ایجاد می‌کند که هر یک شامل سه بخش هستند:

  1. یک نمونه از کلاس Presentation ایجاد کنید.
  2. از طریق شاخص، اسلاید مربوطه را دسترسی پیدا کنید.
  3. یک IAutoShape مستطیلی به اسلاید اضافه کنید.
  4. به ITextFrame شکل دسترسی پیدا کنید.
  5. از پاراگراف پیش‌فرض استفاده کنید و دو شیء دیگر از نوع IParagraph را به فریم متن اضافه کنید.
  6. به اندازه کافی شیء IPortion برای هر پاراگراف اضافه کنید تا شامل سه بخش باشد. پاراگراف پیش‌فرض قبلاً یک بخش خالی دارد.
  7. متن هر بخش را تنظیم کنید.
  8. قالب‌بندی سطح کاراکتر را از طریق IPortion.getPortionFormat اعمال کنید.
  9. ارائه اصلاح‌شده را ذخیره کنید.

این مثال Android از طریق Java مراحل را اجرا می‌کند:

import com.aspose.slides.*;
import android.graphics.Color;

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

    IParagraph firstParagraph = textFrame.getParagraphs().get_Item(0);
    firstParagraph.getPortions().add(new Portion());
    firstParagraph.getPortions().add(new Portion());

    IParagraph secondParagraph = new Paragraph();
    secondParagraph.getPortions().add(new Portion());
    secondParagraph.getPortions().add(new Portion());
    secondParagraph.getPortions().add(new Portion());
    textFrame.getParagraphs().add(secondParagraph);

    IParagraph thirdParagraph = new Paragraph();
    thirdParagraph.getPortions().add(new Portion());
    thirdParagraph.getPortions().add(new Portion());
    thirdParagraph.getPortions().add(new Portion());
    textFrame.getParagraphs().add(thirdParagraph);

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

            if (portionIndex == 0) {
                portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
                portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.RED);
                portion.getPortionFormat().setFontBold(NullableBool.True);
                portion.getPortionFormat().setFontHeight(15);
            } else if (portionIndex == 1) {
                portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
                portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE);
                portion.getPortionFormat().setFontItalic(NullableBool.True);
                portion.getPortionFormat().setFontHeight(18);
            }
        }
    }

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

ایجاد فهرست‌های نقطه‌دار و عددی

ایجاد یک فهرست نقطه‌دار یا عددی

نقطه‌ها و شماره‌گذاری موارد مرتبط را برای مرور راحت‌تر می‌کند. در Aspose.Slides، تنظیمات فهرست از طریق IBulletFormat تعریف می‌شوند.

  1. یک نمونه از کلاس Presentation ایجاد کنید.
  2. از طریق شاخص، اسلاید مربوطه را دسترسی پیدا کنید.
  3. یک IAutoShape به اسلاید انتخاب شده اضافه کنید.
  4. به ITextFrame شکل دسترسی پیدا کنید.
  5. پاراگراف پیش‌فرض را از فریم متن حذف کنید.
  6. یک Paragraph برای یک نقطه نمادین ایجاد کنید.
  7. IBulletFormat.setType را به BulletType.Symbol تنظیم کنید و کاراکتر نقطه را مشخص کنید.
  8. متن پاراگراف، تورفتگی، رنگ نقطه و ارتفاع نقطه را تنظیم کنید.
  9. پاراگراف را به فریم متن اضافه کنید.
  10. یک پاراگراف دوم ایجاد کنید و IBulletFormat.setType را به BulletType.Numbered تنظیم کنید.
  11. سبک نقطه عددی را پیکربندی کنید و پاراگراف را به فریم متن اضافه کنید.
  12. ارائه را ذخیره کنید.

این مثال Android از طریق Java یک نقطه نمادین و یک نقطه عددی ایجاد می‌کند:

import com.aspose.slides.*;
import android.graphics.Color;

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

    Paragraph symbolParagraph = new Paragraph();
    symbolParagraph.setText("Welcome to Aspose.Slides");
    symbolParagraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
    symbolParagraph.getParagraphFormat().getBullet().setChar((char) 0x2022);
    symbolParagraph.getParagraphFormat().setIndent(25);
    symbolParagraph.getParagraphFormat().getBullet().getColor().setColorType(ColorType.RGB);
    symbolParagraph.getParagraphFormat().getBullet().getColor().setColor(Color.BLACK);
    symbolParagraph.getParagraphFormat().getBullet().setBulletHardColor(NullableBool.True);
    symbolParagraph.getParagraphFormat().getBullet().setHeight(100);
    textFrame.getParagraphs().add(symbolParagraph);

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

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

استفاده از نقطه‌های تصویری

نقطه‌های تصویری به شما امکان می‌دهند به‌جای نماد یا عدد، تصویر سفارشی استفاده کنید.

  1. یک نمونه از کلاس Presentation ایجاد کنید.
  2. از طریق شاخص، اسلاید مربوطه را دسترسی پیدا کنید.
  3. یک IAutoShape اضافه کنید و به ITextFrame آن دسترسی پیدا کنید.
  4. پاراگراف پیش‌فرض را از فریم متن حذف کنید.
  5. تصویر نقطه را بارگیری کنید و به مجموعهٔ تصاویر ارائه به عنوان یک IPPImage اضافه کنید.
  6. یک Paragraph ایجاد کنید و متن آن را تنظیم کنید.
  7. IBulletFormat.setType را به BulletType.Picture تنظیم کنید.
  8. تصویر را از طریق IBulletFormat.getPicture اختصاص دهید و ارتفاع نقطه را تنظیم کنید.
  9. پاراگراف را به فریم متن اضافه کنید.
  10. ارائه اصلاح‌شده را ذخیره کنید.

این مثال Android از طریق Java یک نقطه تصویری ایجاد می‌کند:

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IImage bulletImage = Images.fromFile("bullets.png");
    IPPImage presentationImage;
    try {
        presentationImage = presentation.getImages().addImage(bulletImage);
    } finally {
        bulletImage.dispose();
    }

    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

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

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

ایجاد فهرست چندسطحی

IParagraphFormat.setDepth را تنظیم کنید تا پاراگراف‌ها در سطوح مختلف فهرست قرار گیرند. سطح بالایی عمق 0 دارد.

  1. یک Presentation ایجاد کنید و به یک اسلاید دسترسی پیدا کنید.
  2. یک IAutoShape اضافه کنید و پاراگراف پیش‌فرض را از فریم متن آن پاک کنید.
  3. چهار پاراگراف ایجاد کنید و نمادهای نقطهٔ آن‌ها را پیکربندی کنید.
  4. مقادیر IParagraphFormat.setDepth آن‌ها را به ترتیب 0، 1، 2 و 3 تنظیم کنید.
  5. پاراگراف‌ها را به فریم متن اضافه کنید و ارائه را ذخیره کنید.

این مثال Android از طریق Java یک فهرست نقطه‌دار چهار سطحی ایجاد می‌کند:

import com.aspose.slides.*;
import android.graphics.Color;

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

    IParagraph firstParagraph = new Paragraph();
    firstParagraph.setText("Content");
    firstParagraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
    firstParagraph.getParagraphFormat().getBullet().setChar((char) 0x2022);
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    firstParagraph.getParagraphFormat().setDepth((short) 0);

    IParagraph secondParagraph = new Paragraph();
    secondParagraph.setText("Second level");
    secondParagraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
    secondParagraph.getParagraphFormat().getBullet().setChar('-');
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    secondParagraph.getParagraphFormat().setDepth((short) 1);

    IParagraph thirdParagraph = new Paragraph();
    thirdParagraph.setText("Third level");
    thirdParagraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
    thirdParagraph.getParagraphFormat().getBullet().setChar((char) 0x2022);
    thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
    thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    thirdParagraph.getParagraphFormat().setDepth((short) 2);

    IParagraph fourthParagraph = new Paragraph();
    fourthParagraph.setText("Fourth level");
    fourthParagraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
    fourthParagraph.getParagraphFormat().getBullet().setChar('-');
    fourthParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
    fourthParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    fourthParagraph.getParagraphFormat().setDepth((short) 3);

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

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

شروع آیتم‌های فهرست عددی با مقادیر دلخواه

از IBulletFormat.setNumberedBulletStartWith استفاده کنید تا عدد اولیه نمایش داده‌شده برای یک پاراگراف عددی تنظیم شود.

  1. یک Presentation ایجاد کنید و یک IAutoShape به یک اسلاید اضافه کنید.
  2. پاراگراف پیش‌فرض را از فریم متن شکل پاک کنید.
  3. سه پاراگراف عددی ایجاد کنید.
  4. برای پاراگراف‌های مربوطه، IBulletFormat.setNumberedBulletStartWith را به ترتیب به 2، 3 و 7 تنظیم کنید.
  5. پاراگراف‌ها را به فریم متن اضافه کنید و ارائه را ذخیره کنید.

این مثال Android از طریق Java عدد شروع دلخواه را به هر پاراگراف اختصاص می‌دهد:

import com.aspose.slides.*;

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

    Paragraph firstParagraph = new Paragraph();
    firstParagraph.setText("Start at 2");
    firstParagraph.getParagraphFormat().getBullet().setType(BulletType.Numbered);
    firstParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith((short) 2);
    textFrame.getParagraphs().add(firstParagraph);

    Paragraph secondParagraph = new Paragraph();
    secondParagraph.setText("Start at 3");
    secondParagraph.getParagraphFormat().getBullet().setType(BulletType.Numbered);
    secondParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith((short) 3);
    textFrame.getParagraphs().add(secondParagraph);

    Paragraph thirdParagraph = new Paragraph();
    thirdParagraph.setText("Start at 7");
    thirdParagraph.getParagraphFormat().getBullet().setType(BulletType.Numbered);
    thirdParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith((short) 7);
    textFrame.getParagraphs().add(thirdParagraph);

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

کنترل چیدمان پاراگراف و خصوصیات انتها

تنظیم تورفتگی خط اول

از IParagraphFormat.setIndent برای کنترل تورفتگی خط اول یک پاراگراف استفاده کنید. این متد فقط خط اول را نسبت به حاشیهٔ چپ پاراگراف جابه‌جا می‌کند. مقدار مثبت خط اول را به سمت راست می‌کشد، در حالی که خطوط باقی‌مانده به بدنهٔ پاراگراف تراز می‌مانند.

زمانی که نیاز به جابه‌جایی کل پاراگراف دارید از IParagraphFormat.setMarginLeft استفاده کنید. زمانی که فقط خط اول را می‌خواهید جابه‌جا کنید از IParagraphFormat.setIndent استفاده کنید.

مثال زیر چند پاراگراف ایجاد می‌کند و مقادیر مختلف IParagraphFormat.setIndent را برای نشان دادن اثر تورفتگی خط اول بر چیدمان پاراگراف اعمال می‌کند.

  1. یک نمونه از کلاس Presentation ایجاد کنید.
  2. اسلاید هدف را دسترسی پیدا کنید.
  3. یک IAutoShape مستطیلی به اسلاید اضافه کنید.
  4. به ITextFrame شکل دسترسی پیدا کنید و پاراگراف پیش‌فرض را حذف کنید.
  5. چند پاراگراف ایجاد کنید و مقادیر مختلف IParagraphFormat.setIndent را برای آن‌ها تنظیم کنید.
  6. پاراگراف‌ها را به فریم متن اضافه کنید.
  7. ارائه اصلاح‌شده را ذخیره کنید.

این کد نشان می‌دهد چگونه تورفتگی پاراگراف تنظیم شود:

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
    shape.getFillFormat().setFillType(FillType.NoFill);
    shape.getLineFormat().getFillFormat().setFillType(FillType.Solid);
    shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY);

    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.Shape);
    textFrame.getParagraphs().clear();

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

    Paragraph secondParagraph = new 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(FillType.Solid);
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    secondParagraph.getParagraphFormat().setMarginLeft(20f);
    secondParagraph.getParagraphFormat().setIndent(20f);

    Paragraph thirdParagraph = new 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(FillType.Solid);
    thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    thirdParagraph.getParagraphFormat().setMarginLeft(20f);
    thirdParagraph.getParagraphFormat().setIndent(40f);

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

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

نتیجه:

تورفتگی خط اول پاراگراف‌ها

تنظیم تورفتگی معلق

تورفتگی معلق یک چیدمان پاراگراف است که در آن خط اول نسبت به بقیه خطوط به سمت چپ می‌آید. در Aspose.Slides این اثر را با IParagraphFormat.setIndent ایجاد می‌کنید. مقدار منفی به خط اول اجازه می‌دهد نسبت به بدنهٔ پاراگراف به سمت چپ حرکت کند.

در عمل، IParagraphFormat.setMarginLeft موقعیت چپ بدنهٔ پاراگراف را تعیین می‌کند و IParagraphFormat.setIndent موقعیت خط اول نسبت به آن حاشیه را. برای ایجاد تورفتگی معلق، مقدار مثبت را به setMarginLeft و مقدار منفی را به setIndent بدهید.

این قالب‌بندی برای کتاب‌شناسی‌ها، مراجع، واژه‌نامه‌ها و سایر پاراگراف‌هایی که خطوط بسته‌شده باید زیر بدنهٔ پاراگراف تراز شوند مفید است.

  1. یک نمونه از کلاس Presentation ایجاد کنید.
  2. اسلاید هدف را دسترسی پیدا کنید.
  3. یک IAutoShape مستطیلی به اسلاید اضافه کنید.
  4. به ITextFrame شکل دسترسی پیدا کنید و پاراگراف پیش‌فرض را حذف کنید.
  5. برای هر پاراگراف مقدار مثبت به IParagraphFormat.setMarginLeft بدهید.
  6. مقدار منفی به IParagraphFormat.setIndent بدهید تا اثر تورفتگی معلق ایجاد شود.
  7. پاراگراف‌ها را به فریم متن اضافه کنید.
  8. ارائه اصلاح‌شده را ذخیره کنید.

این کد نشان می‌دهد چگونه تورفتگی معلق برای یک پاراگراف تنظیم شود:

import com.aspose.slides.*;
import android.graphics.Color;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
    shape.getFillFormat().setFillType(FillType.NoFill);
    shape.getLineFormat().getFillFormat().setFillType(FillType.Solid);
    shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY);

    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.Shape);
    textFrame.getParagraphs().clear();

    Paragraph firstParagraph = new 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(FillType.Solid);
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    firstParagraph.getParagraphFormat().setMarginLeft(40f);
    firstParagraph.getParagraphFormat().setIndent(-20f);

    Paragraph secondParagraph = new 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(FillType.Solid);
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    secondParagraph.getParagraphFormat().setMarginLeft(60f);
    secondParagraph.getParagraphFormat().setIndent(-30f);

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

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

نتیجه:

تورفتگی معلق پاراگراف‌ها

تنظیم خصوصیات انتهای پاراگراف

IParagraph.setEndParagraphPortionFormat کنترل قالب‌بندی علامت انتهای پاراگراف را بر عهده دارد. مثال زیر اندازهٔ قلم و قلم لاتین را به علامت انتهای پاراگراف دوم اختصاص می‌دهد:

  1. یک Presentation بارگیری کنید و به یک اسلاید دسترسی پیدا کنید.
  2. یک IAutoShape اضافه کنید و پاراگراف پیش‌فرض آن را پاک کنید.
  3. دو پاراگراف ایجاد کنید و به آن‌ها بخش‌های متنی اضافه کنید.
  4. یک PortionFormat برای علامت انتهای پاراگراف دوم ایجاد کنید.
  5. IBasePortionFormat.setFontHeight و IBasePortionFormat.setLatinFont را تنظیم کنید.
  6. قالب را با IParagraph.setEndParagraphPortionFormat اعمال کنید و ارائه را ذخیره کنید.
import com.aspose.slides.*;

Presentation presentation = new Presentation("Test.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 10, 200, 250);
    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

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

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

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

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

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

تعداد خطوط رندر شده

برای قوانین پاراگراف که بر بسته‌شدن خودکار و نقطه‌گذاری در انتهای خطوط تاثیر می‌گذارند، به کنترل شکستن خط و کنترل نقطه‌گذاری معلق مراجعه کنید.

از IParagraph.getLinesCount برای شمارش خطوط اشغال‌شده توسط یک پاراگراف پس از چیدمان متن استفاده کنید؛ این شامل بسته‌شدن خودکار می‌شود. این کار برای بررسی طول متن و چیدمان در قالب‌های ارائه مفید است.

یک پاراگراف یک مورد در ITextFrame.getParagraphs است و می‌تواند چندین خط رندر شده اشغال کند. شکست خط صریح داخل پاراگراف باعث ایجاد خط جدید می‌شود بدون اینکه پاراگراف دیگری ایجاد شود. بسته‌شدن خودکار خطوط را بر اساس عرض موجود ایجاد می‌کند بدون اینکه شکست‌های خط صریح به متن افزوده شود. بنابراین شمارش پاراگراف‌ها یا کاراکترهای شکست خط، تعداد خطوط رندر شده را نمی‌دهد.

مثال زیر یک شکل متنی ایجاد می‌کند، خطوط آن را می‌شمارد، شکل را باریک می‌کند و سپس متن را با رشته‌ای کوتاه‌تر جایگزین می‌کند. بسته‌شدن فعال است و خودکار‑تناسب غیرفعال شده تا عرض شکل کنترل بسته‌شدن را بدون کوچک‌سازی خودکار متن یا تغییر اندازه شکل انجام دهد. ابعاد شکل بر حسب پوینت است. در نهایت مثال یک پاراگراف دیگر اضافه می‌کند و مجموع تعداد خطوط را در فریم متن محاسبه می‌کند.

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 400, 200);
    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getTextFrameFormat().setWrapText(NullableBool.True);
    textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.None);

    IParagraph 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.");
    System.out.println("Original width: " + paragraph.getLinesCount());

    shape.setWidth(150);
    System.out.println("Narrower shape: " + paragraph.getLinesCount());

    paragraph.setText("Short text.");
    System.out.println("Shorter text: " + paragraph.getLinesCount());

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

    int totalLineCount = 0;
    for (IParagraph currentParagraph : textFrame.getParagraphs()) {
        totalLineCount += currentParagraph.getLinesCount();
    }
    System.out.println("Total lines in the text frame: " + totalLineCount);
} finally {
    presentation.dispose();
}

با این متن و این ابعاد، باریک‌کردن شکل تعداد خطوط را افزایش می‌دهد، در حالی که جایگزینی متن با رشتهٔ کوتاه‌تر آن را کاهش می‌دهد. شمارش دقیق می‌تواند بسته به در دسترس بودن قلم، جایگزینی، اندازه قلم، حاشیه‌ها، تورفتگی، بسته‌شدن و تنظیمات خودکار‑تناسب متفاوت باشد. هنگام بررسی قالب، از قلم‌ها و تنظیمات چیدمانی که برای محیط هدف در نظر گرفته شده‌اند استفاده کنید.

تنها شمارش خطوط تصمیم‌گیری دربارهٔ سرریز متن در محفظه را انجام نمی‌دهد. ارتفاع موجود، ارتفاع خطوط، فاصله پاراگراف و خط، و رفتار خودکار‑تناسب نیز مهم هستند؛ حتی یک خط می‌تواند عرض موجود را هنگام غیرفعال بودن بسته‌شدن تجاوز کند.

واردات و صادرات محتوای پاراگراف

وارد کردن متن HTML به پاراگراف‌ها

از ParagraphCollection.addFromHtml برای تبدیل مارکاپ HTML به پاراگراف‌ها و بخش‌ها در یک فریم متنی استفاده کنید.

  1. یک نمونه از کلاس Presentation ایجاد کنید.
  2. به یک اسلاید دسترسی پیدا کنید و یک IAutoShape اضافه کنید.
  3. به ITextFrame شکل دسترسی پیدا کنید و پاراگراف پیش‌فرض را پاک کنید.
  4. فایل HTML منبع را بخوانید.
  5. رشتهٔ HTML را به ParagraphCollection.addFromHtml پاس دهید.
  6. ارائه اصلاح‌شده را ذخیره کنید.

این مثال Android از طریق Java HTML را به فریم متن وارد می‌کند:

import com.aspose.slides.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

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

    try {
        byte[] htmlBytes = Files.readAllBytes(Paths.get("file.html"));
        String html = new String(htmlBytes, StandardCharsets.UTF_8);
        shape.getTextFrame().getParagraphs().addFromHtml(html);
        presentation.save("html_text.pptx", SaveFormat.Pptx);
    } catch (IOException exception) {
        System.out.println("The HTML file could not be read: " + exception.getMessage());
    }
} finally {
    presentation.dispose();
}

صادرات متن پاراگراف به HTML

از ParagraphCollection.exportToHtml برای صادرات یک بازهٔ انتخاب‌شده از پاراگراف‌ها به HTML استفاده کنید.

  1. یک نمونه از کلاس Presentation ایجاد کنید و ارائه مورد نظر را بارگیری کنید.
  2. به اسلاید دسترسی پیدا کنید و IAutoShape حاوی متن را پیدا کنید.
  3. به ITextFrame شکل دسترسی پیدا کنید.
  4. با استفاده از ParagraphCollection.exportToHtml ایندکس پاراگراف شروع و تعداد پاراگراف‌های مورد نظر برای صادرات را مشخص کنید.
  5. رشتهٔ HTML بازگشتی را در یک فایل بنویسید.

این مثال Android از طریق Java تمام پاراگراف‌ها را از اولین فریم متنی صادر می‌کند:

import com.aspose.slides.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

Presentation presentation = new Presentation("ExportingHTMLText.pptx");
try {
    IShape shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);

    if (shape instanceof IAutoShape) {
        IAutoShape textShape = (IAutoShape) shape;
        ITextFrame textFrame = textShape.getTextFrame();
        if (textFrame != null) {
            IParagraphCollection paragraphs = textFrame.getParagraphs();
            String html = paragraphs.exportToHtml(0, paragraphs.getCount(), null);
            try {
                Files.write(Paths.get("paragraphs.html"), html.getBytes(StandardCharsets.UTF_8));
            } catch (IOException exception) {
                System.out.println("The HTML file could not be written: " + exception.getMessage());
            }
        } else {
            System.out.println("The first shape does not contain a text frame.");
        }
    } else {
        System.out.println("The first shape is not a text shape.");
    }
} finally {
    presentation.dispose();
}

رندر یک پاراگراف به عنوان تصویر

IParagraph.getImage یک پاراگراف منفرد را مستقیماً رندر می‌کند و یک IImage بر می‌گرداند. نتیجه را با IImage.save در فایل یا جریان ذخیره کنید. نیازی به رندر کل شکل حاوی آن یا برش دستی bitmap ندارید.

IParagraph.getImage می‌تواند null برگرداند اگر پاراگراف در مجموعهٔ والد خود پیدا نشود، ابعاد رندر معتبری نداشته باشد یا قابل رندر نباشد. قبل از ذخیره‌سازی نتیجه را بررسی کنید و پس از استفاده تصویر بازگردانده‌شده را آزاد کنید.

رندر پاراگراف با مقیاس پیش‌فرض

فرض کنید فایلی به نام sample.pptx داریم که یک اسلاید دارد و اولین شکل آن یک جعبه متن حاوی سه پاراگراف است.

جعبه متن با سه پاراگراف

مثال زیر پاراگراف دوم را در یک شکل متن عادی با مقیاس پیش‌فرض رندر می‌کند و تصویر حاصل را در قالب PNG ذخیره می‌گیرد. بلوک finally تضمین می‌کند تصویر به‌درستی آزاد شود.

import com.aspose.slides.*;

Presentation presentation = new Presentation("sample.pptx");
try {
    IShape shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);

    if (shape instanceof IAutoShape) {
        IAutoShape textShape = (IAutoShape) shape;
        ITextFrame textFrame = textShape.getTextFrame();
        if (textFrame != null && textFrame.getParagraphs().getCount() > 1) {
            IParagraph paragraph = textFrame.getParagraphs().get_Item(1);
            IImage paragraphImage = paragraph.getImage();

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

نتیجه:

تصویر پاراگراف

رندر پاراگراف در یک سلول جدول با مقیاس‌دهی

از overload متد IParagraph.getImage که پارامترهای float scaleX و float scaleY را می‌پذیرد استفاده کنید تا عوامل مقیاس افقی و عمودی را تنظیم کنید. مثال زیر یک جدول می‌سازد، پاراگراف را در اولین سلول آن با دو برابر عرض و ارتفاع پیش‌فرض رندر می‌کند و نتیجه را به صورت تصویر PNG ذخیره می‌گیرد.

import com.aspose.slides.*;

float scaleX = 2f;
float scaleY = 2f;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    ITable table = slide.getShapes().addTable(50, 50, new double[] { 300 }, new double[] { 80 });
    IParagraph paragraph = table.get_Item(0, 0).getTextFrame().getParagraphs().get_Item(0);
    paragraph.setText("Text in a table cell");

    IImage paragraphImage = paragraph.getImage(scaleX, scaleY);
    if (paragraphImage != null) {
        try {
            paragraphImage.save("table_paragraph.png", ImageFormat.Png);
        } finally {
            paragraphImage.dispose();
        }
    } else {
        System.out.println("The paragraph could not be rendered.");
    }
} finally {
    presentation.dispose();
}

عامل مقیاس 1 آن محور را در اندازهٔ پیش‌فرض پیکسل نگه می‌دارد. برای مثال 2 برای هر دو عامل تصویری تولید می‌کند که عرض و ارتفاع آن تقریباً دو برابر ابعاد پیش‌فرض است و به این ترتیب چهار برابر پیکسل دارد. عوامل بزرگتر معمولاً متن واضح‌تری برای بزرگ‌نمایی یا خروجی با وضوح بالا تولید می‌کنند، اما مصرف حافظه و حجم فایل را نیز افزایش می‌دهند. عوامل زیر 1 تصاویر کوچکتری با جزئیات کمتر تولید می‌کنند. برای حفظ نسبت طول و عرض پاراگراف از عوامل مساوی استفاده کنید؛ عوامل افقی و عمودی متفاوت تصویر را به‌صورت مستقل کشیده می‌کنند.

رندر کل شکل با استفاده از IShape.getImage زمانی مفید است که خروجی نیاز به شامل پرکردن، حاشیه یا سایر زمینه‌های تصویری شکل داشته باشد. برای تصویر فقط پاراگراف، از IParagraph.getImage استفاده کنید.

سوالات متداول

آیا می‌توانم بسته شدن خط داخل یک فریم متن را کاملاً غیرفعال کنم؟

بله. با تنظیم ITextFrameFormat.setWrapText بسته شدن را غیرفعال کنید تا خطوط در لبه‌های فریم متن شکسته نشوند.

چگونه می‌توانم مرز دقیق روی اسلاید یک پاراگراف خاص را به‌دست آورم؟

از IParagraph.getRect برای دریافت مستطیل محاطی پاراگراف استفاده کنید. IPortion.getRect مرزهای یک بخش منفرد را فراهم می‌کند.

محل تنظیم تراز پاراگراف (چپ، راست، وسط یا توجیه) کجا کنترل می‌شود؟

IParagraphFormat.setAlignment یک تنظیم سطح پاراگراف است و روی تمام پاراگراف اعمال می‌شود، صرف‌نظر از قالب‌بندی جداگانهٔ بخش‌ها.

برای تراز عمودی بخش‌های با اندازهٔ قلم متفاوت در هر خط، به Align Fonts Within a Line مراجعه کنید.

آیا می‌توانم زبان بازبینی را برای بخشی از یک پاراگراف تنظیم کنم؟

بله. برای بخش‌های منفرد IBasePortionFormat.setLanguageId را تنظیم کنید تا یک پاراگراف بتواند متنی با زبان‌های متعدد داشته باشد.