在 Android 上格式化演示文稿文本
概述
本文介绍如何使用 Aspose.Slides for Android via Java 对 PowerPoint 和 OpenDocument 演示文稿中的文本进行格式化。内容包括高亮显示、背景颜色、透明度、字符间距、字体属性、旋转、段落间距、自动适应行为、文本锚点、制表位以及语言设置。
在下面的示例中,我们将使用名为 “sample.pptx” 的文件,该文件在第一页包含一个仅有以下文本的文本框:

高亮文本
当需要高亮显示文本框中与特定样本匹配的文本时,请使用ITextFrame.highlightText 方法。该方法会为匹配的文本片段应用高亮颜色,并可配合ITextSearchOptions 控制搜索方式,例如仅匹配完整单词。
下面的代码示例先高亮所有出现的 “try” 字符,然后仅高亮完整单词 “to”。
Presentation presentation = new Presentation("sample.pptx");
try {
// 获取第一张幻灯片中的第一个形状。
IAutoShape shape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
// 在形状中高亮单词 "try"。
shape.getTextFrame().highlightText("try", Color.rgb(173, 216, 230));
TextSearchOptions searchOptions = new TextSearchOptions();
searchOptions.setWholeWordsOnly(true);
// 在形状中高亮单词 "to"。
int violetColor = Color.rgb(238, 130, 238);
shape.getTextFrame().highlightText("to", violetColor, searchOptions, null);
presentation.save("highlighted_text.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
结果:

使用正则表达式高亮文本
ITextFrame.highlightRegex 方法可高亮正则表达式匹配到的文本。
下面的代码示例高亮所有包含 七个或以上字符 的单词:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape shape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
java.util.regex.Pattern regex = java.util.regex.Pattern.compile("\\b[^\\s]{7,}\\b");
// 高亮所有包含七个或以上字符的单词。
shape.getTextFrame().highlightRegex(regex, Color.YELLOW, null);
presentation.save("highlighted_text_using_regex.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
结果:

设置文本背景颜色
使用IParagraphFormat.getDefaultPortionFormat 为段落设置默认高亮颜色,或使用IBasePortionFormat.getHighlightColor 为单独的文本片段设置。
下面的代码示例演示如何为 整个段落 设置背景颜色:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).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();
}
结果:

下面的代码示例演示如何为 粗体字体的文本片段 设置背景颜色:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
for (int portionIndex = 0; portionIndex < paragraph.getPortions().getCount(); portionIndex++) {
IPortion portion = paragraph.getPortions().get_Item(portionIndex);
if (portion.getPortionFormat().getEffective().getFontBold()) {
// 为文本片段设置高亮颜色。
portion.getPortionFormat().getHighlightColor().setColor(Color.LTGRAY);
}
}
presentation.save("gray_text_portions.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
结果:

对齐文本段落
使用IParagraphFormat.setAlignment 可设置文本框中段落的对齐方式。可选值包括居中、左对齐、右对齐、两端对齐等。
下面的代码示例演示如何将段落对齐到 居中:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).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();
}
结果:

设置文本透明度
文本透明度通过分配给IBasePortionFormat.getFillFormat 的颜色的 Alpha 分量来控制。以下示例中的 alpha = 50 为 0-255 范围内的 ARGB Alpha 通道值,而非透明度百分比。
下面的代码示例演示如何为 整个段落 应用透明度:
int alpha = 50;
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).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();
}
结果:

下面的代码示例演示如何为 粗体字体的文本片段 应用透明度:
int alpha = 50;
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
for (int portionIndex = 0; portionIndex < paragraph.getPortions().getCount(); portionIndex++) {
IPortion portion = paragraph.getPortions().get_Item(portionIndex);
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 可在文本框中扩大或压缩字符之间的间距。
下面的 Java 代码演示如何在 整个段落 中扩大字符间距:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).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();
}
结果:

下面的代码示例演示如何在 粗体字体的文本片段 中扩大字符间距:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
for (int portionIndex = 0; portionIndex < paragraph.getPortions().getCount(); portionIndex++) {
IPortion portion = paragraph.getPortions().get_Item(portionIndex);
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 presentation = new Presentation("presentation.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
String targetFont = "Roboto";
for (int paragraphIndex = 0; paragraphIndex < autoShape.getTextFrame().getParagraphs().getCount(); paragraphIndex++) {
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(paragraphIndex);
for (int portionIndex = 0; portionIndex < paragraph.getPortions().getCount(); portionIndex++) {
IPortion portion = paragraph.getPortions().get_Item(portionIndex);
IFontData latinFont = portion.getPortionFormat().getLatinFont();
IFontData eastAsianFont = portion.getPortionFormat().getEastAsianFont();
IFontData complexScriptFont = portion.getPortionFormat().getComplexScriptFont();
boolean usesTargetFont =
latinFont != null && targetFont.equals(latinFont.getFontName()) ||
eastAsianFont != null && targetFont.equals(eastAsianFont.getFontName()) ||
complexScriptFont != null && targetFont.equals(complexScriptFont.getFontName());
if (usesTargetFont) {
portion.getPortionFormat().setKerningMinimalSize(100);
}
}
}
presentation.save("output.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
此设置可阻止对匹配的文本片段应用连字,从而帮助 Aspose.Slides 的渲染效果与 PowerPoint 在受影响字体上的视觉表现保持一致。
管理文本字体属性
字体属性可以通过IParagraphFormat.getDefaultPortionFormat 在段落层面设置,也可以通过IPortionFormat 在单个片段上设置。
下面的代码为整个段落设置字体和文本样式:对段落中的所有片段应用字体大小、粗体、斜体、点状下划线以及 Times New Roman 字体。
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).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();
}
结果:

下面的代码示例对 粗体字体的文本片段 应用类似的属性:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
for (int portionIndex = 0; portionIndex < paragraph.getPortions().getCount(); portionIndex++) {
IPortion portion = paragraph.getPortions().get_Item(portionIndex);
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 可在形状内设置预定义的文本方向。
下面的代码示例将形状中文本方向设置为 Vertical270,即逆时针旋转 90 度:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setTextVerticalType(TextVerticalType.Vertical270);
presentation.save("text_rotation.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
结果:

为文本框设置自定义旋转
使用ITextFrameFormat.setRotationAngle 可为ITextFrame 设置自定义旋转角度。
下面的代码示例将文本框在形状内顺时针旋转 3 度:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).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 来控制段落间距。使用方式如下:
- 正值表示按行高的百分比指定行间距。
- 负值表示以点数指定行间距。
下面的代码示例演示如何在段落内部指定行间距:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).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();
}
结果:

设置文本框的自动适应类型
ITextFrameFormat.setAutofitType 决定文本在超出容器边界时的行为。可用于控制文本是收缩、溢出还是自动调整形状大小。
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setAutofitType(TextAutofitType.Shape);
presentation.save("autofit_type.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
设置文本框的锚点
ITextFrameFormat.setAnchoringType 定义文本在形状内部的垂直位置,例如顶部、居中或底部。
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Bottom);
presentation.save("text_anchor.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
设置文本制表位
使用IParagraphFormat.setDefaultTabSize 和IParagraphFormat.getTabs 可配置段落中的制表位。
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).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 presentation = new Presentation("presentation.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).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 可定义在加载或创建演示文稿时创建的文本的默认语言。
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 磅、粗体的默认字体样式。
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 效果的文本:
Presentation presentation = new Presentation("sample2.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).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,并配置渐变止点、方向和透明度。