在 Java 中格式化簡報文字
概觀
本文說明如何使用 Aspose.Slides for Java 來格式化 PowerPoint 與 OpenDocument 簡報中的文字。它涵蓋背景顏色、透明度、字元間距、字型屬性、旋轉、段落間距、自動適應行為、文字錨點、定位點及語言設定。
除非另有說明,範例皆使用 sample.pptx。其第一張投影片的第一個圖形是一個文字方塊,且其第一段包含以下顯示的文字。投影片與圖形的索引均為零基礎。選取粗體部分的範例使用有效格式,包括繼承的粗體格式:

若要尋找並標示文字或正則表達式匹配項目,請參閱 Search and Replace Text。
設定文字背景顏色
使用 IParagraphFormat.getDefaultPortionFormat 為段落設定預設標記顏色,或使用 IBasePortionFormat.getHighlightColor 為個別文字部分設定標記顏色。
以下範例將淡灰色標記設為第一段的預設。個別部分的明確標記顏色會優先於此預設:
import com.aspose.slides.*;
import java.awt.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.LIGHT_GRAY);
presentation.save("gray_paragraph.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
結果:

以下程式碼示範如何為 具有粗體字型的文字部分 設定背景顏色:
import com.aspose.slides.*;
import java.awt.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.LIGHT_GRAY);
}
}
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 java.awt.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 java.awt.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(new Color(0, 0, 0, alpha));
presentation.save("transparent_paragraph.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
結果:

以下程式碼示範如何將透明度套用至 具有粗體字型的文字部分:
import com.aspose.slides.*;
import java.awt.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(new Color(0, 0, 0, alpha));
}
}
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();
}
結果:

設定文字旋轉
使用 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 java.awt.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 java.awt.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 中,套用 All Caps 字型效果會使文字在投影片上以大寫形式顯示,即使原始輸入為小寫。使用 Aspose.Slides 取得此類文字部分時,函式庫會回傳原始輸入的文字。為了匹配顯示的文字,請檢查 TextCapType,並在值為 All 時將回傳的字串轉為大寫。
此範例需要「sample2.pptx」且首張投影片的第一個圖形為文字方塊。其第一段的第一個部分包含「Hello, Aspose!」並套用 All Caps 效果,如下所示:

以下程式碼示範如何擷取套用 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();
}
輸出:
Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!
常見問題
如何在投影片的表格中修改文字?
使用 ITable 於投影片的表格中修改文字,遍歷儲存格,並透過 ICell.getTextFrame 取得文字框,使用 IParagraph.getParagraphFormat 變更段落格式。
如何在 PowerPoint 投影片上的文字套用漸層顏色?
使用 IBasePortionFormat.getFillFormat 取得填充格式。將 IFillFormat.setFillType 設為 FillType.Gradient,並配置漸層停點、方向與透明度。