在 Android 上格式化簡報文字
概述
本文說明如何使用 Aspose.Slides for Android 透過 Java 為 PowerPoint 與 OpenDocument 簡報中的文字進行格式設定。內容涵蓋背景色、透明度、字元間距、字型屬性、旋轉、段落間距、自動調整行為、文字錨點、定位點以及語言設定。
除非另有說明,範例皆使用 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 點的門檻。這會對字型大小低於 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 特定行為影響的字型在 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();
}
結果如下:

控制文字高度正規化
在重現範本排版或研究字元高度差異時,使用 IBasePortionFormat.setNormaliseHeight 。在 PowerPoint 中,啟用正規化會垂直拉伸或壓縮單個字元,使其在文字部份內的可見高度統一。但這並不保證不同字型之間的文字高度相等。
將此屬性設為 NullableBool.True 或 False 以明確啟用或停用正規化。NullableBool.NotDefined 會讓本地設定保持未設定,讓部份繼承其值,例如來自段落的 getDefaultPortionFormat。它並不等同於 False。可使用 IPortionFormat.getEffective 並讀取 IBasePortionFormatEffectiveData.getNormaliseHeight 以取得解析後的布林值。
以下獨立範例需要安裝 Arial。它會建立兩個文字區塊,內容包含相同的大寫字母、小寫字母、下緣伸展字元與重音字元,字型大小為 54 點。兩個段落預設皆啟用正規化。上方部份明確停用,下方部份則繼承設定。框架尺寸、邊距與段落間距相同,且停用換行與自動調整,以保持比較的一致性。程式會輸出 False: false 與 NotDefined: true,然後保存為「text_height_normalization.pptx」。
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
byte[] settings = { NullableBool.False, NullableBool.NotDefined };
String[] settingNames = { "False", "NotDefined" };
for (int i = 0; i < settings.length; i++) {
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 30, 60 + i * 200, 660, 150);
shape.getFillFormat().setFillType(FillType.NoFill);
shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
ITextFrame textFrame = shape.getTextFrame();
textFrame.setText("Hxgjpq ÅÉ 0123");
textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.None);
textFrame.getTextFrameFormat().setWrapText(NullableBool.False);
textFrame.getTextFrameFormat().setAnchoringType(TextAnchorType.Top);
textFrame.getTextFrameFormat().setMarginTop(0);
textFrame.getTextFrameFormat().setMarginLeft(0);
IParagraph paragraph = textFrame.getParagraphs().get_Item(0);
paragraph.getParagraphFormat().setAlignment(TextAlignment.Left);
paragraph.getParagraphFormat().setSpaceBefore(0);
paragraph.getParagraphFormat().setSpaceAfter(0);
paragraph.getParagraphFormat().setSpaceWithin(100);
paragraph.getParagraphFormat().getDefaultPortionFormat().setNormaliseHeight(NullableBool.True);
IPortion portion = paragraph.getPortions().get_Item(0);
FontData font = new FontData("Arial");
portion.getPortionFormat().setLatinFont(font);
portion.getPortionFormat().setFontHeight(54);
portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
portion.getPortionFormat().setNormaliseHeight(settings[i]);
System.out.println(settingNames[i] + ": " + portion.getPortionFormat().getEffective().getNormaliseHeight());
}
presentation.save("text_height_normalization.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
以下比較圖是從已保存的簡報匯出的。上排的正規化已停用 (False),下排則繼承已啟用的設定 (NotDefined → True)。請保持安裝 Arial 以重現此外觀。

正規化會改變字元高度,但不會改變已指派的 getFontHeight 值,該值以點數指定字型大小。段落的 setSpaceWithin 控制行距,而文字框的 setAutofitType 控制文字或其容器如何自動調整以適應。這些設定各有不同用途,且會獨立影響整體版面。欲了解更多段落格式設定與繼承,請參閱 管理段落。
套用與移除刪除線
在文字部份上使用 IBasePortionFormat.setStrikethroughType 可顯示更正、取消的詞彙或先前的價格,而不刪除原始文字。將其設定為 TextStrikethroughType.Single 或 Double 可在文字上加上一條或兩條刪除線。
若要移除刪除線,請傳入 TextStrikethroughType.None。此設定會明確停用效果,即使周圍的格式指定了刪除線。相較之下, TextStrikethroughType.NotDefined 會讓本地設定保持未定義,允許部份繼承格式,例如來自段落。它並不保證刪除線會被移除。可使用 IPortionFormat.getEffective 讀取最終的格式,包括繼承的值。
以下獨立範例會建立三個價格比較。每個段落都包含標籤、先前價格與目前價格的不同部份,故僅先前價格會受到影響。第一列先套用再移除刪除線;其他列則保留單線與雙線刪除線。變更此設定可保留文字及其他格式,例如粗體的先前價格。
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, 620, 220);
shape.getFillFormat().setFillType(FillType.NoFill);
shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
shape.getTextFrame().getParagraphs().clear();
String[] labels = { "None: ", "Single: ", "Double: " };
for (String label : labels) {
Paragraph paragraph = new Paragraph();
paragraph.getParagraphFormat().setAlignment(TextAlignment.Left);
paragraph.getParagraphFormat().setSpaceAfter(20);
IPortionFormat format = paragraph.getParagraphFormat().getDefaultPortionFormat();
format.setFontHeight(28);
FontData font = new FontData("Arial");
format.setLatinFont(font);
format.getFillFormat().setFillType(FillType.Solid);
format.getFillFormat().getSolidFillColor().setColor(Color.BLACK);
Portion labelPortion = new Portion(label);
paragraph.getPortions().add(labelPortion);
Portion previousPrice = new Portion("$100");
previousPrice.getPortionFormat().setFontBold(NullableBool.True);
paragraph.getPortions().add(previousPrice);
Portion currentPrice = new Portion(" → $80");
paragraph.getPortions().add(currentPrice);
shape.getTextFrame().getParagraphs().add(paragraph);
}
IParagraphCollection paragraphs = shape.getTextFrame().getParagraphs();
// 套用刪除線,然後明確移除第一個先前價格的刪除線。
paragraphs.get_Item(0).getPortions().get_Item(1).getPortionFormat().setStrikethroughType(TextStrikethroughType.Single);
paragraphs.get_Item(0).getPortions().get_Item(1).getPortionFormat().setStrikethroughType(TextStrikethroughType.None);
paragraphs.get_Item(1).getPortions().get_Item(1).getPortionFormat().setStrikethroughType(TextStrikethroughType.Single);
paragraphs.get_Item(2).getPortions().get_Item(1).getPortionFormat().setStrikethroughType(TextStrikethroughType.Double);
presentation.save("strikethrough.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 點,水平文字框邊距為 0。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 且水平文字框邊距為 0,最後的句點會留在「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();
}
若要在自動換行後計算行數,並查看文字或圖形寬度變更對結果的影響,請參閱 計算已呈現行數。僅行數並不能判斷文字是否溢出容器。
設定文字框的錨點
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 中,套用 All Caps(全大寫)字型效果會使文字在投影片上以大寫顯示,即使原本是小寫。若使用 Aspose.Slides 取得此類文字部份,函式庫會回傳原始輸入的文字。若要匹配顯示的文字,請檢查 TextCapType,當值為 All 時將回傳的字串轉為大寫。
此範例需要「sample2.pptx」,其第一張投影片的第一個圖形為文字方塊。第一段落的第一部份包含套用 All Caps 效果的「Hello, Aspose!」,如下圖所示。

以下程式碼示範如何擷取套用 All Caps 效果的文字:
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();
}
輸出:
原始文字: Hello, Aspose!
全大寫效果: HELLO, ASPOSE!
常見問題
如何在投影片的表格中修改文字?
在投影片的表格中修改文字,可使用 ITable。遍歷儲存格,並透過 ICell.getTextFrame 以及 IParagraph.getParagraphFormat 來更新每個儲存格的文字框與段落格式。
如何在 PowerPoint 投影片的文字上套用漸層顏色?
若要為文字套用漸層顏色,可使用 IBasePortionFormat.getFillFormat。將 IFillFormat.setFillType 設為 FillType.Gradient,並設定漸層停靠點、方向與透明度。