Manage Presentation Placeholders in PHP
Change Text in a Placeholder
Using Aspose.Slides for PHP via Java, you can find and modify placeholders on slides in presentations. Aspose.Slides allows you to make changes to the text in a placeholder.
Prerequisite: You need a presentation that contains a placeholder. You can create such a presentation in the standard Microsoft PowerPoint app.
This is how you use Aspose.Slides to replace the text in the placeholder in that presentation:
- Instantiate the
Presentationclass. and pass the presentation as an argument. - Get a slide reference through its index.
- Iterate through the shapes to find the placeholder.
- Typecast the placeholder shape to an
AutoShapeand change the text using theTextFrameassociated with theAutoShape. - Save the modified presentation.
This PHP code shows how to change the text in a placeholder:
# Instantiates a Presentation class
$pres = new Presentation("ReplacingText.pptx");
try {
# Accesses the first slide
$sld = $pres->getSlides()->get_Item(0);
# Iterates through shapes to find the placeholder
foreach($sld->getShapes() as $shp) {
if (!java_is_null($shp->getPlaceholder())) {
# Changes the text in each placeholder
$shp->getTextFrame()->setText("This is Placeholder");
}
}
# Saves the presentation to disk
$pres->save("output.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Set Prompt Text in a Placeholder
Standard and pre-built layouts contain placeholder prompt texts such as Click to add a title or Click to add a subtitle. Using Aspose.Slides, you can insert your preferred prompt texts into placeholder layouts.
This PHP code shows you how to set the prompt text in a placeholder:
$pres = new Presentation("Presentation.pptx");
try {
$slide = $pres->getSlides()->get_Item(0);
# Iterates through the slide
foreach($slide->getSlide()->getShapes() as $shape) {
if (java_instanceof($shape->getPlaceholder()) != null && $shape, new JavaClass("com.aspose.slides.AutoShape")) {
$text = "";
# PowerPoint displays "Click to add title"
if ($shape->getPlaceholder()->getType() == PlaceholderType::CenteredTitle) {
$text = "Add Title";
} else // Adds subtitle
if ($shape->getPlaceholder()->getType() == PlaceholderType::Subtitle) {
$text = "Add Subtitle";
}
$shape->getTextFrame()->setText($text);
echo("Placeholder with text: " . $text);
}
}
$pres->save("Placeholders_PromptText.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Set Placeholder Image Transparency
Aspose.Slides allows you to set the transparency of the background image in a text placeholder. By adjusting the transparency of the picture in such a frame, you can make the text or the image stand out (depending on the text’s and picture’s colors).
This PHP code shows you how to set the transparency for a picture background (inside a shape):
$presentation = new Presentation("example.pptx");
$shape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
$operationCollection = $shape->getFillFormat()->getPictureFillFormat()->getPicture()->getImageTransform();
for($i = 0; $i < java_values($operationCollection->size()) ; $i++) {
if (java_instanceof($operationCollection->get_Item($i)), new JavaClass("com.aspose.slides.AlphaModulateFixed")) {
$alphaModulate = $operationCollection->get_Item($i);
$currentValue = 100 - $alphaModulate->getAmount();
echo("Current transparency value: " . $currentValue);
$alphaValue = 40;
$alphaModulate->setAmount(100 - $alphaValue);
}
}
$presentation->save("example_out.pptx", SaveFormat::Pptx);
FAQ
What is a base placeholder, and how is it different from a local shape on a slide?
A base placeholder is the original shape on a layout or master that the slide’s shape inherits from—type, position, and some formatting come from it. A local shape is independent; if there’s no base placeholder, inheritance doesn’t apply.
How can I update all titles or captions across a presentation without iterating over every slide?
Edit the corresponding placeholder on the layout or the master. Slides based on those layouts/that master will automatically inherit the change.
How do I control the standard header/footer placeholders—date & time, slide number, and footer text?
Use the HeaderFooter managers at the appropriate scope (normal slides, layouts, master, notes/handouts) to turn those placeholders on or off and to set their content.