在 Android 上格式化簡報文字

概述

本文說明如何在 PowerPoint 與 OpenDocument 簡報中使用 Aspose.Slides for Android via Java 進行文字格式設定。內容涵蓋背景色、透明度、字元間距、字型屬性、旋轉、段落間距、自動適應行為、文字錨點、定位點與語言設定等。

除非另有說明,範例皆使用 sample.pptx。其第一張投影片的第一個圖形是一個文字方塊,第一段落包含下列文字。投影片與圖形的索引皆為零基礎。使用粗體區段的範例會使用有效格式,包括繼承的粗體格式:

範例文字

如需尋找並突顯純文字或正則表達式匹配項目,請參閱 搜尋與取代文字。

設定文字背景顏色

使用 IParagraphFormat.getDefaultPortionFormat 來設定段落的預設突顯顏色,或使用 IBasePortionFormat.getHighlightColor 為個別文字區段設定突顯顏色。

以下範例將第一段落的預設突顯色設為淡灰色。個別區段的顯式突顯色會優先於此預設值:

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

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

    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    // 設定整段落的突顯顏色。
    paragraph.getParagraphFormat().getDefaultPortionFormat().getHighlightColor().setColor(Color.LTGRAY);

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

結果:

灰色段落

以下程式碼範例示範如何為 粗體字型的文字區段 設定背景顏色:

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

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

    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    for (IPortion portion : paragraph.getPortions()) {
        if (portion.getPortionFormat().getEffective().getFontBold()) {
            // 設定文字區段的突顯顏色。
            portion.getPortionFormat().getHighlightColor().setColor(Color.LTGRAY);
        }
    }

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

結果:

灰色文字區段

對齊文字段落

使用 IParagraphFormat.setAlignment 來設定文字框內段落的對齊方式。可設定為置中、左對齊、右對齊、兩端對齊等。

以下程式碼範例示範如何將段落對齊至 置中:

import com.aspose.slides.*;

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

    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    // 設定段落的對齊方式為置中。
    paragraph.getParagraphFormat().setAlignment(TextAlignment.Center);

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

結果:

已對齊的段落

在同一行內對齊字型

使用 IParagraphFormat.setFontAlignment 來垂直對齊一行內不同字型大小的文字區段。此設定適用於整個段落,並控制每行內的對齊方式。

以下獨立範例會在同一張投影片上建立四個有標籤的文字方塊。每個段落以 18、36、54 點的相同文字顯示,並使用不同的字型對齊方式。範例使用 Arial、停用自動適應與換行,且文字框足夠寬以容納單行文字。

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

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

    int[] alignments = { FontAlignment.Baseline, FontAlignment.Top, FontAlignment.Center, FontAlignment.Bottom };
    String[] alignmentNames = { "Baseline", "Top", "Center", "Bottom" };
    float[] fontSizes = { 18f, 36f, 54f };

    for (int i = 0; i < alignments.length; i++) {
        IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 30, 20 + i * 130, 660, 120);
        shape.getFillFormat().setFillType(FillType.NoFill);
        shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);

        ITextFrame textFrame = shape.getTextFrame();
        textFrame.getTextFrameFormat().setAnchoringType(TextAnchorType.Top);
        textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.None);
        textFrame.getTextFrameFormat().setWrapText(NullableBool.False);

        IParagraph label = textFrame.getParagraphs().get_Item(0);
        label.setText(alignmentNames[i]);
        label.getParagraphFormat().setAlignment(TextAlignment.Left);
        label.getParagraphFormat().getDefaultPortionFormat().setFontHeight(14);
        label.getParagraphFormat().getDefaultPortionFormat().setLatinFont(new FontData("Arial"));
        label.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
        label.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY);

        Paragraph paragraph = new Paragraph();
        paragraph.getParagraphFormat().setFontAlignment(alignments[i]);
        paragraph.getParagraphFormat().setAlignment(TextAlignment.Left);
        paragraph.getParagraphFormat().getDefaultPortionFormat().setLatinFont(new FontData("Arial"));
        paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
        paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);

        for (float fontSize : fontSizes) {
            Portion portion = new Portion("Ag ");
            portion.getPortionFormat().setFontHeight(fontSize);
            paragraph.getPortions().add(portion);
        }

        textFrame.getParagraphs().add(paragraph);
    }

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

