Java 给 PowerPoint 文档添加背景颜色和背景图片

在制作Powerpoint文档时,背景是非常重要的,统一的背景能让Powerpoint 演示文稿看起来更加干净美观。本文将详细讲述如何在Java应用程序中使用免费的Free Spire.Presentation for Java为幻灯片设置纯色背景颜色,渐变背景颜色以及添加背景图片。

Jar文件导入方法

方法一:

下载最新的Free Spire.Presentation for Java包并解压缩然后从lib文件夹下,Spire.Presentation.jar包导入到你的Java应用程序中。导入成功如下图所示

方法二:

通过Maven仓库安装导入详细的操作步骤请参考链接:

https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html

设置纯色背景颜色

 

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);
    }
}

纯色背景效果图:

 

设置渐变背景颜色

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);
    }
}

渐变背景色效果图:

添加背景图片

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);
    }
}

添加背景图效果:

 

原文地址:https://www.cnblogs.com/jazz-z/p/12699398.html