Manage Presentation Placeholders in C++
Overview
A placeholder is a shape that reserves a position for a particular kind of content in a presentation template. Common examples are title, body, picture, chart, and general-purpose content placeholders. Unlike an ordinary shape, a placeholder can inherit its position, size, formatting, and other settings from a layout slide or master slide.
Aspose.Slides exposes placeholder information through the IShape::get_Placeholder method. The method returns an IPlaceholder object or nullptr for a normal shape. Use IPlaceholder::get_Type to determine what the placeholder is intended to contain.
The shape interface still matters after you know the placeholder type:
- An empty text, picture, chart, or content placeholder is commonly represented by an IAutoShape.
- A populated picture placeholder can be represented by an IPictureFrame.
- A populated chart placeholder can be represented by an IChart.
- A content placeholder can contain several kinds of content. Check both IPlaceholder::get_Type and the runtime shape interface instead of assuming that every placeholder is an IAutoShape.
Warning
IPlaceholder::get_Type describes a placeholder’s role; it does not guarantee the shape’s runtime type. Always use a type check before accessing text, picture, chart, table, or media-specific members.Understand Placeholder Inheritance
Placeholders form a hierarchy:
- A master slide defines reusable styles and, in some cases, master-level placeholders.
- A layout slide defines the arrangement used by one or more normal slides and can inherit from the master.
- A normal slide contains the placeholders for that slide and can inherit from its layout.
Call IShape::GetBasePlaceholder to move one level up this hierarchy. A slide placeholder normally returns its layout placeholder; a layout placeholder can return its master placeholder. The method returns nullptr when the shape has no base placeholder.
The following example lists placeholders on the first slide and reports their base placeholders:
#include <DOM/IPlaceholder.h>
#include <DOM/IShape.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <system/type_info.h>
using namespace Aspose::Slides;
using namespace System;
auto presentation = MakeObject<Presentation>(u"template.pptx");
auto slide = presentation->get_Slide(0);
for (auto&& shape : slide->get_Shapes())
{
auto placeholder = shape->get_Placeholder();
if (placeholder == nullptr)
{
continue;
}
auto placeholderType = placeholder->get_Type();
auto typeName = shape->GetType().get_Name();
Console::WriteLine(u"Slide placeholder: {0}; shape interface: {1}", placeholderType, typeName);
auto layoutPlaceholder = shape->GetBasePlaceholder();
if (layoutPlaceholder != nullptr)
{
auto layoutPlaceholderInfo = layoutPlaceholder->get_Placeholder();
if (layoutPlaceholderInfo != nullptr)
{
auto layoutPlaceholderType = layoutPlaceholderInfo->get_Type();
Console::WriteLine(u" Layout placeholder: {0}", layoutPlaceholderType);
}
auto masterPlaceholder = layoutPlaceholder->GetBasePlaceholder();
if (masterPlaceholder != nullptr)
{
auto masterPlaceholderInfo = masterPlaceholder->get_Placeholder();
if (masterPlaceholderInfo != nullptr)
{
auto masterPlaceholderType = masterPlaceholderInfo->get_Type();
Console::WriteLine(u" Master placeholder: {0}", masterPlaceholderType);
}
}
}
}
Editing a placeholder on a normal slide creates or changes a local override for that slide. Editing the related layout or master can affect all slides that still inherit that setting. A local ordinary shape has no base placeholder and does not begin inheriting merely because it occupies the same coordinates.
Change Text in a Placeholder
Title, centered-title, subtitle, body, and text placeholders normally support text. Check for IAutoShape before using its get_TextFrame method.
This example updates the first title placeholder on the first slide and saves the result:
#include <DOM/IAutoShape.h>
#include <DOM/IPlaceholder.h>
#include <DOM/IShape.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ITextFrame.h>
#include <DOM/PlaceholderType.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/exceptions.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
auto presentation = MakeObject<Presentation>(u"template.pptx");
auto slide = presentation->get_Slide(0);
SharedPtr<IAutoShape> titleShape;
for (auto&& shape : slide->get_Shapes())
{
if (!ObjectExt::Is<IAutoShape>(shape))
{
continue;
}
auto autoShape = ExplicitCast<IAutoShape>(shape);
auto placeholder = autoShape->get_Placeholder();
if (placeholder == nullptr)
{
continue;
}
auto placeholderType = placeholder->get_Type();
if (placeholderType == PlaceholderType::Title || placeholderType == PlaceholderType::CenteredTitle)
{
titleShape = autoShape;
break;
}
}
if (titleShape == nullptr)
{
throw InvalidOperationException(u"The first slide does not contain a title placeholder.");
}
titleShape->get_TextFrame()->set_Text(u"Quarterly Business Review");
presentation->Save(u"title-placeholder-updated.pptx", SaveFormat::Pptx);
This pattern avoids casting picture, chart, table, or media placeholders to IAutoShape. It also identifies the placeholder by purpose instead of relying on a fragile shape index.
Set Prompt Text on a Layout
Prompt text is the design-time instruction displayed in an empty placeholder, such as Click to add title. Set custom prompt text on the layout placeholder rather than trying to reach it through a normal slide’s shape collection. Access the layout through ISlide::get_LayoutSlide and iterate over IBaseSlide::get_Shapes.
The following example changes the title and subtitle prompts on the layout used by the first slide:
#include <DOM/IAutoShape.h>
#include <DOM/ILayoutSlide.h>
#include <DOM/IPlaceholder.h>
#include <DOM/IShape.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ITextFrame.h>
#include <DOM/PlaceholderType.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
auto presentation = MakeObject<Presentation>(u"template.pptx");
auto layoutSlide = presentation->get_Slide(0)->get_LayoutSlide();
for (auto&& shape : layoutSlide->get_Shapes())
{
if (!ObjectExt::Is<IAutoShape>(shape))
{
continue;
}
auto autoShape = ExplicitCast<IAutoShape>(shape);
auto placeholder = autoShape->get_Placeholder();
if (placeholder == nullptr)
{
continue;
}
switch (placeholder->get_Type())
{
case PlaceholderType::Title:
case PlaceholderType::CenteredTitle:
autoShape->get_TextFrame()->set_Text(u"Enter a concise slide title");
break;
case PlaceholderType::Subtitle:
autoShape->get_TextFrame()->set_Text(u"Enter a subtitle or reporting period");
break;
default:
break;
}
}
presentation->Save(u"custom-placeholder-prompts.pptx", SaveFormat::Pptx);
Prompt text is not normal slide content. It is intended for empty placeholders in editing applications such as PowerPoint. Once a user or program supplies real content, the prompt is no longer displayed. Changing a prompt also does not replace existing text on slides that use the layout.
Update a Picture Placeholder
There are two cases to handle:
- If the picture placeholder is already populated and represented by an IPictureFrame, replace the image through IPictureFillFormat::get_Picture and ISlidesPicture::set_Image.
- If it is still an empty placeholder, add a picture frame at the placeholder’s coordinates with IShapeCollection::AddPictureFrame and remove the empty placeholder.
The next example supports both cases and saves the presentation:
#include <DOM/IImageCollection.h>
#include <DOM/IPictureFillFormat.h>
#include <DOM/IPictureFrame.h>
#include <DOM/IPlaceholder.h>
#include <DOM/IShape.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlidesPicture.h>
#include <DOM/PlaceholderType.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <system/exceptions.h>
#include <system/io/file.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::IO;
auto presentation = MakeObject<Presentation>(u"picture-template.pptx");
auto slide = presentation->get_Slide(0);
SharedPtr<IShape> picturePlaceholder;
for (auto&& shape : slide->get_Shapes())
{
auto placeholder = shape->get_Placeholder();
if (placeholder != nullptr && placeholder->get_Type() == PlaceholderType::Picture)
{
picturePlaceholder = shape;
break;
}
}
if (picturePlaceholder == nullptr)
{
throw InvalidOperationException(u"The first slide does not contain a picture placeholder.");
}
auto imageBytes = File::ReadAllBytes(u"replacement.png");
auto image = presentation->get_Images()->AddImage(imageBytes);
if (ObjectExt::Is<IPictureFrame>(picturePlaceholder))
{
auto pictureFrame = ExplicitCast<IPictureFrame>(picturePlaceholder);
pictureFrame->get_PictureFormat()->get_Picture()->set_Image(image);
}
else
{
auto x = picturePlaceholder->get_X();
auto y = picturePlaceholder->get_Y();
auto width = picturePlaceholder->get_Width();
auto height = picturePlaceholder->get_Height();
auto shapes = slide->get_Shapes();
shapes->AddPictureFrame(ShapeType::Rectangle, x, y, width, height, image);
shapes->Remove(picturePlaceholder);
}
presentation->Save(u"picture-placeholder-updated.pptx", SaveFormat::Pptx);
The replacement created for an empty placeholder is a local picture frame, not a new placeholder, because IShape::get_Placeholder is read-only. It keeps the reserved position but no longer inherits placeholder-specific behavior. If retaining the placeholder relationship is essential, prepare and populate the placeholder in PowerPoint first, then update the resulting IPictureFrame with Aspose.Slides.
For image transparency, cropping, and other picture-specific effects, see Manage Picture Frames. Those operations belong to the picture frame or picture fill, not to placeholder metadata.
Work with Chart and Content Placeholders
A populated chart placeholder can be represented by an IChart. This example finds such a chart by both placeholder type and runtime interface, changes its title, and saves the file:
#include <DOM/IChart.h>
#include <DOM/Chart/IChartTitle.h>
#include <DOM/IPlaceholder.h>
#include <DOM/IShape.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/PlaceholderType.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/exceptions.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;
using namespace System;
auto presentation = MakeObject<Presentation>(u"chart-template.pptx");
auto slide = presentation->get_Slide(0);
SharedPtr<IChart> placeholderChart;
for (auto&& shape : slide->get_Shapes())
{
if (!ObjectExt::Is<IChart>(shape))
{
continue;
}
auto chart = ExplicitCast<IChart>(shape);
auto placeholder = chart->get_Placeholder();
if (placeholder != nullptr && placeholder->get_Type() == PlaceholderType::Chart)
{
placeholderChart = chart;
break;
}
}
if (placeholderChart == nullptr)
{
throw InvalidOperationException(u"The first slide does not contain a populated chart placeholder.");
}
placeholderChart->set_HasTitle(true);
placeholderChart->get_ChartTitle()->AddTextFrameForOverriding(u"Quarterly Revenue");
presentation->Save(u"chart-placeholder-updated.pptx", SaveFormat::Pptx);
A general content placeholder usually has PlaceholderType::Object. In PowerPoint it acts as a launcher for several content types, including charts, tables, diagrams, pictures, and media. After it has been populated, inspect the actual shape interface to learn what it contains. Specialized layouts can also expose PlaceholderType::Chart, PlaceholderType::Table, PlaceholderType::Picture, PlaceholderType::Media, or PlaceholderType::Diagram.
Aspose.Slides does not convert an empty IAutoShape placeholder into an IChart merely by changing IPlaceholder::get_Type; the type is read-only. To fill an empty chart or content area programmatically, add the required object at the placeholder’s coordinates and then remove the empty placeholder. The following example does that for a chart:
#include <DOM/Chart/ChartType.h>
#include <DOM/IChart.h>
#include <DOM/Chart/IChartTitle.h>
#include <DOM/IPlaceholder.h>
#include <DOM/IShape.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/PlaceholderType.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>
#include <system/exceptions.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Charts;
using namespace Aspose::Slides::Export;
using namespace System;
auto presentation = MakeObject<Presentation>(u"content-template.pptx");
auto slide = presentation->get_Slide(0);
SharedPtr<IShape> targetPlaceholder;
for (auto&& shape : slide->get_Shapes())
{
auto placeholder = shape->get_Placeholder();
if (placeholder == nullptr)
{
continue;
}
auto placeholderType = placeholder->get_Type();
if (placeholderType == PlaceholderType::Chart || placeholderType == PlaceholderType::Object)
{
targetPlaceholder = shape;
break;
}
}
if (targetPlaceholder == nullptr)
{
throw InvalidOperationException(u"The first slide does not contain a chart or content placeholder.");
}
auto x = targetPlaceholder->get_X();
auto y = targetPlaceholder->get_Y();
auto width = targetPlaceholder->get_Width();
auto height = targetPlaceholder->get_Height();
auto shapes = slide->get_Shapes();
auto chart = shapes->AddChart(ChartType::ClusteredColumn, x, y, width, height);
chart->set_HasTitle(true);
chart->get_ChartTitle()->AddTextFrameForOverriding(u"Quarterly Revenue");
shapes->Remove(targetPlaceholder);
presentation->Save(u"content-placeholder-replaced-with-chart.pptx", SaveFormat::Pptx);
The added chart is an ordinary local chart. It occupies the placeholder’s area but does not inherit from the layout placeholder. Use the dedicated chart management articles when you need to replace its categories, series, or workbook data.
Complete Example: Update Text or Image Content
The following end-to-end example opens a template, searches the first slide for either a title or picture placeholder, checks the placeholder and shape types, updates the appropriate content, and saves the output. The example deliberately avoids assuming a shape index or casting every placeholder to the same interface.
#include <DOM/IAutoShape.h>
#include <DOM/IImageCollection.h>
#include <DOM/IPictureFillFormat.h>
#include <DOM/IPictureFrame.h>
#include <DOM/IPlaceholder.h>
#include <DOM/IShape.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlidesPicture.h>
#include <DOM/ITextFrame.h>
#include <DOM/PlaceholderType.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <system/exceptions.h>
#include <system/io/file.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::IO;
auto presentation = MakeObject<Presentation>(u"template.pptx");
auto slide = presentation->get_Slide(0);
auto updated = false;
for (auto&& shape : slide->get_Shapes())
{
auto placeholder = shape->get_Placeholder();
if (placeholder == nullptr)
{
continue;
}
auto placeholderType = placeholder->get_Type();
if ((placeholderType == PlaceholderType::Title || placeholderType == PlaceholderType::CenteredTitle) && ObjectExt::Is<IAutoShape>(shape))
{
auto titleShape = ExplicitCast<IAutoShape>(shape);
titleShape->get_TextFrame()->set_Text(u"Quarterly Business Review");
updated = true;
break;
}
if (placeholderType == PlaceholderType::Picture)
{
auto imageBytes = File::ReadAllBytes(u"replacement.png");
auto image = presentation->get_Images()->AddImage(imageBytes);
if (ObjectExt::Is<IPictureFrame>(shape))
{
auto pictureFrame = ExplicitCast<IPictureFrame>(shape);
pictureFrame->get_PictureFormat()->get_Picture()->set_Image(image);
}
else
{
auto x = shape->get_X();
auto y = shape->get_Y();
auto width = shape->get_Width();
auto height = shape->get_Height();
auto shapes = slide->get_Shapes();
shapes->AddPictureFrame(ShapeType::Rectangle, x, y, width, height, image);
shapes->Remove(shape);
}
updated = true;
break;
}
}
if (!updated)
{
throw InvalidOperationException(u"No supported title or picture placeholder was found on the first slide.");
}
presentation->Save(u"placeholder-content-updated.pptx", SaveFormat::Pptx);
FAQ
What is a base placeholder?
A base placeholder is the corresponding shape on the layout or master from which another placeholder inherits. Use IShape::GetBasePlaceholder to retrieve it. An ordinary local shape returns nullptr because it is not part of the placeholder hierarchy.
Can I change all slide titles by editing a layout placeholder?
You can change inherited formatting or prompt text through a layout, but existing title content is stored on the normal slides. To replace actual title text across a presentation, iterate over the slides and update each title placeholder.
How do I manage date, slide-number, header, and footer placeholders?
Use the header and footer managers at the appropriate slide, layout, master, notes, or handout scope. See Manage Presentation Header and Footer for complete examples.