Slide Master
What is a Slide Master in PowerPoint
A Slide Master is a slide template that defines the layout, styles, theme, fonts, background, and other properties for slides in a presentation. If you want to create a presentation (or series of presentations) with the same style and template for your company, you can use a slide master.
A Slide Master is useful because it allows you to set and change the look of all presentation slides at once. Aspose.Slides supports the Slide Master mechanism from PowerPoint.
VBA also allows you to manipulate a Slide Master and execute the same operations supported in PowerPoint: change backgrounds, add shapes, customize the layout, etc. Aspose.Slides provides flexible mechanisms to allow you to use Slide Masters and perform basic tasks with them.
These are basic Slide Master operations:
- Create or Slide Master.
- Apply Slides Master to presentation slides.
- Change Slide Master background.
- Add an image, placeholder, Smart Art, etc. to Slide Master.
These are more advanced operations involving Slide Master:
- Compare Slide Masters.
- Merge Slide Masters.
- Apply several Slide Masters.
- Copy slide with Slide Master to another presentation.
- Find out duplicate Slide Masters in presentations.
- Set Slide Master as the presentation default view.
How is Slide Master applied
Before you work with a slide master, you may want to understand how they are used in presentations and applied to slides.
- Every presentation has at least one Slide Master by default.
- A presentation can contain several Slide Masters. You can add several Slide Masters and use them to style different parts of a presentation in different ways.
In Aspose.Slides, a Slide Master is represented by IMasterSlide type.
Aspose.Slides' Presentation object contains the getMasters list of IMasterSlideCollection type, which contains a list of all master slides that are defined in a presentation.
Besides CRUD operations, the IMasterSlideCollection interface contains these useful methods: addClone(ILayoutSlide sourceLayout) and insertClone(int index, IMasterSlide sourceMaster) methods. Those methods are inherited from the basic slide cloning function. But when dealing with Slide Masters, those methods allow you to implement complicated setups.
When a new slide is added to a presentation, a Slide Master is applied to it automatically. The Slide Master of the previous slide is selected by default.
Note: Presentation slides are stored in getSlides() list, and every new slide is added to the end of the collection by default. If a presentation contains a single Slide Master, that slide master is selected for all new slides. This is the reason you do not have to define the Slide Master for every new slide you create.
The principle is the same for PowerPoint and Aspose.Slides. For example, in PowerPoint, when you add a new presentation, you can just press on the bottom line under the last slide and then a new slide (with the last presentation’s Slide Master) will be created:
In Aspose.Slides, you can perform the equivalent task with the addClone(ISlide sourceSlide) method under the Presentation class.
Slide Master in Slides hierarchy
Using Slide Layouts with Slide Master allows for maximum flexibility. A Slide Layout allows you to set all the same styles as Slide Master (background, fonts, shapes, etc.). However, when several Slide Layouts are combined on a Slide Master, a new style is created. When you apply a Slide Layout to a single slide, you can change its style from the one applied by the Slide Master.
Slide Master outranks all setups items: Slide Master -> Slide Layout -> Slide:
Each IMasterSlide object has a getLayoutSlides property with a list of Slide Layouts. A Slide type has a getLayoutSlide property with a link on a Slide Layout applied to the slide. The interaction between a slide and Slide Master occurs through a Slide Layout.
Note
- In Aspose.Slides, all the slide setups (Slide Master, Slide Layout, and the slide itself) are actually slide objects implementing the IBaseSlide interface.
- Therefore, Slide Master and Slide Layout may implement the same properties and you need to know how their values will be applied to a Slide object. The Slide Master is applied first to a slide and then the Slide Layout is applied. For example, if the Slide Master and Slide Layout both have a background value, the Slide will end up with the background from the Slide Layout.
What A Slide Master Comprises
To understand how a Slide Master can be changed, you need to know its constituents. These are MasterSlide core properties.
- getBackground get/set slide background.
- getBodyStyle - get/set text styles of the slide’s body.
- getShapes get/set all the shapes of the Slide Master (placeholders, picture frames, etc).
- getControls get/set ActiveX controls.
- getThemeManager - get theme manager.
- getHeaderFooterManager - get header and footer manager.
Slide Master methods:
- getDependingSlides - get all Slides depending on the Slide Master.
- applyExternalThemeToDependingSlides - allows you to create a new Slide Master based on the current Slide Master and a new theme. The new Slide Master will then be applied to all dependent slides.
Get Slide Master
In PowerPoint, Slide Master can be accessed from the View -> Slide Master menu:
Using Aspose.Slides, you can access a Slide Master this way:
$pres = new Presentation();
try {
# Gives access to the Presentation's master slide
$masterSlide = $pres->getMasters()->get_Item(0);
} finally {
$pres->dispose();
}
The IMasterSlide interface represents a Slide Master. The Masters property (related to IMasterSlideCollection type) contains a list of all Slide Masters that are defined in the presentation.
Add Image to Slide Master
When you add an image to a Slide Master, that image will appear on all slides dependent on that slide master.
For example, you can place your company’s logo and a few images on the Slide Master and then switch back to slide editing mode. You should see the image on every slide.
You can add images to a slide master with Aspose.Slides:
$pres = new Presentation();
try {
$picture;
$image = Images->fromFile("image.png");
try {
$picture = $pres->getImages()->addImage($image);
} finally {
if (!java_is_null($image)) {
$image->dispose();
}
}
$pres->getMasters()->get_Item(0)->getShapes()->addPictureFrame(ShapeType::Rectangle, 10, 10, 100, 100, $picture);
$pres->save("pres.pptx", SaveFormat::Pptx);
} catch (JavaException $e) {
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
See also
For more information on adding images to a slide, see the Picture Frame article.Add Placeholder to Slide Master
These text fields are standard placeholders on a Slide Master:
-
Click to edit Master title style
-
Edit Master text styles
-
Second level
-
Third level
They also appear on the slides based on Slide Master. You can edit those placeholders on a Slide Master and the changes are applied automatically to the slides.
In PowerPoint, you can add a placeholder through the Slide Master -> Insert Placeholder path:
Let’s examine a more complicated example for placeholders with Aspose.Slides. Consider a slide with placeholders templated from the Slide Master:
We want to change the Title and Subtitle formatting on the Slide Master this way:
First, we retrieve the title placeholder content from the Slide Master object and then use thePlaceHolder.FillFormat
field:
The title style and formatting will change for all slides based on the slide master:
Change Background on Slide Master
When you change a master slide’s background color, all the normal slides in the presentation will get the new color. This PHP code demonstrates the operation:
$pres = new Presentation();
try {
$master = $pres->getMasters()->get_Item(0);
$master->getBackground()->setType(BackgroundType::OwnBackground);
$master->getBackground()->getFillFormat()->setFillType(FillType::Solid);
$master->getBackground()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->GREEN);
$pres->save("pres.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Clone Slide Master to Another Presentation
To clone a Slide Master to another presentation, call the addClone method from the destination presentation alongside a Slide Master passed into it. This PHP code shows you how to clone a Slide Master to another presentation:
$presSource = new Presentation();
$presTarget = new Presentation();
try {
$master = $presTarget->getMasters()->addClone($presSource->getMasters()->get_Item(0));
} finally {
if (!java_is_null($presSource)) {
$presSource->dispose();
}
}
Add Multiple Slide Masters to Presentation
Aspose.Slides allows you to add several Slide Masters and Slide Layouts to any given presentation. This allows you to set up styles, layouts, and formatting options for presentation slides in many ways.
In PowerPoint, you can add new Slide Masters and Layouts (from the “Slide Master menu) this way:
Using Aspose.Slides, you can add a new Slide Master by calling the addClone method:
# Adds a new master slide
$secondMasterSlide = $pres->getMasters()->addClone($masterSlide);
Compare Slide Masters
A Master Slide implements the IBaseSlide interface containing the equals method, which can then be used to compare slides. It returns true
for Master Slides identical in structure and static content.
Two Master Slides are equal if their shapes, styles, texts, animation and other settings, etc are equal. The comparison does not take unique identifier values (e.g. SlideId) and dynamic content (e.g. current date value in Date Placeholder) into account.
Set Slide Master as Presentation Default View
Aspose.Slides allows you to set a Slide Master as the default view for a presentation. The default view is what you see first when you open a presentation.
This code shows you how to set a Slide Master as a presentation’s default view :
# Instantiates a Presentation class that represents the presentation file
$presentation = new Presentation();
try {
# Sets the Default View as SlideMasterView
$presentation->getViewProperties()->setLastView(ViewType::SlideMasterView);
# Saves the presentation
$presentation->save("PresView.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Remove Unused Master Slide
Aspose.Slides provides the removeUnusedMasterSlides method (from the Compress class) to allow you to delete unwanted and unused master slides. This PHP code shows you how to remove a master slide from a PowerPoint presentation:
$pres = new Presentation("pres.pptx");
try {
Compress->removeUnusedMasterSlides($pres);
$pres->save("pres-out.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}