Java: Set Backgrounds for PowerPoint Presentations

The background of a PowerPoint presentation sets the tone and mood of the presentation and can greatly enhance the aesthetic and impact of the slides. There are five types of backgrounds available in PowerPoint presentations, solid color backgrounds, gradient backgrounds, picture backgrounds, texture backgrounds, and patterned backgrounds. They each apply to different usage scenarios. For example, a professional business presentation may benefit from a clean and simple solid color background, while a creative presentation may use inspiring and interesting picture backgrounds to capture the audience's attention. This article is going to show how to set backgrounds for PowerPoint presentations through Java programs using Spire.Presentation for Java.

Install Spire.Presentation for Java

First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation</artifactId>
        <version>9.10.2</version>
    </dependency>
</dependencies>
    

Set a Solid Color Background for a PowerPoint Presentation

Before customizing the background, it is necessary to use the SlideBackground.setType(BackgroundType.CUSTOM) method to allow the customization of the background. Then, the background type can be set to solid color background using the SlideBackground.getFill().setFillType(FillFormatType.SOLID) method, and the color can be set using the FillFormat.getSolidColor().setColor() method.

The detailed steps are as follows:

  • Create an object of Presentation class.
  • Load a PowerPoint presentation using Presentation.loadFromFile() method.
  • Get the first slide using Presentation.getSlides().get() method.
  • Get the background of the slide using ISlide.getSlideBackground() method.
  • Set the background type to custom background to enable the customization of the background using SlideBackground.setType(BackgroundType.CUSTOM) method.
  • Set the fill type of the background to solid color using SlideBackground.getFill().setFillType(FillFormatType.SOLID) method.
  • Customize the background color using FillFormat.getSolidColor().setColor() method.
  • Save the presentation using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideBackground;
import com.spire.presentation.drawing.BackgroundType;
import com.spire.presentation.drawing.FillFormat;
import com.spire.presentation.drawing.FillFormatType;
import org.w3c.dom.css.RGBColor;

import java.awt.*;

public class SolidColor {
    public static void main(String[] args) throws Exception {
        //Create an object of Presentation class
        Presentation ppt = new Presentation();

        //Load a PowerPoint presentation
        ppt.loadFromFile("Sample.pptx");

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //Get the background
        SlideBackground background = slide.getSlideBackground();

        //Set the background type to custom
        background.setType(BackgroundType.CUSTOM);

        //Set the background fill type to solid color
        background.getFill().setFillType(FillFormatType.SOLID);

        //Set the background color
        FillFormat fillFormat = background.getFill();
        fillFormat.getSolidColor().setColor(new Color(199, 213, 237));

        //Save the presentation
        ppt.saveToFile("SolidColorBackground.pptx", FileFormat.AUTO);
    }
}

Java: Set Backgrounds for PowerPoint Presentations

Set a Gradient Background for a PowerPoint Presentation

Gradient background can be set by setting the background type to Gradient Background and then setting the gradient type, color, and angle. The detailed steps are as follows:

  • Create an object of Presentation class.
  • Load a PowerPoint presentation using Presentation.loadFromFile() method.
  • Get the first slide using Presentation.getSlides().get() method.
  • Get the background of the slide using ISlide.getSlideBackground() method.
  • Set the background type to custom background to enable the customization of the background using SlideBackground.setType(BackgroundType.CUSTOM) method.
  • Set the fill type of the background to gradient using SlideBackground.getFill().setFillType(FillFormatType.GRADIENT) method.
  • Set the gradient type to linear gradient using GradientFillFormat.setGradientShape(GradientShapeType.LINEAR) method.
  • Add the gradient stops and set the gradient colors using GradientFillFormat.getGradientStops().append() method.
  • Set the angle of the linear gradient using GradientFillFormat.getLinearGradientFill().setAngle() method.
  • Save the presentation using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideBackground;
import com.spire.presentation.drawing.*;

import java.awt.*;

