DEV Community

carlwils
carlwils

Posted on

 

Set the Slide Size in PowerPoint Using Java

When creating a PowerPoint document, apart from the default slide size, you can also set or change the slide size to a predefined size like Widescreen 16x9, Overhead, A3, A4, Banner, B4, and B5. To meet some specific requirements, PowerPoint also allows you to customize the slide size. In this article, you will learn how to programmatically set the slide size using a free Java library.

Import JAR Dependency (2 Methods)

● Download the free library (Free Spire.Presentation for Java) and unzip it, and then add the Spire.Presentation.jar file to your project as dependency.
● Directly 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>5.1.0</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Set slide size to a predefined size

The SlideSize.setType() method offered by Free Spire.Presentation for Java allows you to set the slide size to a predefined size effortlessly.

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

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

        //Create a Presentation object
        Presentation presentation = new Presentation();

        //Set the slide size to Screen 16x9
        presentation.getSlideSize().setType(SlideSizeType.OVERHEAD);

        //Add a shape to the first slide
        IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float(220, 150, 300, 100));
        shape.getTextFrame().setText("This example shows you how to set slide size to Widescreen 16X9.");

        //Save to file
        presentation.saveToFile("output/PredefinedSize.pptx", FileFormat.PPTX_2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

PredefinedSize

Customize slide size

To customize the slide size, set the size type to custom, and then apply a customized width and height to slide using the SlideSize.setSize() method.

import com.spire.presentation.*;

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class CustomizeSlideSize {

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

        //Create a Presentation object
        Presentation presentation = new Presentation();

        //Set the slide size type to custom
        presentation.getSlideSize().setType(SlideSizeType.CUSTOM);

        //Set the slide size to a custom size 
        presentation.getSlideSize().setSize(new Dimension(800,400));

        //Add a shape to the first slide
        IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float(20, 50, 500, 50));
        shape.getTextFrame().setText("This example shows you how to customize the slide size.");

        //Save to file
        presentation.saveToFile("output/CustomSize.pptx", FileFormat.PPTX_2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.