DEV Community

CodeSharing
CodeSharing

Posted on

Apply Slide Layouts in PowerPoint in Java

Slide layouts are some simply “ready-to-use” slide templates that contain formatting, positioning, and placeholders. A properly constructed slide layout will greatly improve the clarity and readability of your slide content. This article will demonstrate how to apply slide layouts in PowerPoint via Java. (API: Free Spire.Presentation for Java)

Installation
Method 1: Download the Free Spire.Presentation for Java and unzip it. Then add the Spire.Presentation.jar file to your project as dependency.

Method 2: You can also add the jar dependency to your maven project by adding the following configurations to the pom.xml.

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation.free</artifactId>
        <version>3.9.0</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

1# Apply a title slide layout:

import com.spire.presentation.*;

public class SlideLayout {
    public static void main(String[] args) throws Exception{

        //Create an instance of presentation document
        Presentation ppt = new Presentation();

        //Remove the default slide
        ppt.getSlides().removeAt(0);

        //Append a slide and set its layout
        ISlide slide = ppt.getSlides().append(SlideLayoutType.TITLE);

        //Add content for Title and Text
        IAutoShape shape = (IAutoShape)slide.getShapes().get(0);
        shape.getTextFrame().setText("The Scarlet Letter");

        shape = (IAutoShape)slide.getShapes().get(1);
        shape.getTextFrame().setText("A Dark Romanticism Fiction");

        //Save the document
        ppt.saveToFile("SlideLayout.pptx", FileFormat.PPTX_2013);
        ppt.dispose();
    }
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

2# Apply 11 kinds of different slide layouts:

import com.spire.presentation.*;

public class DifferentSlideLayout {
    public static void main(String[] args) throws Exception{

        //Create a PPT document
        Presentation presentation = new Presentation();

        //Remove the default slide
        presentation.getSlides().removeAt(0);

        //Loop through slide layouts
        for (SlideLayoutType type : SlideLayoutType.values())
        {
            //Append slide by specified slide layout
            presentation.getSlides().append(type);
        }

        //Save the document
        presentation.saveToFile("Result.pptx", FileFormat.PPTX_2013);
        presentation.dispose();
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)