The background is very important when creating a Powerpoint document. If you unify the background, the Powerpoint presentation will look beautiful. In this article, I will show you how to use Free Spire.Presentation for Java in a Java application to set a solid background color and a gradient background color, and to add a background image for PowerPoint slides.
** Import JAR package ** ** Method 1: ** After downloading and unzipping Free Spire.Presentation for Java, in the lib folder Import the Spire.Presentation.jar package into your Java application as a dependency.
** Method 2: ** After installing the JAR package directly from the Maven repository, configure the pom.xml file as follows:
<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>2.6.1</version>
    </dependency>
</dependencies>
** Set a solid background color: **
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
public class PPTbackground {
    public static void main(String[] args) throws Exception {
        //Import PowerPoint document
        Presentation ppt = new Presentation();
        ppt.loadFromFile("file1.pptx");
        //Get the number of slides
        int slideCount = ppt.getSlides().getCount();
        ISlide slide = null;
        //Loop through the slides and set a solid background color for each slide
        for(int i = 0; i < slideCount;i++) {
            slide = ppt.getSlides().get(i);
            slide.getSlideBackground().setType(BackgroundType.CUSTOM);
            //Set a solid background fill
            slide.getSlideBackground().getFill().setFillType(FillFormatType.SOLID);
            slide.getSlideBackground().getFill().getSolidColor().setColor(Color.PINK);
        }
        //Save the result file
        ppt.saveToFile("bg1.pptx", FileFormat.PPTX_2010);
    }
}
** Effect image of plain background: **

** Set the background color of the gradient: **
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
public class PPTbackground {
    public static void main(String[] args) throws Exception {
        //Import PowerPoint document
        Presentation ppt = new Presentation();
        ppt.loadFromFile("file1.pptx");
        //Get the number of slides
        int slideCount = ppt.getSlides().getCount();
        ISlide slide = null;
        //Traverse slides and set a gradient background color for each slide
        for(int i = 0; i < slideCount;i++) {
            slide = ppt.getSlides().get(i);
            slide.getSlideBackground().setType(BackgroundType.CUSTOM);
            //Set the gradient background color fill
            slide.getSlideBackground().getFill().setFillType(FillFormatType.GRADIENT);
            slide.getSlideBackground().getFill().getGradient().getGradientStops().append(0, Color.WHITE);
            slide.getSlideBackground().getFill().getGradient().getGradientStops().append(1, Color.PINK);
        }
        //Save the result file
        ppt.saveToFile("bg2.pptx", FileFormat.PPTX_2010);
    }
}
** Effect image of gradient background color: **

** Add background image: **
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
public class PPTbackground {
    public static void main(String[] args) throws Exception {
        //Import PowerPoint document
        Presentation ppt = new Presentation();
        ppt.loadFromFile("file1.pptx");
        //Get the number of slides
        int slideCount = ppt.getSlides().getCount();
        ISlide slide = null;
        //Loop through the slides and add a background image to each slide
        for(int i = 0; i < slideCount;i++) {
            slide = ppt.getSlides().get(i);
            slide.getSlideBackground().setType(BackgroundType.CUSTOM);
            //Set the background fill of the image
            slide.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
            slide.getSlideBackground().getFill().getPictureFill().setAlignment(RectangleAlignment.NONE);
            slide.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
            slide.getSlideBackground().getFill().getPictureFill().getPicture().setUrl((new java.io.File("background.jpg ")).getAbsolutePath());
        }
        //Save the result file
        ppt.saveToFile("bg3.pptx", FileFormat.PPTX_2010);
    }
}
** Effect of adding background image: **

Recommended Posts