在 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();
}
复制语言元数据
从模板复制多语言文本时,需要保留 IBasePortionFormat.getLanguageId 和 IBasePortionFormat.getAlternativeLanguageId。以下独立示例创建一个源片段,其语言为美式英文,备用语言为法语,并将文本和标识符复制到新演示文稿中的一个片段。
import com.aspose.slides.*;
Portion source = new Portion("Hello! Bonjour!");
source.getPortionFormat().setLanguageId("en-US");
source.getPortionFormat().setAlternativeLanguageId("fr-FR");
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 20, 400, 80);
IParagraph paragraph = shape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getPortions().clear();
Portion destination = new Portion();
destination.setText(source.getText());
destination.getPortionFormat().setLanguageId(source.getPortionFormat().getLanguageId());
// 如果源没有备用语言,null 会清除目标的该值。
destination.getPortionFormat().setAlternativeLanguageId(source.getPortionFormat().getAlternativeLanguageId());
paragraph.getPortions().add(destination);
presentation.save("copied_language_metadata.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
若要尝试没有备用语言的源,请省略对 "fr-FR" 的赋值。这时备用标识符为 null,相同的复制语句会保留其缺失,而不会引入默认值,即使目标先前已分配了备用语言。
这些标识符存储为语言元数据。保留它们并不决定 PowerPoint 使用哪个词典进行拼写或语法检查。
设置默认语言
使用 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();
}
输出:
Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!
常见问题
如何在幻灯片上的表格中修改文本?
要在幻灯片的表格中修改文本,请使用 ITable。遍历单元格,并通过 ICell.getTextFrame 更新每个单元格的文本框,以及通过 IParagraph.getParagraphFormat 更新段落格式。
如何在 PowerPoint 幻灯片的文本上应用渐变颜色?
要对文本应用渐变颜色,请使用 IBasePortionFormat.getFillFormat。将 IFillFormat.setFillType 设置为 FillType.Gradient 并配置渐变点、方向和透明度。