Java adds background color and background image to PowerPoint document

When creating a Powerpoint document, the background is very important. A unified background can make the Powerpoint presentation look cleaner and more beautiful. This article will detail how to use the free Free Spire. Presentation for Java in Java applications to set a solid background color, gradient background color and add a background picture for the slide.

Jar file import method

Method 1:
Download and unzip the latest Free Spire.Presentation for Java package, and then import the Spire.Presentation.jar package from your lib folder into your Java application. (As shown below after successful import)

Java adds background color and background image to PowerPoint document

Method 2:
Install and import through Maven repository. For detailed operation steps, please refer to the link:
https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html

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 {

        //加载PowerPoint文档
        Presentation ppt = new Presentation();
        ppt.loadFromFile("Sample.pptx");

        //获取幻灯片的数量
        int slideCount = ppt.getSlides().getCount();

        ISlide slide = null;

        //遍历幻灯片,为每张幻灯片设置纯色背景色
        for(int i = 0; i < slideCount;i++) {
            slide = ppt.getSlides().get(i);
            slide.getSlideBackground().setType(BackgroundType.CUSTOM);

            //设置纯色背景填充
            slide.getSlideBackground().getFill().setFillType(FillFormatType.SOLID);
            slide.getSlideBackground().getFill().getSolidColor().setColor(Color.lightGray);
        }
        //保存结果文档
        ppt.saveToFile("纯色背景.pptx", FileFormat.PPTX_2010);
    }
}

Solid color background renderings:

Java adds background color and background image to PowerPoint document


Set gradient 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 {

        //加载PowerPoint文档
        Presentation ppt = new Presentation();
        ppt.loadFromFile("Sample.pptx");

        //获取幻灯片的数量
        int slideCount = ppt.getSlides().getCount();

        ISlide slide = null;

        //遍历幻灯片,为每张幻灯片设置渐变背景色
        for(int i = 0; i < slideCount;i++) {
            slide = ppt.getSlides().get(i);
            slide.getSlideBackground().setType(BackgroundType.CUSTOM);

            //设置渐变背景色填充
        slide.getSlideBackground().getFill().setFillType(FillFormatType.GRADIENT);
        slide.getSlideBackground().getFill().getGradient().getGradientStops().append(0, Color.WHITE);
        slide.getSlideBackground().getFill().getGradient().getGradientStops().append(1, Color.LIGHT_GRAY);

        }
        //保存结果文档
        ppt.saveToFile("渐变色背景.pptx", FileFormat.PPTX_2010);
    }
}

Effect picture of gradient background color:

Java adds background color and background image to PowerPoint document

**

Add background image **

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

public class PPTbackground {

    public static void main(String[] args) throws Exception {
        //加载PowerPoint文档
        Presentation ppt = new Presentation();
        ppt.loadFromFile("Sample.pptx");

        //获取幻灯片的数量
        int slideCount = ppt.getSlides().getCount();
        ISlide slide = null;

        //遍历幻灯片,为每张幻灯片添加背景图片
        for(int i = 0; i < slideCount;i++) {
            slide = ppt.getSlides().get(i);
            slide.getSlideBackground().setType(BackgroundType.CUSTOM);

            //设置图片背景填充
            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("1.png")).getAbsolutePath());
        }

        //保存结果文档
        ppt.saveToFile("背景图片.pptx", FileFormat.PPTX_2010);
    }
}

Add background image effect:

Java adds background color and background image to PowerPoint document

Guess you like

Origin blog.51cto.com/14765544/2487296