Apply or Change a Slide Layout in JavaScript

Overview

A slide layout defines the arrangement of placeholder boxes and formatting for the content on a slide. It controls which placeholders are available and where they appear. Slide layouts help you design presentations quickly and consistently—whether you’re creating something simple or more complex. Some of the most common slide layouts in PowerPoint include:

Title Slide layout – Includes two text placeholders: one for the title and one for the subtitle.

Title and Content layout – Features a smaller title placeholder at the top and a larger one below for main content (such as text, bullet points, charts, images, and more).

Blank layout – Contains no placeholders, giving you full control to design the slide from scratch.

Slide layouts are part of a slide master, which is the top-level slide that defines layout styles for the presentation. You can access and modify layout slides through the slide master—either by their type, name, or unique ID. Alternatively, you can edit a specific layout slide directly within the presentation.

To work with slide layouts in Aspose.Slides for Node.js, you can use:

Add Slide Layouts to Presentations

To customize the appearance and structure of your slides, you may need to add new layout slides to a presentation. Aspose.Slides for Node.js allows you to check whether a specific layout already exists, add a new one if needed, and use it to insert slides based on that layout.

  1. Create an instance of the Presentation class.
  2. Access the MasterLayoutSlideCollection.
  3. Check whether the desired layout slide already exists in the collection. If not, add the layout slide you need.
  4. Add an empty slide based on the new layout slide.
  5. Save the presentation.

The following JavaScript code demonstrates how to add a slide layout to a PowerPoint presentation:

