Text Box
In Aspose.Slides, a text box is represented by an AutoShape. Nearly any shape can contain text, but a typical text box has no fill or border and displays only text.
This guide explains how to add, access, and remove text boxes programmatically.
Add a Text Box
A text box is simply an AutoShape with no fill or border and some formatted text. Here’s how to create one:
function addTextBox() {
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
// Create a rectangle shape (defaults to filled with border and no text).
$textBox = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 50, 75, 150, 100);
// Remove fill and border to make it look like a typical text box.
$textBox->getFillFormat()->setFillType(FillType::NoFill);
$textBox->getLineFormat()->getFillFormat()->setFillType(FillType::NoFill);
// Set text formatting.
$paragraph = $textBox->getTextFrame()->getParagraphs()->get_Item(0);
$portionFormat = $paragraph->getParagraphFormat()->getDefaultPortionFormat();
$portionFormat->getFillFormat()->setFillType(FillType::Solid);
$portionFormat->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
// Assign the actual text content.
$textBox->getTextFrame()->setText("Some text...");
$presentation->save("text_box.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
}
💡 Note: Any
AutoShapethat contains a non-emptyTextFramecan function as a text box.
Access Text Boxes by Content
To find all text boxes containing a specific keyword (e.g. “Slide”), iterate through the shapes and check their text:
function accessTextBox() {
$presentation = new Presentation("text_box.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
// Access the first text box on the slide.
$firstTextBox = null;
$shapeCount = java_values($slide->getShapes()->size());
for ($index = 0; $index < $shapeCount; $index++) {
$shape = $slide->getShapes()->get_Item($index);
if (java_instanceof($shape, new JavaClass("com.aspose.slides.AutoShape"))) {
$firstTextBox = $shape;
if (strpos($firstTextBox->getTextFrame()->getText(), "Slide") !== false) {
// Do something with the matching text box.
}
break;
}
}
} finally {
$presentation->dispose();
}
}
Remove Text Boxes by Content
This example finds and deletes all text boxes on the first slide that contain a specific keyword:
function removeTextBoxes() {
$presentation = new Presentation("text_box.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$shapesToRemove = [];
$shapeCount = java_values($slide->getShapes()->size());
for ($index = 0; $index < $shapeCount; $index++) {
$shape = $slide->getShapes()->get_Item($index);
if (java_instanceof($shape, new JavaClass("com.aspose.slides.AutoShape"))) {
$autoShape = $shape;
if (strpos($autoShape->getTextFrame()->getText(), "Slide") !== false) {
$shapesToRemove[] = $shape;
}
}
}
foreach ($shapesToRemove as $shape) {
$slide->getShapes()->remove($shape);
}
$presentation->save("text_boxes_removed.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
}
💡 Tip: Always create a copy of the shape collection before modifying it during iteration to avoid collection modification errors.