結果:

基線、頂端、置中與底端字型對齊比較(混合字型大小)

字型對齊使用字型度量資訊,個別字母的可見邊緣不一定完全對齊。範例同時包含大寫字母與下行字元,以說明基線與底端對齊的差異。字型的可用性與替代、使用的字元以及字型大小差異皆會影響結果。框架尺寸、邊距、行距、換行與自動適應亦會影響版面;比較模式時請使用相同的字型與版面設定。

此設定與 IParagraphFormat.setAlignment(水平段落對齊)以及 ITextFrameFormat.setAnchoringType(在圖形內垂直定位文字區塊)不同。透過 IBasePortionFormat.setEscapement 進行的上標與下標格式會相對於基線移動個別區段,而不是為段落的行設定字型對齊。

設定文字透明度

文字透明度透過指派給 IBasePortionFormat.getFillFormat 的顏色之 Alpha 成分來控制。以下範例中,alpha = 50 為 0–255 範圍的 ARGB Alpha 通道值,並非透明度百分比。

以下程式碼範例示範如何為 整段落 套用透明度:

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

int alpha = 50;

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

    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    // 設定文字的填充顏色為透明顏色。
    paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
    paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.argb(alpha, 0, 0, 0));

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

結果:

透明段落

以下程式碼範例示範如何為 粗體字型的文字區段 套用透明度:

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

int alpha = 50;

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

    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    for (IPortion portion : paragraph.getPortions()) {
        if (portion.getPortionFormat().getEffective().getFontBold()) {
            // 設定文字區段的透明度。
            portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
            portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.argb(alpha, 0, 0, 0));
        }
    }

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

結果:

透明文字區段

設定文字字元間距

使用 IBasePortionFormat.setSpacing 來展開或壓縮文字方塊中字元之間的間距。以下範例加入 3 點間距,負值則壓縮文字。

以下 Java 程式碼示範如何在 整段落 展開字元間距:

import com.aspose.slides.*;

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

    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    // 注意:使用負值可壓縮字元間距。
    paragraph.getParagraphFormat().getDefaultPortionFormat().setSpacing(3); // 展開字元間距。

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

結果:

段落中的字元間距

以下程式碼範例示範如何在 粗體字型的文字區段 展開字元間距:

import com.aspose.slides.*;

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

    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    for (IPortion portion : paragraph.getPortions()) {
        if (portion.getPortionFormat().getEffective().getFontBold()) {
            // 注意:使用負值可壓縮字元間距。
            portion.getPortionFormat().setSpacing(3); // 展開字元間距。
        }
    }

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

結果:

文字區段中的字元間距

停用特定字型的字距調整

在某些情況下,Aspose.Slides 產生的文字看起來會比 PowerPoint 中的相同文字稍微緊密。這可能是因為 PowerPoint 會忽略某些字型的字距調整資料,即使該字型本身包含有效的字距資訊且在 PowerPoint 設定中已啟用字距調整。

若要使輸出更接近 PowerPoint,可對使用受影響字型的文字區段停用字距調整。將 IBasePortionFormat.setKerningMinimalSize 設為大於實際字型大小的值。本例需要「presentation.pptx」且其第一張投影片的第一個圖形為文字方塊。範例會檢查有效的字型名稱(含繼承字型),並對使用 Roboto 且字型大小低於 100 點的區段設定門檻,從而停用其字距調整:

import com.aspose.slides.*;

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

    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    String targetFont = "Roboto";

    for (IParagraph paragraph : autoShape.getTextFrame().getParagraphs()) {
        for (IPortion portion : paragraph.getPortions()) {
            IPortionFormatEffectiveData portionFormat = portion.getPortionFormat().getEffective();

            if ((portionFormat.getLatinFont() != null &&
                 portionFormat.getLatinFont().getFontName().equals(targetFont)) ||
                (portionFormat.getEastAsianFont() != null &&
                 portionFormat.getEastAsianFont().getFontName().equals(targetFont)) ||
                (portionFormat.getComplexScriptFont() != null &&
                 portionFormat.getComplexScriptFont().getFontName().equals(targetFont))) {
                portion.getPortionFormat().setKerningMinimalSize(100);
            }
        }
    }

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