// Instantiate the Presentation class that represents a PowerPoint file.
let presentation = new aspose.slides.Presentation("Sample.pptx");
try {
    // Go through the layout slide types to select a layout slide.
    let layoutSlides = presentation.getMasters().get_Item(0).getLayoutSlides();
    let layoutSlide = null;
    if (layoutSlides.getByType(java.newByte(aspose.slides.SlideLayoutType.TitleAndObject)) != null) {
        layoutSlide = layoutSlides.getByType(java.newByte(aspose.slides.SlideLayoutType.TitleAndObject));
    } else {
        layoutSlide = layoutSlides.getByType(java.newByte(aspose.slides.SlideLayoutType.Title));
    }

    if (layoutSlide == null) {
        // A situation where the presentation doesn't contain all layout types.
        // The presentation file contains only Blank and Custom layout types.
        // However, layout slides with custom types may have recognizable names,
        // such as "Title", "Title and Content", etc., which can be used for layout slide selection.
        // You can also rely on a set of placeholder shape types.
        // For example, a Title slide should have only the Title placeholder type, and so on.
        for (let i = 0; i < layoutSlides.size(); i++) {
            let titleAndObjectLayoutSlide = layoutSlides.get_Item(i);
            if (titleAndObjectLayoutSlide.getName() === "Title and Object") {
                layoutSlide = titleAndObjectLayoutSlide;
                break;
            }
        }

        if (layoutSlide == null) {
            for (let i = 0; i < layoutSlides.size(); i++) {
                let titleLayoutSlide = layoutSlides.get_Item(i);
                if (titleLayoutSlide.getName() === "Title") {
                    layoutSlide = titleLayoutSlide;
                    break;
                }
            }

            if (layoutSlide == null) {
                layoutSlide = layoutSlides.getByType(java.newByte(aspose.slides.SlideLayoutType.Blank));
                if (layoutSlide == null) {
                    layoutSlide = layoutSlides.add(java.newByte(aspose.slides.SlideLayoutType.TitleAndObject), "Title and Object");
                }
            }
        }
    }

    // Add an empty slide using the added layout slide.
    presentation.getSlides().insertEmptySlide(0, layoutSlide);

    // Save the presentation to disk.
    presentation.save("output.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

Remove Unused Layout Slides

Aspose.Slides provides the removeUnusedLayoutSlides method from the Compress class to allow you to delete unwanted and unused layout slides.

The following JavaScript code shows how to remove a layout slide from a PowerPoint presentation:

let presentation = new aspose.slides.Presentation("Presentation.pptx");
try {
    aspose.slides.Compress.removeUnusedLayoutSlides(presentation);
    presentation.save("Output.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

Add Placeholders To Slide Layouts

Aspose.Slides provides the LayoutSlide.getPlaceholderManager method, which allows you to add new placeholders to a layout slide.

This manager contains methods for the following placeholder types:

PowerPoint Placeholder LayoutPlaceholderManager Method
Content addContentPlaceholder(float x, float y, float width, float height)
Content (Vertical) addVerticalContentPlaceholder(float x, float y, float width, float height)
Text addTextPlaceholder(float x, float y, float width, float height)
Text (Vertical) addVerticalTextPlaceholder(float x, float y, float width, float height)
Picture addPicturePlaceholder(float x, float y, float width, float height)
Chart addChartPlaceholder(float x, float y, float width, float height)
Table addTablePlaceholder(float x, float y, float width, float height)
SmartArt addSmartArtPlaceholder(float x, float y, float width, float height)
Media addMediaPlaceholder(float x, float y, float width, float height)
Online Image addOnlineImagePlaceholder(float x, float y, float width, float height)

The following JavaScript code demonstrates how to add new placeholder shapes to the Blank layout slide:

let presentation = new aspose.slides.Presentation();
try {
    // Get the Blank layout slide.
    let layout = presentation.getLayoutSlides().getByType(java.newByte(aspose.slides.SlideLayoutType.Blank));

    // Get the placeholder manager of the layout slide.
    let placeholderManager = layout.getPlaceholderManager();

    // Add different placeholders to the Blank layout slide.
    placeholderManager.addContentPlaceholder(20, 20, 310, 270);
    placeholderManager.addVerticalTextPlaceholder(350, 20, 350, 270);
    placeholderManager.addChartPlaceholder(20, 310, 310, 180);
    placeholderManager.addTablePlaceholder(350, 310, 350, 180);

    // Add a new slide with the Blank layout.
    let newSlide = presentation.getSlides().addEmptySlide(layout);

    presentation.save("Placeholders.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

The result:

The placeholders on the layout slide

In PowerPoint presentations, footer elements like date, slide number, and custom text can be shown or hidden depending on the slide layout. Aspose.Slides for Node.js allows you to control the visibility of these footer placeholders. This is useful when you want certain layouts to display footer information while others remain clean and minimal.

  1. Create an instance of the Presentation class.
  2. Get a layout slide reference by its index.
  3. Set the slide footer placeholder to visible.
  4. Set the slide number placeholder to visible.
  5. Set the date-time placeholder to visible.
  6. Save the presentation.

The following JavaScript code shows how to set the visibility of a slide footer and perform related tasks:

let presentation = new aspose.slides.Presentation("Presentation.ppt");
try {
    let headerFooterManager = presentation.getLayoutSlides().get_Item(0).getHeaderFooterManager();

    if (!headerFooterManager.isFooterVisible()) {
        headerFooterManager.setFooterVisibility(true);
    }

    if (!headerFooterManager.isSlideNumberVisible()) {
        headerFooterManager.setSlideNumberVisibility(true);
    }

    if (!headerFooterManager.isDateTimeVisible()) {
        headerFooterManager.setDateTimeVisibility(true);
    }

    headerFooterManager.setFooterText("Footer text");
    headerFooterManager.setDateTimeText("Date and time text");

    presentation.save("Presentation.ppt", aspose.slides.SaveFormat.Ppt);
} finally {
    presentation.dispose();
}

​In PowerPoint presentations, footer elements such as date, slide number, and custom text can be controlled at the master slide level to ensure consistency across all layout slides. Aspose.Slides for Node.js enables you to set the visibility and content of these footer placeholders on the master slide and propagate these settings to all child layout slides. This approach ensures uniform footer information throughout your presentation.​

  1. Create an instance of the Presentation class.
  2. Get a reference to the master slide by its index.
  3. Set the master’s and all child footer placeholders to visible.
  4. Set the master’s and all child slide number placeholders to visible.
  5. Set the master’s and all child date-time placeholders to visible.
  6. Save the presentation.

The following JavaScript code demonstrates this operation:

let presentation = new aspose.slides.Presentation("Presentation.ppt");
try {
    let headerFooterManager = presentation.getMasters().get_Item(0).getHeaderFooterManager();

    headerFooterManager.setFooterAndChildFootersVisibility(true);
    headerFooterManager.setSlideNumberAndChildSlideNumbersVisibility(true);
    headerFooterManager.setDateTimeAndChildDateTimesVisibility(true);

    headerFooterManager.setFooterAndChildFootersText("Footer text");
    headerFooterManager.setDateTimeAndChildDateTimesText("Date and time text");

    presentation.save("Output.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

FAQs

What’s the difference between a master slide and a layout slide?

A master slide defines the overall theme and default formatting, while layout slides define specific arrangements of placeholders for different types of content.

Can I copy a layout slide from one presentation to another?

Yes, you can clone a layout slide from one presentation’s layout slide collection, accessible via the getLayoutSlides method, and insert it into another presentation using the addClone method.

What happens if I delete a layout slide that’s still used by a slide?

If you try to delete a layout slide that is still referenced by at least one slide in the presentation, Aspose.Slides will throw a PptxEditException. To avoid this, use removeUnusedLayoutSlides which safely removes only the layout slides that are not in use.