Layout Slide

This article demonstrates how to work with Layout Slides in Aspose.Slides for Android via Java. A layout slide defines the design and formatting inherited by normal slides. You can add, access, clone, and remove layout slides, as well as clean up unused ones to reduce presentation size.

Add a Layout Slide

You can create a custom layout slide to define reusable formatting. For example, you might add a text box that appears on all slides using this layout.

static void addLayoutSlide() {
    Presentation presentation = new Presentation();
    try {
        IMasterSlide masterSlide = presentation.getMasters().get_Item(0);

        // Create a layout slide with a blank layout type and a custom name.
        ILayoutSlide layoutSlide = presentation.getLayoutSlides().add(masterSlide, SlideLayoutType.Blank, "Main layout");

        // Add a text box to the layout slide.
        IAutoShape layoutTextBox = layoutSlide.getShapes().addAutoShape(ShapeType.Rectangle, 75, 75, 150, 150);
        layoutTextBox.getTextFrame().setText("Layout Slide Text");

        // Add two slides using this layout; both will inherit the text from the layout.
        presentation.getSlides().addEmptySlide(layoutSlide);
        presentation.getSlides().addEmptySlide(layoutSlide);
    } finally {
        presentation.dispose();
    }
}

💡 Note 1: Layout slides act as templates for individual slides. You can define common elements once and reuse them across many slides.

💡 Note 2: When you add shapes or text to a layout slide, all slides based on that layout will display this shared content automatically. The screenshot below shows two slides, each inheriting a text box from the same layout slide.

Slides Inheriting Layout Content

Access a Layout Slide

Layout slides can be accessed by index or by layout type (e.g., Blank, Title, SectionHeader, etc.).

static void accessLayoutSlide() {
    Presentation presentation = new Presentation();
    try {
        // Access a layout slide by index.
        ILayoutSlide firstLayoutSlide = presentation.getLayoutSlides().get_Item(0);

        // Access a layout slide by type.
        ILayoutSlide blankLayoutSlide = presentation.getLayoutSlides().getByType(SlideLayoutType.Blank);
    } finally {
        presentation.dispose();
    }
}

Remove a Layout Slide

You can remove a specific layout slide if it’s no longer needed.

static void removeLayoutSlide() {
    Presentation presentation = new Presentation();
    try {
        // Get a layout slide by type and remove it.
        ILayoutSlide blankLayoutSlide = presentation.getLayoutSlides().getByType(SlideLayoutType.Custom);
        presentation.getLayoutSlides().remove(blankLayoutSlide);
    } finally {
        presentation.dispose();
    }
}

Remove Unused Layout Slides

To reduce the presentation size, you may want to remove layout slides that are not used by any normal slides.

static void removeUnusedLayoutSlides() {
    Presentation presentation = new Presentation();
    try {
        // Automatically removes all layout slides not referenced by any slide.
        presentation.getLayoutSlides().removeUnused();
    } finally {
        presentation.dispose();
    }
}

Clone a Layout Slide

You can duplicate a layout slide using the addClone method.

static void cloneLayoutSlides() {
    Presentation presentation = new Presentation();
    try {
        // Get an existing layout slide by type.
        ILayoutSlide blankLayoutSlide = presentation.getLayoutSlides().getByType(SlideLayoutType.Blank);

        // Clone the layout slide to the end of the layout slide collection.
        ILayoutSlide clonedLayoutSlide = presentation.getLayoutSlides().addClone(blankLayoutSlide);
    } finally {
        presentation.dispose();
    }
}

Summary: Layout slides are powerful tools for managing consistent formatting across slides. Aspose.Slides allows full control over creating, managing, and optimizing layout slides.