對於低於門檻的符合條件文字,此設定會阻止字距調整,並可協助使 Aspose.Slides 的渲染結果與受此 PowerPoint 特定行為影響的字型的視覺輸出更為一致。

管理文字字型屬性

字型屬性可透過 IParagraphFormat.getDefaultPortionFormat 在段落層級設定,或透過 IPortionFormat 在個別區段設定。

以下範例將第一段落的預設字型設定為 12 點 Times New Roman,且使用粗體、斜體與點狀底線。個別區段的顯式格式會優先於這些預設值:

import com.aspose.slides.*;

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

    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    // 設定段落的字型屬性。
    paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(12);
    paragraph.getParagraphFormat().getDefaultPortionFormat().setFontBold(NullableBool.True);
    paragraph.getParagraphFormat().getDefaultPortionFormat().setFontItalic(NullableBool.True);
    paragraph.getParagraphFormat().getDefaultPortionFormat().setFontUnderline(TextUnderlineType.Dotted);
    paragraph.getParagraphFormat().getDefaultPortionFormat().setLatinFont(new FontData("Times New Roman"));

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

結果:

段落的字型屬性

以下範例對有效格式為粗體的區段套用 13 點 Times New Roman、斜體與點狀底線:

import com.aspose.slides.*;

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

    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

    for (IPortion portion : paragraph.getPortions()) {
        if (portion.getPortionFormat().getEffective().getFontBold()) {
            // 設定文字區段的字型屬性。
            portion.getPortionFormat().setFontHeight(13);
            portion.getPortionFormat().setFontItalic(NullableBool.True);
            portion.getPortionFormat().setFontUnderline(TextUnderlineType.Dotted);
            portion.getPortionFormat().setLatinFont(new FontData("Times New Roman"));
        }
    }

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

結果:

文字區段的字型屬性

設定文字旋轉

使用 ITextFrameFormat.setTextVerticalType 來設定圖形內的預定義文字方向。

以下程式碼範例將圖形內的文字方向設為 TextVerticalType.Vertical270,即文字 逆時針旋轉 90 度:

import com.aspose.slides.*;

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

    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    autoShape.getTextFrame().getTextFrameFormat().setTextVerticalType(TextVerticalType.Vertical270);

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

結果:

文字旋轉

為文字框設定自訂旋轉角度

使用 ITextFrameFormat.setRotationAngle 為 ITextFrame 設定自訂旋轉角度。

以下程式碼範例將文字框在圖形內順時針旋轉 3 度:

import com.aspose.slides.*;

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

    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    autoShape.getTextFrame().getTextFrameFormat().setRotationAngle(3);

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

結果:

自訂文字旋轉

設定段落行距

Aspose.Slides 提供 IParagraphFormat.setSpaceAfter、IParagraphFormat.setSpaceBefore 與 IParagraphFormat.setSpaceWithin 以控制段落間距。這些屬性的使用方式如下:

  • 正值指定行距為行高的百分比。
  • 負值指定行距的點數。

以下範例將第一段落的行距設定為行高的 200%(雙倍行距):

import com.aspose.slides.*;

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

    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);

    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
    paragraph.getParagraphFormat().setSpaceWithin(200);

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

結果:

段落內的行距

控制換行行為

段落換行規則在窄文字區塊或混合拉丁與東亞文字的簡報中相當有用。以下方法屬於 IParagraphFormat,因此適用於整個段落:

  • setLatinLineBreak 控制拉丁文字的換行規則。於混合文字中變更此設定也會影響相鄰東亞文字與標點的換行位置。
  • setEastAsianLineBreak 控制東亞文字的換行規則,包括行首與行尾字元的限制。

這些規則不會取代 ITextFrameFormat.setWrapText,後者會在文字框內啟用自動換行。規則會在換行發生時影響版面配置,但不會插入換行字元。使用明確的換行符號則會在段落內強制另起新行,與可用寬度無關。

