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

突出显示文本
当需要在文本框中突出显示匹配特定样本的文本时,请使用 ITextFrame.highlightText 方法。该方法会对匹配的文本片段应用高亮颜色,并可配合 TextSearchOptions 控制搜索方式,例如仅匹配完整单词。
下面的代码示例先突出显示所有出现的 “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.LIGHT_GRAY);
TextSearchOptions searchOptions = new TextSearchOptions();
searchOptions.setWholeWordsOnly(true);
// 在形状中突出显示单词 "to"。
shape.getTextFrame().highlightText("to", Color.MAGENTA, searchOptions, null);
presentation.save("highlighted_text.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
结果:

使用正则表达式突出显示文本
ITextFrame.highlightRegex 方法可突出显示正则表达式匹配到的文本。在 Java 中,此 API 通过 ITextFrame 暴露。
下面的代码示例突出显示所有包含 七个或更多字符 的单词:
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.LIGHT_GRAY);
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 (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 可以设置文本框内段落的对齐方式,可选居中、左对齐、右对齐、两端对齐等。
以下代码示例演示如何将段落对齐到 居中:
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 透明通道值,而不是百分比。
下面的代码示例展示如何对 整个段落 应用透明度:
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(new Color(0, 0, 0, alpha));
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 (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 可以在文本框中扩大或收缩字符之间的间距。
以下 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 (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 presentation = new Presentation("presentation.pptx");
try {
IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).getShapes().get_Item(0);
String targetFont = "Roboto";
for (IParagraph paragraph : autoShape.getTextFrame().getParagraphs()) {
for (IPortion portion : paragraph.getPortions()) {
IPortionFormat portionFormat = portion.getPortionFormat();
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))) {
portionFormat.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 (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 可以在形状内部设置预定义的文本方向。
以下代码示例将形状内的文本方向设置为 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,并配置渐变止点、方向以及透明度。