public class Gradient {
    public static void main(String[] args) throws Exception {
        //Create an object of Presentation class
        Presentation ppt = new Presentation();

        //Load a PowerPoint presentation
        ppt.loadFromFile("Sample.pptx");

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //Get the background
        SlideBackground background = slide.getSlideBackground();

        //Set the background type to custom
        background.setType(BackgroundType.CUSTOM);

        //Set the background fill type to gradient color
        background.getFill().setFillType(FillFormatType.GRADIENT);

        //Set the gradient type to linear
        GradientFillFormat gradient = background.getFill().getGradient();
        gradient.setGradientShape(GradientShapeType.LINEAR);

        //Add gradient stops and set the colors
        gradient.getGradientStops().append(0f, new Color(230, 255, 255));
        gradient.getGradientStops().append(0.5f, new Color(255, 255, 255));
        gradient.getGradientStops().append(1f, new Color(199, 213, 237));

        //Set the angle of the linear gradient
        gradient.getLinearGradientFill().setAngle(90);

        //Save the presentation
        ppt.saveToFile("GradientBackground.pptx", FileFormat.AUTO);
    }
}

Java: Set Backgrounds for PowerPoint Presentations

Set a Picture Background for a PowerPoint Presentation

To set the picture background, set the background type to picture, set the picture fill type to stretch fill, and then set the background image. The detailed steps are as follows:

  • Create an object of Presentation class.
  • Load a PowerPoint presentation using Presentation.loadFromFile() method.
  • Load a picture using Presentation.getImages().append() method.
  • Get the first slide using Presentation.getSlides().get() method.
  • Get the background of the slide using ISlide.getSlideBackground() method.
  • Set the background type to custom background to enable the customization of the background using SlideBackground.setType(BackgroundType.CUSTOM) method.
  • Set the fill type of the background to picture using SlideBackground.getFill().setFillType(FillFormatType.PICTURE) method.
  • Set the picture fill type to stretch fill using PictureFillFormat.setFillType(PictureFillType.STRETCH) method.
  • Set the transparency of the background using PictureFillFormat.getPicture().setTransparency() method.
  • Set the background image using PictureFillFormat.getPicture().setEmbedImage() method.
  • Save the presentation using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideBackground;
import com.spire.presentation.drawing.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;

public class Picture {
    public static void main(String[] args) throws Exception {
        //Create an object of Presentation class
        Presentation ppt = new Presentation();

        //Load a PowerPoint presentation
        ppt.loadFromFile("Sample.pptx");

        //Load a picture
        IImageData image = ppt.getImages().append(ImageIO.read(new File("background.jpg")));

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //Get the background
        SlideBackground background = slide.getSlideBackground();

        //Set the background type to custom
        background.setType(BackgroundType.CUSTOM);

        //Set the background fill type to picture
        background.getFill().setFillType(FillFormatType.PICTURE);

        //Set the picture fill type to stretch
        PictureFillFormat pictureFillFormat = background.getFill().getPictureFill();
        pictureFillFormat.setFillType(PictureFillType.STRETCH);

        //Set the transparency of the background
        pictureFillFormat.getPicture().setTransparency(50);

        //Set the background picture
        pictureFillFormat.getPicture().setEmbedImage(image);

        //Save the presentation
        ppt.saveToFile("PictureBackground.pptx", FileFormat.AUTO);
    }
}

Java: Set Backgrounds for PowerPoint Presentations

Set a Texture Background for a PowerPoint Presentation