以下獨立範例會建立包含中文與拉丁文字的窄文字區塊,明確設定兩項換行選項並儲存為「line_breaking.pptx」。若要測試任一規則,請在維持另一設定不變的前提下變更對應的值。範例使用 24 點 Arial 與 SimSun,框寬 160 點且水平文字框邊距為零。呼叫 ITextFrameFormat.setAutofitType 並傳入 TextAutofitType.None,以固定文字大小與框架尺寸。

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, 160, 300);
    shape.getFillFormat().setFillType(FillType.NoFill);

    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getTextFrameFormat().setWrapText(NullableBool.True);
    textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.None);
    textFrame.getTextFrameFormat().setMarginLeft(0);
    textFrame.getTextFrameFormat().setMarginRight(0);

    IParagraph paragraph = textFrame.getParagraphs().get_Item(0);
    paragraph.setText("中文排版测试,PowerPoint 中文演示。");

    IParagraphFormat format = paragraph.getParagraphFormat();
    format.setAlignment(TextAlignment.Left);
    format.getDefaultPortionFormat().setFontHeight(24);
    FontData latinFont = new FontData("Arial");
    format.getDefaultPortionFormat().setLatinFont(latinFont);
    FontData eastAsianFont = new FontData("SimSun");
    format.getDefaultPortionFormat().setEastAsianFont(eastAsianFont);
    format.getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
    format.getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    format.setLatinLineBreak(NullableBool.False);
    format.setEastAsianLineBreak(NullableBool.True);

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

控制懸掛標點符號

IParagraphFormat.setHangingPunctuation 允許符合條件的標點符號延伸到文字行右側之外,而非佔用下一行。此屬性適用於整個段落,且不同於懸掛縮排。

以下獨立範例在寬度為 100 點的文字框內啟用懸掛標點,並儲存為「hanging_punctuation.pptx」。使用 24 點 Arial 且水平文字框邊距為零,最終的句點會保留在「sentence」之後,並延伸至文字右側。將屬性設為 NullableBool.False 可作比較:此設定下,句點會佔據單獨一行。已啟用換行且停用自動適應,以保持可用寬度固定。

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, 100, 200);
    shape.getFillFormat().setFillType(FillType.NoFill);

    ITextFrame textFrame = shape.getTextFrame();
    textFrame.getTextFrameFormat().setWrapText(NullableBool.True);
    textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.None);
    textFrame.getTextFrameFormat().setMarginLeft(0);
    textFrame.getTextFrameFormat().setMarginRight(0);

    IParagraph paragraph = textFrame.getParagraphs().get_Item(0);
    paragraph.setText("Simple text, next sentence.");

    IParagraphFormat format = paragraph.getParagraphFormat();
    format.setAlignment(TextAlignment.Left);
    format.getDefaultPortionFormat().setFontHeight(24);
    FontData latinFont = new FontData("Arial");
    format.getDefaultPortionFormat().setLatinFont(latinFont);
    format.getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
    format.getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    format.setHangingPunctuation(NullableBool.True);

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

並非所有標點符號皆可懸掛。上述 字型與版面條件 亦同樣影響此比較:變更字型、可用寬度、邊距或自動適應設定,都可能使可見差異消失。

設定文字框的自動適應類型

ITextFrameFormat.setAutofitType 決定文字在超出容器邊界時的行為。可用來控制文字是縮小、溢出,或自動調整圖形尺寸。以下範例將圖形設定為依文字自動調整大小,並儲存結果為「autofit_type.pptx」。

import com.aspose.slides.*;

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

    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    autoShape.getTextFrame().getTextFrameFormat().setAutofitType(TextAutofitType.Shape);

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

若要在自動換行後統計行數並觀察文字或圖形寬度變化的結果,請參閱 Count Rendered Lines。僅行數不足以表示文字是否溢出容器。

設定文字框的錨點

ITextFrameFormat.setAnchoringType 定義文字在圖形內的垂直定位方式,例如置頂、置中或置底。以下範例將文字錨點設為第一個圖形的底部,並將結果儲存為「text_anchor.pptx」。

import com.aspose.slides.*;

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

    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    autoShape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Bottom);

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

設定文字定位點

使用 IParagraphFormat.setDefaultTabSize 以及 IParagraphFormat.getTabs 來配置段落中的定位點。以下範例將預設定位點間距設為 100 點,並在 30 點處加入左對齊的定位點。此設定會影響包含定位字元的文字。

import com.aspose.slides.*;

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

    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);

    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
    paragraph.getParagraphFormat().setDefaultTabSize(100);
    paragraph.getParagraphFormat().getTabs().add(30, TabAlignment.Left);

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

