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 MasterSlide type.

Aspose.Slides' Presentation object contains the getMasters list of MasterSlideCollection type, which contains a list of all master slides that are defined in a presentation.

Besides CRUD operations, the MasterSlideCollection class 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:

todo:image_alt_text

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:

todo:image_alt_text

Each MasterSlide 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.

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.

Slide Master methods:

Get Slide Master

In PowerPoint, Slide Master can be accessed from the View -> Slide Master menu:

todo:image_alt_text

Using Aspose.Slides, you can access a Slide Master this way:

var pres = new aspose.slides.Presentation();
try {
    // Gives access to the Presentation's master slide
    var masterSlide = pres.getMasters().get_Item(0);
} finally {
    pres.dispose();
}

The MasterSlide class represents a Slide Master. The Masters property (related to MasterSlideCollection 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.

todo:image_alt_text

You can add images to a slide master with Aspose.Slides:

var pres = new aspose.slides.Presentation();
try {
    var picture;
    var image = aspose.slides.Images.fromFile("image.png");
    try {
        picture = pres.getImages().addImage(image);
    } finally {
        if (image != null) {
            image.dispose();
        }
    }
    pres.getMasters().get_Item(0).getShapes().addPictureFrame(aspose.slides.ShapeType.Rectangle, 10, 10, 100, 100, picture);
    pres.save("pres.pptx", aspose.slides.SaveFormat.Pptx);
} catch (e) {
    console.log(e);
} finally {
    if (pres != null) {
        pres.dispose();
    }
}

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:

todo:image_alt_text

Let’s examine a more complicated example for placeholders with Aspose.Slides. Consider a slide with placeholders templated from the Slide Master:

todo:image_alt_text

We want to change the Title and Subtitle formatting on the Slide Master this way:

todo:image_alt_text

First, we retrieve the title placeholder content from the Slide Master object and then use thePlaceHolder.FillFormat field:

var pres = new aspose.slides.Presentation();
try {
    var master = pres.getMasters().get_Item(0);
    var placeHolder = findPlaceholder(master, aspose.slides.PlaceholderType.Title);
    placeHolder.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Gradient));
    placeHolder.getFillFormat().getGradientFormat().setGradientShape(java.newByte(aspose.slides.GradientShape.Linear));
    var awtColor = java.import('java.awt.Color');
    placeHolder.getFillFormat().getGradientFormat().getGradientStops().add(0, java.newInstanceSync('java.awt.Color', 255, 0, 0));
    placeHolder.getFillFormat().getGradientFormat().getGradientStops().add(255, java.newInstanceSync('java.awt.Color', 128, 0, 128));

    pres.save("pres.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

function findPlaceholder(master, type)
{    
    for (var i = 0 ; i < master.getShapes().size(); i++)
    {
        var autoShape = master.getShapes().get_Item(i);
        if (autoShape != null)
        {
            if (autoShape.getPlaceholder().getType() == type)
            {
                return autoShape;
            }
        }
    }

    return null;
}

The title style and formatting will change for all slides based on the slide master:

todo:image_alt_text

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 JavaScript code demonstrates the operation:

var pres = new aspose.slides.Presentation();
try {
    var master = pres.getMasters().get_Item(0);
    master.getBackground().setType(aspose.slides.BackgroundType.OwnBackground);
    master.getBackground().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    master.getBackground().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "GREEN"));
    pres.save("pres.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    if (pres != null) {
        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 JavaScript code shows you how to clone a Slide Master to another presentation:

var presSource = new aspose.slides.Presentation();
var presTarget = new aspose.slides.Presentation();
try {
    var master = presTarget.getMasters().addClone(presSource.getMasters().get_Item(0));
} finally {
    if (presSource != null) {
        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:

todo:image_alt_text

Using Aspose.Slides, you can add a new Slide Master by calling the addClone method:

// Adds a new master slide
var secondMasterSlide = pres.getMasters().addClone(masterSlide);

Compare Slide Masters

A Master Slide implements the BaseSlide class 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 in JavaScript:

// Instantiates a Presentation class that represents the presentation file
var presentation = new aspose.slides.Presentation();
try {
    // Sets the Default View as SlideMasterView
    presentation.getViewProperties().setLastView(aspose.slides.ViewType.SlideMasterView);
    // Saves the presentation
    presentation.save("PresView.pptx", aspose.slides.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 JavaScript code shows you how to remove a master slide from a PowerPoint presentation:

var pres = new aspose.slides.Presentation("pres.pptx");
try {
    aspose.slides.Compress.removeUnusedMasterSlides(pres);
    pres.save("pres-out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    if (pres != null) {
        pres.dispose();
    }
}