Format Presentation Text in PHP
Overview
This article shows how to format text in PowerPoint and OpenDocument presentations using Aspose.Slides for PHP via Java. It covers highlighting, background colors, transparency, character spacing, font properties, rotation, paragraph spacing, autofit behavior, text anchoring, tab stops, and language settings.
In the examples below, we’ll use a file named “sample.pptx”, which contains a single text box on the first slide with the following text:

Highlight Text
Use the TextFrame::highlightText method when you need to highlight text that matches a specific sample within a text frame. The method applies a highlight color to matching text fragments and can be used with TextHighlightingOptions to control how the search is performed, for example, to match only whole words.
The code example below highlights all occurrences of the characters “try” and then highlights only the full word “to”.
$presentation = new Presentation("sample.pptx");
try {
// Get the first shape from the first slide.
$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);
// Highlight the word "try" in the shape.
$shape->getTextFrame()->highlightText("try", $lightBlue);
$searchOptions = new TextHighlightingOptions();
$searchOptions->setWholeWordsOnly(true);
// Highlight the word "to" in the shape.
$shape->getTextFrame()->highlightText("to", $violet, $searchOptions);
$presentation->save("highlighted_text.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
The result:

Highlight Text Using Regular Expressions
The TextFrame::highlightRegex method highlights text matches found by a regular expression.
The code example below highlights all words that contain seven or more characters:
$presentation = new Presentation("sample.pptx");
try {
$shape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
// Highlight all words with seven or more characters.
$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();
}
The result:

Set Text Background Color
Use ParagraphFormat’s default portion format to set the default highlight color for a paragraph, or use PortionFormat for individual text portions.
The following code example shows how to set the background color for the entire paragraph:
$presentation = new Presentation("sample.pptx");
try {
$autoShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
// Set the highlight color for the entire paragraph.
$paragraph->getParagraphFormat()->getDefaultPortionFormat()->getHighlightColor()->setColor(java("java.awt.Color")->LIGHT_GRAY);
$presentation->save("gray_paragraph.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
The result:

The code example below demonstrates how to set the background color for text portions with a bold font:
$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) {
// Set the highlight color for the text portion.
$portion->getPortionFormat()->getHighlightColor()->setColor(java("java.awt.Color")->LIGHT_GRAY);
}
}
$presentation->save("gray_text_portions.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
The result:

Align Text Paragraphs
Use ParagraphFormat::setAlignment method to set paragraph alignment within a text frame. The value can be centered, left-aligned, right-aligned, justified, and so on.
The following code example shows how to align the paragraph to the center:
$presentation = new Presentation("sample.pptx");
try {
$autoShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
// Set the alignment of the paragraph to center.
$paragraph->getParagraphFormat()->setAlignment(TextAlignment::Center);
$presentation->save("aligned_paragraph.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
The result:

Set Transparency for Text
Text transparency is controlled through the alpha component of the color assigned to PortionFormat’s fill format. In the examples below, alpha = 50 is an ARGB alpha-channel value on the 0-255 scale, not a transparency percentage.
The code example below shows how to apply transparency to the entire paragraph:
$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();
// Set the fill color of the text to a transparent color.
$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();
}
The result:

The following code example shows how to apply transparency to text portions with a bold font:
$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) {
// Set the transparency of the text portion.
$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();
}
The result:

Set Character Spacing for Text
Use BasePortionFormat::setSpacing method to expand or condense spacing between characters in a text box.
The following PHP code shows how to expand the character spacing in the entire paragraph:
$presentation = new Presentation("sample.pptx");
try {
$autoShape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
// Note: Use negative values to compress the character spacing.
$paragraph->getParagraphFormat()->getDefaultPortionFormat()->setSpacing(3); // Expand character spacing.
$presentation->save("character_spacing_in_paragraph.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
The result:

The code example below shows how to expand the character spacing in text portions with a bold font:
$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) {
// Note: Use negative values to compress the character spacing.
$portion->getPortionFormat()->setSpacing(3); // Expand character spacing.
}
}
$presentation->save("character_spacing_in_text_portions.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
The result:

Disable Kerning for Specific Fonts
In some cases, text rendered by Aspose.Slides may look slightly tighter than the same text displayed in PowerPoint. This can happen because PowerPoint may ignore kerning data for certain fonts, even when the font contains valid kerning information and kerning is enabled in PowerPoint settings.
To make the rendered output closer to PowerPoint in such cases, you can disable kerning for text portions that use the affected font. Set BasePortionFormat::setKerningMinimalSize method to a value significantly larger than the actual font size:
$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();
}
This setting prevents kerning from being applied to matching text portions and can help align Aspose.Slides rendering with PowerPoint’s visual output for fonts affected by this PowerPoint-specific behavior.
Manage Text Font Properties
Font properties can be set at the paragraph level through ParagraphFormat’s default portion format or on individual portions through PortionFormat.
The following code sets the font and text style for the entire paragraph: it applies font size, bold, italic, dotted underline, and the Times New Roman font to all portions in the paragraph.
$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();
// Set the font properties for the paragraph.
$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();
}
The result:

The code example below applies similar properties to text portions with a bold font:
$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) {
// Set the font properties for the text portion.
$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();
}
The result:

Set Text Rotation
Use TextFrameFormat::setTextVerticalType method to set a predefined text orientation within a shape.
The following code example sets the text orientation in the shape to Vertical270, which rotates the text 90 degrees counterclockwise:
$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();
}
The result:

Set Custom Rotation for Text Frames
Use TextFrameFormat::setRotationAngle method to set a custom rotation angle for a TextFrame.
The code example below rotates the text frame by 3 degrees clockwise within the shape:
$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();
}
The result:

Set Line Spacing of Paragraphs
Aspose.Slides provides ParagraphFormat::setSpaceAfter, ParagraphFormat::setSpaceBefore, and ParagraphFormat::setSpaceWithin methods to control paragraph spacing. These methods are used as follows:
- Use a positive value to specify line spacing as a percentage of the line height.
- Use a negative value to specify line spacing in points.
The following code example shows how to specify the line spacing within the paragraph:
$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();
}
The result:

Set Autofit Type for Text Frames
TextFrameFormat::setAutofitType method determines how text behaves when it exceeds the boundaries of its container. Use it to control whether the text shrinks, overflows, or resizes the shape automatically.
$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();
}
Set Anchor of Text Frames
TextFrameFormat::setAnchoringType method defines how text is positioned vertically inside a shape, for example at the top, middle, or bottom.
$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();
}
Set Text Tabulation
Use ParagraphFormat::setDefaultTabSize method and its tabs collection to configure tab stops in a paragraph.
$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();
}
The result:

Set Proofing Language
Aspose.Slides provides BasePortionFormat::setLanguageId method, which allows you to set the proofing language for a text portion. The proofing language determines the language used for spelling and grammar checks in PowerPoint.
The following code example shows how to set the proofing language for a text portion:
$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);
// Set the ID of a proofing language.
$textPortion->getPortionFormat()->setLanguageId("zh-CN");
$textPortion->setText("1.");
$paragraph->getPortions()->add($textPortion);
$presentation->save("proofing_language.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Set Default Language
Use LoadOptions::setDefaultTextLanguage method to define the default language for text created while loading or creating a presentation.
$loadOptions = new LoadOptions();
$loadOptions->setDefaultTextLanguage("en-US");
$presentation = new Presentation($loadOptions);
try {
$slide = $presentation->getSlides()->get_Item(0);
// Add a new rectangle shape with text.
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 20, 20, 150, 50);
$shape->getTextFrame()->setText("Sample text");
// Check the first portion language.
$portion = $shape->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
echo $portion->getPortionFormat()->getLanguageId();
} finally {
$presentation->dispose();
}
Set Default Text Style
To apply default text formatting at the presentation level, use Presentation’s default text style.
The following code example shows how to set a default bold font with a 14 pt size for all text across slides in a new presentation.
$presentation = new Presentation();
try {
// Get the top level paragraph format.
$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();
}
Extract Text with the All-Caps Effect
In PowerPoint, applying the All Caps font effect makes text appear in uppercase on the slide even when it was originally typed in lowercase. When you retrieve such a text portion with Aspose.Slides, the library returns the text exactly as it was entered. To match the displayed text, check TextCapType and convert the returned string to uppercase when the value is All.
Let’s say we have the following text box on the first slide of the sample2.pptx file.

The code example below shows how to extract the text with the All Caps effect applied:
$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();
}
Output:
Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!
FAQ
How to modify text in a table on a slide?
To modify text in a table on a slide, use Table. Iterate through the cells and update each cell through Cell’s text frame and paragraph formatting through Paragraph’s paragraph format.
How to apply gradient color to text in a PowerPoint slide?
To apply a gradient color to text, use PortionFormat’s fill format. Set FillFormat’s fill type to FillType Gradient and configure the gradient stops, direction, and transparency.