結果:

段落定位點

設定校對語言

Aspose.Slides 提供 IBasePortionFormat.setLanguageId,可為文字區段設定校對語言。校對語言決定 PowerPoint 中拼寫與文法檢查所使用的語言。

以下範例需要「presentation.pptx」且其第一張投影片的第一個圖形為文字方塊,且至少有一個段落。範例會將第一段落的內容替換為「1。」,設定其字型為 SimSun,並指派簡體中文校對語言 (zh-CN),最後儲存為「proofing_language.pptx」:

import com.aspose.slides.*;

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

    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);

    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
    paragraph.getPortions().clear();

    FontData font = new FontData("SimSun");

    Portion textPortion = new Portion();
    textPortion.getPortionFormat().setComplexScriptFont(font);
    textPortion.getPortionFormat().setEastAsianFont(font);
    textPortion.getPortionFormat().setLatinFont(font);

    // 設定校對語言的 Id。
    textPortion.getPortionFormat().setLanguageId("zh-CN");

    textPortion.setText("1。");
    paragraph.getPortions().add(textPortion);

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

設定預設語言

使用 LoadOptions.setDefaultTextLanguage 來定義在載入或建立簡報時所建立文字的預設語言。以下範例建立一個預設文字語言為美式英語的簡報,加入文字方塊,並對其第一個文字區段印出 en-US。

import com.aspose.slides.*;

LoadOptions loadOptions = new LoadOptions();
loadOptions.setDefaultTextLanguage("en-US");

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

    // 新增一個帶文字的矩形圖形。
    IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 20, 150, 50);
    shape.getTextFrame().setText("Sample text");

    // 檢查第一個文字區段的語言。
    IPortion portion = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
    System.out.println(portion.getPortionFormat().getLanguageId());
} finally {
    presentation.dispose();
}

設定預設文字樣式

若要在簡報層級套用預設文字格式,請使用 IPresentation.getDefaultTextStyle。

以下範例將新簡報中頂層段落的預設字型設定為 14 點粗體,並儲存為「default_text_style.pptx」。文字會在未另行指定格式時繼承這些預設值,除非較具體的格式覆寫。

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    // 取得頂層段落格式。
    IParagraphFormat paragraphFormat = presentation.getDefaultTextStyle().getLevel(0);

    if (paragraphFormat != null) {
        paragraphFormat.getDefaultPortionFormat().setFontHeight(14);
        paragraphFormat.getDefaultPortionFormat().setFontBold(NullableBool.True);
    }

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

提取全部大寫效果的文字

在 PowerPoint 中,套用 全部大寫 字型效果會使投影片上的文字以大寫顯示,即使原始輸入為小寫。使用 Aspose.Slides 取得此類文字區段時,函式庫會回傳原始輸入的文字。若要匹配顯示的文字,請檢查 TextCapType 並在值為 All 時將回傳字串轉為大寫。

此範例需要「sample2.pptx」且其第一張投影片的第一個圖形為文字方塊。其第一段落的第一個區段包含「Hello, Aspose!」並套用了全部大寫效果,如下圖所示。

全部大寫效果

以下程式碼範例示範如何提取套用 全部大寫 效果的文字:

import com.aspose.slides.*;

Presentation presentation = new Presentation("sample2.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    
    IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
    IPortion textPortion = autoShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);

    System.out.println("Original text: " + textPortion.getText());

    IPortionFormatEffectiveData textFormat = textPortion.getPortionFormat().getEffective();
    if (textFormat.getTextCapType() == TextCapType.All) {
        String text = textPortion.getText().toUpperCase();
        System.out.println("All-Caps effect: " + text);
    }
} finally {
    presentation.dispose();
}

輸出:

Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!

常見問與答

如何在投影片的表格中修改文字?

要在投影片的表格中修改文字,請使用 ITable。遍歷儲存格,並透過 ICell.getTextFrame 以及 IParagraph.getParagraphFormat 更新每個儲存格的文字與段落格式。

如何在 PowerPoint 投影片的文字上套用漸層色彩?

要在文字上套用漸層色彩,請使用 IBasePortionFormat.getFillFormat。將 IFillFormat.setFillType 設為 FillType.Gradient,並配置漸層停點、方向與透明度。