Adding Background Colors and Images to PowerPoint Documents in Java
When creating PowerPoint presentations, bcakground design plays a significant role in anhancing visual appeal and maintaining consistency across slides. This article explains how to use the free Free Spire.Presentation for Java library to apply solid color backgrounds, gradient fills, and image backgrounds to slides within a PowerPoint document.
Including the Library
Option 1:
Download the latest version of the Free Spire.Presentation for Java package, extract it, and add the Spire.Presentation.jar file located in the lib folder to your Java project. (Once successfully added, it should appear as shown in the illustration below.)
Option 2:
Install via Maven repository. For detailed steps, refer to:
https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html
Applying Solid Color Backgrounds
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
public class SlideBackgroundSetter {
public static void main(String[] args) throws Exception {
// Load the presentation
Presentation document = new Presentation();
document.loadFromFile("Sample.pptx");
// Retrieve total number of slides
int slideCount = document.getSlides().getCount();
ISlide currentSlide = null;
// Iterate through each slide to set solid background color
for(int index = 0; index < slideCount; index++) {
currentSlide = document.getSlides().get(index);
currentSlide.getSlideBackground().setType(BackgroundType.CUSTOM);
// Apply solid fill color
currentSlide.getSlideBackground().getFill().setFillType(FillFormatType.SOLID);
currentSlide.getSlideBackground().getFill().getSolidColor().setColor(Color.lightGray);
}
// Save the modified document
document.saveToFile("SolidBackground.pptx", FileFormat.PPTX_2010);
}
}
<br></br>
Result with Solid Background:
Setting Gradient Backgrounds
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
public class SlideBackgroundSetter {
public static void main(String[] args) throws Exception {
// Load the presentation
Presentation document = new Presentation();
document.loadFromFile("Sample.pptx");
// Retrieve total number of slides
int slideCount = document.getSlides().getCount();
ISlide currentSlide = null;
// Iterate through each slide to set gradient background
for(int index = 0; index < slideCount; index++) {
currentSlide = document.getSlides().get(index);
currentSlide.getSlideBackground().setType(BackgroundType.CUSTOM);
// Configure gradient fill
currentSlide.getSlideBackground().getFill().setFillType(FillFormatType.GRADIENT);
currentSlide.getSlideBackground().getFill().getGradient().getGradientStops().append(0, Color.WHITE);
currentSlide.getSlideBackground().getFill().getGradient().getGradientStops().append(1, Color.LIGHT_GRAY);
}
// Save the modified document
document.saveToFile("GradientBackground.pptx", FileFormat.PPTX_2010);
}
}
Result with Gradient Bacgkround:
Adding Background Images
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
public class SlideBackgroundSetter {
public static void main(String[] args) throws Exception {
// Load the presentation
Presentation document = new Presentation();
document.loadFromFile("Sample.pptx");
// Retrieve total number of slides
int slideCount = document.getSlides().getCount();
ISlide currentSlide = null;
// Iterate through each slide to add background image
for(int index = 0; index < slideCount; index++) {
currentSlide = document.getSlides().get(index);
currentSlide.getSlideBackground().setType(BackgroundType.CUSTOM);
// Set image as background
currentSlide.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
currentSlide.getSlideBackground().getFill().getPictureFill().setAlignment(RectangleAlignment.NONE);
currentSlide.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
currentSlide.getSlideBackground().getFill().getPictureFill().getPicture().setUrl((new java.io.File("1.png")).getAbsolutePath());
}
// Save the modified document
document.saveToFile("ImageBackground.pptx", FileFormat.PPTX_2010);
}
}
**Result with Image Background: