在 Java 中格式化演示文稿文本
概述
本文演示如何使用 Aspose.Slides for Java 对 PowerPoint 和 OpenDocument 演示文稿中的文本进行格式化。内容涵盖背景颜色、透明度、字符间距、字体属性、旋转、段落间距、自动适应行为、文本锚定、制表位以及语言设置。
除非另有说明,示例均使用 sample.pptx。其第一张幻灯片的第一个形状是一个文本框,首段包含下面显示的文本。幻灯片和形状的索引均从零开始。选择粗体部分的示例使用有效格式,包括继承的粗体格式:

要查找并突出显示文字字面值或正则表达式匹配项,请参阅 搜索和替换文本。
设置文本背景颜色
使用 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 表示 ARGB 透明度通道值,范围为 0–255,而非透明度百分比。
下面的代码示例展示如何对 整个段落 应用透明度:
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,并配置渐变停止、方向和透明度。