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

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

使用正则表达式高亮文本
TextFrame::highlightRegex 方法会高亮正则表达式匹配的文本。
下面的代码示例高亮所有包含 七个或更多字符 的单词:
$presentation = new Presentation("sample.pptx");
try {
$shape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
// 高亮所有包含七个或更多字符的单词。
$shape->getTextFrame()->highlightRegex("\\b[^\\s]{7,}\\b", java("java.awt.Color")->YELLOW, null);
$presentation->save("highlighted_text_using_regex.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}

设置文本背景颜色
使用 ParagraphFormat 的默认部分格式来设置段落的默认高亮颜色,或使用 PortionFormat 为单个文本部分设置颜色。
下面的代码示例展示如何为 整个段落 设置背景颜色:
$presentation = new Presentation("sample.pptx");
try {
$autoShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
// 为整个段落设置高亮颜色。
$paragraph->getParagraphFormat()->getDefaultPortionFormat()->getHighlightColor()->setColor(java("java.awt.Color")->LIGHT_GRAY);
$presentation->save("gray_paragraph.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}

下面的代码示例演示如何为 加粗字体的文本部分 设置背景颜色:
$presentation = new Presentation("sample.pptx");
try {
$autoShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
$portionCount = java_values($paragraph->getPortions()->getCount());
for ($portionIndex = 0; $portionIndex < $portionCount; $portionIndex++) {
$portion = $paragraph->getPortions()->get_Item($portionIndex);
if (java_values($portion->getPortionFormat()->getEffective()->getFontBold()) === NullableBool::True) {
// 为文本部分设置高亮颜色。
$portion->getPortionFormat()->getHighlightColor()->setColor(java("java.awt.Color")->LIGHT_GRAY);
}
}
$presentation->save("gray_text_portions.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}

对齐文本段落
使用 ParagraphFormat::setAlignment 方法在文本框内设置段落对齐方式。该值可以是居中、左对齐、右对齐、两端对齐等。
下面的代码示例展示如何将段落对齐到 居中:
$presentation = new Presentation("sample.pptx");
try {
$autoShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
// 将段落的对齐方式设置为居中。
$paragraph->getParagraphFormat()->setAlignment(TextAlignment::Center);
$presentation->save("aligned_paragraph.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}

设置文本透明度
文本透明度通过分配给 PortionFormat 填充格式的颜色的 alpha 分量进行控制。在下面的示例中,alpha = 50 是 0-255 规模上的 ARGB alpha 通道值,而不是透明度百分比。
下面的代码示例展示如何对 整个段落 应用透明度:
$alpha = 50;
$presentation = new Presentation("sample.pptx");
try {
$autoShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
$fillFormat = $paragraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat();
// 将文本的填充颜色设置为透明颜色。
$fillFormat->setFillType(FillType::Solid);
$fillFormat->getSolidFillColor()->setColor(new Java("java.awt.Color", 0, 0, 0, $alpha));
$presentation->save("transparent_paragraph.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}

下面的代码示例展示如何对 加粗字体的文本部分 应用透明度:
$alpha = 50;
$presentation = new Presentation("sample.pptx");
try {
$autoShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
$portionCount = java_values($paragraph->getPortions()->getCount());
for ($portionIndex = 0; $portionIndex < $portionCount; $portionIndex++) {
$portion = $paragraph->getPortions()->get_Item($portionIndex);
if (java_values($portion->getPortionFormat()->getEffective()->getFontBold()) === NullableBool::True) {
// 设置文本部分的透明度。
$fillFormat = $portion->getPortionFormat()->getFillFormat();
$fillFormat->setFillType(FillType::Solid);
$fillFormat->getSolidFillColor()->setColor(new Java("java.awt.Color", 0, 0, 0, $alpha));
}
}
$presentation->save("transparent_text_portions.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}

设置文本字符间距
使用 BasePortionFormat::setSpacing 方法来扩展或压缩文本框中字符之间的间距。
下面的 PHP 代码展示如何在 整个段落 中扩展字符间距:
$presentation = new Presentation("sample.pptx");
try {
$autoShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
$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 = new Presentation("sample.pptx");
try {
$autoShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
$portionCount = java_values($paragraph->getPortions()->getCount());
for ($portionIndex = 0; $portionIndex < $portionCount; $portionIndex++) {
$portion = $paragraph->getPortions()->get_Item($portionIndex);
if (java_values($portion->getPortionFormat()->getEffective()->getFontBold()) === NullableBool::True) {
// 注意:使用负值来压缩字符间距。
$portion->getPortionFormat()->setSpacing(3); // 扩展字符间距。
}
}
$presentation->save("character_spacing_in_text_portions.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}

为特定字体禁用字距调整
在某些情况下,Aspose.Slides 渲染的文本可能比 PowerPoint 中显示的相同文本略显紧凑。这可能是因为 PowerPoint 会忽略某些字体的字距信息,即使该字体包含有效的字距数据且在 PowerPoint 设置中已启用字距调整。
为使渲染输出更接近 PowerPoint,可以为使用受影响字体的文本部分禁用字距调整。将 BasePortionFormat::setKerningMinimalSize 方法设置为远大于实际字体大小的值:
$presentation = new Presentation("presentation.pptx");
try {
$autoShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
$targetFont = "Roboto";
$paragraphCount = java_values($autoShape->getTextFrame()->getParagraphs()->getCount());
for ($paragraphIndex = 0; $paragraphIndex < $paragraphCount; $paragraphIndex++) {
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item($paragraphIndex);
$portionCount = java_values($paragraph->getPortions()->getCount());
for ($portionIndex = 0; $portionIndex < $portionCount; $portionIndex++) {
$portion = $paragraph->getPortions()->get_Item($portionIndex);
$portionFormat = $portion->getPortionFormat();
$latinFont = $portionFormat->getLatinFont();
$eastAsianFont = $portionFormat->getEastAsianFont();
$complexScriptFont = $portionFormat->getComplexScriptFont();
if ((!java_is_null($latinFont) && $latinFont->getFontName() == $targetFont) ||
(!java_is_null($eastAsianFont) && $eastAsianFont->getFontName() == $targetFont) ||
(!java_is_null($complexScriptFont) && $complexScriptFont->getFontName() == $targetFont)) {
$portionFormat->setKerningMinimalSize(100);
}
}
}
$presentation->save("output.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
此设置可防止对匹配的文本部分应用字距调整,并有助于使 Aspose.Slides 的渲染效果与受此 PowerPoint 特定行为影响的字体的 PowerPoint 可视输出保持一致。
管理文本字体属性
字体属性可以通过 ParagraphFormat 的默认部分格式在段落级别设置,或者通过 PortionFormat 在单个部分上设置。
下面的代码为整个段落设置字体和文本样式:它对段落中的所有部分应用字体大小、粗体、斜体、点状下划线以及 Times New Roman 字体。
$presentation = new Presentation("sample.pptx");
try {
$autoShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
$defaultPortionFormat = $paragraph->getParagraphFormat()->getDefaultPortionFormat();
// 为段落设置字体属性。
$defaultPortionFormat->setFontHeight(12);
$defaultPortionFormat->setFontBold(NullableBool::True);
$defaultPortionFormat->setFontItalic(NullableBool::True);
$defaultPortionFormat->setFontUnderline(TextUnderlineType::Dotted);
$defaultPortionFormat->setLatinFont(new FontData("Times New Roman"));
$presentation->save("font_properties_for_paragraph.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}

下面的代码示例对 加粗字体的文本部分 应用类似属性:
$presentation = new Presentation("sample.pptx");
try {
$autoShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
$portionCount = java_values($paragraph->getPortions()->getCount());
for ($portionIndex = 0; $portionIndex < $portionCount; $portionIndex++) {
$portion = $paragraph->getPortions()->get_Item($portionIndex);
if (java_values($portion->getPortionFormat()->getEffective()->getFontBold()) === NullableBool::True) {
// 为文本部分设置字体属性。
$portionFormat = $portion->getPortionFormat();
$portionFormat->setFontHeight(13);
$portionFormat->setFontItalic(NullableBool::True);
$portionFormat->setFontUnderline(TextUnderlineType::Dotted);
$portionFormat->setLatinFont(new FontData("Times New Roman"));
}
}
$presentation->save("font_properties_for_text_portions.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}

设置文本旋转
使用 TextFrameFormat::setTextVerticalType 方法在形状内设置预定义的文本方向。
下面的代码示例将形状中的文本方向设置为 Vertical270,即将文本 逆时针旋转 90 度:
$presentation = new Presentation("sample.pptx");
try {
$autoShape = $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();
}

为文本框设置自定义旋转
使用 TextFrameFormat::setRotationAngle 方法为 TextFrame 设置自定义旋转角度。
下面的代码示例将在形状内将文本框顺时针旋转 3 度:
$presentation = new Presentation("sample.pptx");
try {
$autoShape = $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 提供 ParagraphFormat::setSpaceAfter、ParagraphFormat::setSpaceBefore 和 ParagraphFormat::setSpaceWithin 方法来控制段落间距。这些方法的使用如下:
- 使用正值将行间距指定为行高的百分比。
- 使用负值将行间距指定为磅值。
下面的代码示例展示如何在段落中指定行间距:
$presentation = new Presentation("sample.pptx");
try {
$autoShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
$paragraph->getParagraphFormat()->setSpaceWithin(200);
$presentation->save("line_spacing.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}

设置文本框的自动适应类型
TextFrameFormat::setAutofitType 方法决定文本超出容器边界时的行为。使用它可以控制文本是缩小、溢出还是自动调整形状大小。
$presentation = new Presentation("sample.pptx");
try {
$autoShape = $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();
}
设置文本框的锚点
TextFrameFormat::setAnchoringType 方法定义文本在形状内部的垂直定位方式,例如位于顶部、居中或底部。
$presentation = new Presentation("sample.pptx");
try {
$autoShape = $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();
}
设置文本制表
使用 ParagraphFormat::setDefaultTabSize 方法及其制表符集合来配置段落中的制表位。
$presentation = new Presentation("sample.pptx");
try {
$autoShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
$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 提供 BasePortionFormat::setLanguageId 方法,允许您为文本部分设置校对语言。校对语言决定 PowerPoint 中拼写和语法检查所使用的语言。
下面的代码示例展示如何为文本部分设置校对语言:
$presentation = new Presentation("presentation.pptx");
try {
$autoShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
$paragraph->getPortions()->clear();
$font = new FontData("SimSun");
$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 = new LoadOptions();
$loadOptions->setDefaultTextLanguage("en-US");
$presentation = new Presentation($loadOptions);
try {
$slide = $presentation->getSlides()->get_Item(0);
// 添加一个带文本的矩形形状。
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 20, 20, 150, 50);
$shape->getTextFrame()->setText("Sample text");
// 检查第一个文本段的语言。
$portion = $shape->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
echo $portion->getPortionFormat()->getLanguageId();
} finally {
$presentation->dispose();
}
设置默认文本样式
要在演示文稿级别应用默认文本格式,请使用 Presentation 的默认文本样式。
下面的代码示例展示如何在新演示文稿中为所有幻灯片的所有文本设置默认的粗体、14 磅大小的字体。
$presentation = new Presentation();
try {
// 获取顶级段落格式。
$paragraphFormat = $presentation->getDefaultTextStyle()->getLevel(0);
if (!java_is_null($paragraphFormat)) {
$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 = new Presentation("sample2.pptx");
try {
$autoShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
$textPortion = $autoShape->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
echo "Original text: ", $textPortion->getText(), "\n";
$textFormat = $textPortion->getPortionFormat()->getEffective();
if (java_values($textFormat->getTextCapType()) === TextCapType::All) {
$text = strtoupper($textPortion->getText());
echo "All-Caps effect: ", $text, "\n";
}
} finally {
$presentation->dispose();
}
Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!
常见问题
如何在幻灯片的表格中修改文本?
要在幻灯片的表格中修改文本,请使用 Table。遍历单元格,并通过 Cell 的文本框以及通过 Paragraph 的段落格式更新每个单元格的文本。
如何在 PowerPoint 幻灯片的文本上应用渐变颜色?
要对文本应用渐变颜色,请使用 PortionFormat 的填充格式。将 FillFormat 的填充类型设置为 FillType Gradient,并配置渐变停靠点、方向和透明度。