管理 Android 上的 PowerPoint 文字段落

概觀

Aspose.Slides for Android via Java 將文字表示為文字框、段落和部份的層次結構:

  • ITextFrame 代表形狀中的文字容器,並提供存取其段落集合的功能。
  • IParagraph 代表文字框中的一個段落,並提供存取其部份及段落層級格式設定的功能。
  • IPortion 代表段落中的文字執行。每個部份可以有自己的文字和字元層級格式設定。

因此,段落可以透過使用多個部份,包含不同字型、顏色、大小和其他格式的文字。

建立並格式化段落

建立包含多個部份的段落

以下步驟會建立一個文字框,內含三個段落,每個段落都有三個部份:

  1. 建立 Presentation 類別的實例。
  2. 透過索引存取相關投影片。
  3. 在投影片上新增一個矩形的 IAutoShape。
  4. 取得形狀的 ITextFrame。
  5. 使用預設段落,並向文字框中新增另外兩個 IParagraph 物件。
  6. 為每個段落新增足夠的 IPortion 物件,使其包含三個部份。預設段落已包含一個空的部份。
  7. 設定每個部份的文字。
  8. 透過 IPortion.getPortionFormat 套用字元層級的格式設定。
  9. 儲存已修改的簡報。
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. 儲存簡報。
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. 儲存已修改的簡報。
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. 將段落加入文字框並儲存簡報。
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. 將段落加入文字框並儲存簡報。
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 控制段落結尾標記的格式設定。以下範例將字型大小與 Latin 字型套用到第二個段落的結尾標記:

  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. 儲存已修改的簡報。
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 字串寫入檔案。
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 將結果儲存為檔案或串流。無需呈現包含的形狀或手動裁切位圖。

如果段落在其父集合中找不到、沒有有效的呈現邊界,或無法呈現,則 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();
}

結果:

段落影像

在表格儲存格中以縮放呈現段落

使用接受 float scaleX 與 float scaleY 參數的 IParagraph.getImage 重載,以設定水平與垂直縮放係數。以下範例建立表格,並在第一個儲存格中以兩倍寬高的預設比例呈現段落,最後將結果儲存為 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 為段落層級設定,會套用至整個段落,與個別部份的格式無關。

若需在每行內垂直對齊不同字型大小的部份,請參閱 對齊行內字型。

我可以為段落的一部分設定校對語言嗎?

可以。對個別部份使用 IBasePortionFormat.setLanguageId,即可讓同一段落包含多種語言的文字。