Setting a texture background is similar to setting a picture background. The difference is that the image fill type needs to be changed to a tiled fill and the texture alignment needs to be set. The detailed steps are as follows:

  • Create an object of Presentation class.
  • Load a PowerPoint presentation using Presentation.loadFromFile() method.
  • Load the texture using Presentation.getImages().append() method.
  • Get the first slide using Presentation.getSlides().get() method.
  • Get the background of the slide using ISlide.getSlideBackground() method.
  • Set the background type to custom background to enable the customization of the background using SlideBackground.setType(BackgroundType.CUSTOM) method.
  • Set the fill type of the background to picture using SlideBackground.getFill().setFillType(FillFormatType.PICTURE) method.
  • Set the picture fill type to tile fill using PictureFillFormat.setFillType(PictureFillType.TILE) method.
  • Set the transparency of the background using PictureFillFormat.getPicture().setTransparency() method.
  • Set the background texture using PictureFillFormat.getPicture().setEmbedImage() method.
  • Save the presentation using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;

import javax.imageio.ImageIO;
import java.io.File;

public class Texture {
    public static void main(String[] args) throws Exception {
        //Create an object of Presentation class
        Presentation ppt = new Presentation();

        //Load a PowerPoint presentation
        ppt.loadFromFile("Sample.pptx");

        //Load the texture
        IImageData image = ppt.getImages().append(ImageIO.read(new File("texture.png")));

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //Get the background
        SlideBackground background = slide.getSlideBackground();

        //Set the background type to custom
        background.setType(BackgroundType.CUSTOM);

        //Set the background fill type to picture
        background.getFill().setFillType(FillFormatType.PICTURE);

        //Set the picture fill type to tile
        PictureFillFormat pictureFillFormat = background.getFill().getPictureFill();
        pictureFillFormat.setFillType(PictureFillType.TILE);

        //Set the texture alignment
        pictureFillFormat.setAlignment(RectangleAlignment.TOP_LEFT);

        //Set the transparency of the background
        pictureFillFormat.getPicture().setTransparency(50);

        //Set the background texture
        pictureFillFormat.getPicture().setEmbedImage(image);

        //Save the presentation
        ppt.saveToFile("TextureBackground.pptx", FileFormat.AUTO);
    }
}

Java: Set Backgrounds for PowerPoint Presentations

Set a Pattern Background for a PowerPoint Presentation

In adding a pattern background, it is necessary to set the type of pattern as well as the foreground and background colors of the pattern. The detailed steps are as follows:

  • Create an object of Presentation class.
  • Load a PowerPoint presentation using Presentation.loadFromFile() method.
  • Get the first slide using Presentation.getSlides().get() method.
  • Get the background of the slide using ISlide.getSlideBackground() method.
  • Set the background type to custom background to enable the customization of the background using SlideBackground.setType(BackgroundType.CUSTOM) method.
  • Set the fill type of the background to pattern using SlideBackground.getFill().setFillType(FillFormatType.PATTERN) method.
  • Set the pattern type using PatternFillFormat.setPatternType() method.
  • Set the foreground color of the pattern using PatternFillFormat.getForegroundColor().setColor() method.
  • Set the background color of the pattern using PatternFillFormat.getBackgroundColor().setColor() method.
  • Save the presentation using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;

public class Pattern {
    public static void main(String[] args) throws Exception {
        //Create an object of Presentation class
        Presentation ppt = new Presentation();

        //Load a PowerPoint presentation
        ppt.loadFromFile("Sample.pptx");

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //Get the background
        SlideBackground background = slide.getSlideBackground();

        //Set the background type to custom
        background.setType(BackgroundType.CUSTOM);

        //Set the background fill type to pattern
        background.getFill().setFillType(FillFormatType.PATTERN);

        //Set the pattern type
        PatternFillFormat patternFillFormat = background.getFill().getPattern();
        patternFillFormat.setPatternType(PatternFillType.DOTTED_GRID);

        //Set the foreground color of the pattern
        patternFillFormat.getForegroundColor().setColor(new Color(230, 255, 255));

        //Set the background color of the pattern
        patternFillFormat.getBackgroundColor().setColor(new Color(199, 213, 237));

        //Save the presentation
        ppt.saveToFile("PatternBackground.pptx", FileFormat.AUTO);
    }
}

Java: Set Backgrounds for PowerPoint Presentations

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.