Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Adding Background Colors and Images to PowerPoint Documents in Java

Tech May 19 1

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:

Tags: Java

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.