Spire.Cloud.PowerPoint 给 PowerPoint 文档设置背景颜色和背景图片

 

Spire.Cloud.PowerPoint提供的SlidesApi接口,可以给PowerPoint文档设置背景颜色和背景图片。本文将介绍如何使用该接口来给一个现有幻灯片文档设置纯色背景颜色、渐变色背景颜色以及添加背景图片。

详细步骤如下:

步骤一:创建一个maven程序,并通过maven仓库安装Spire.Cloud.SDK.jar包及其依赖。详细步骤可参考此篇文章

步骤二:通过冰蓝云官网(https://cloud.e-iceblue.cn/)注册账号并登陆,在“我的应用”版块创建应用程序,以获得App ID及App Key。

Spire.Cloud.PowerPoint 给 PowerPoint 文档设置背景颜色和背景图片

步骤三:上传PPT示例文档至冰蓝云官网的“文档管理”版块。

Spire.Cloud.PowerPoint 给 PowerPoint 文档设置背景颜色和背景图片

步骤四:在Maven程序中编写代码调用SlidesApi接口给示例PPT文档设置背景颜色及添加背景图片。

示例1、设置纯色背景颜色

import spire.cloud.powerpoint.sdk.ApiException;
import spire.cloud.powerpoint.sdk.Configuration;
import spire.cloud.powerpoint.sdk.api.SlidesApi;
import spire.cloud.powerpoint.sdk.model.FillFormat;
import spire.cloud.powerpoint.sdk.model.SlideBackground;
import spire.cloud.powerpoint.sdk.model.SolidFill;

public class SolidColor {
    static String appId = "App ID";
    static String appKey = "App Key";
    static String baseUrl = "https://api.e-iceblue.cn";

    public static void main(String[] args) throws ApiException {
        //配置App ID和App Key
        Configuration configuration = new Configuration(appId, appKey, baseUrl);
        //初始化SlidesApi对象
        SlidesApi slidesApi = new SlidesApi(configuration);
        //PowerPoint文档名称
        String name = "Sample.pptx";
        //文档的打开密码,没有则为null
        String password = null;
        //存放示例文档的文件夹,如果没有文件夹则为null
        String folder = null;
        //使用冰蓝云配置的2G空间存贮文档,可设置为null
        String storage = null;
        //指定设置背景的位置
        Integer slideIndex = 0;
        //设置纯色背景颜色
        SlideBackground source = new SlideBackground();
        FillFormat fillFormat = new FillFormat();
        fillFormat.setType("SolidFill");
        source.setFillFormat(fillFormat);
        source.setType("SolidFill");
        SolidFill fill = new SolidFill("lightpink");
        source.fillFormat(fill);
        //调用setSlideBackground方法设置纯色背景颜色
        slidesApi.setSlideBackground(name, slideIndex, source, folder, password, storage);
    }
}

Spire.Cloud.PowerPoint 给 PowerPoint 文档设置背景颜色和背景图片

示例2、设置渐变色背景颜色

import spire.cloud.powerpoint.sdk.ApiException;
import spire.cloud.powerpoint.sdk.Configuration;
import spire.cloud.powerpoint.sdk.api.SlidesApi;
import spire.cloud.powerpoint.sdk.model.*;
import java.util.ArrayList;

public class GradientColor {
    static String appId = "App ID";
    static String appKey = "App Key";
    static String baseUrl = "https://api.e-iceblue.cn";

    public static void main(String[] args) throws ApiException {
        //配置App ID和App Key
        Configuration configuration = new Configuration(appId, appKey, baseUrl);
        //初始化SlidesApi对象
        SlidesApi slidesApi = new SlidesApi(configuration);
        //示例文档名称
        String name = "Sample.pptx";
        //文档的打开密码,没有则为null
        String password = null;
        //存放示例文档的文件夹,如果没有文件夹则为null
        String folder = null;
        //使用冰蓝云配置的2G空间存贮文档,可设置为null
        String storage = null;
        //指定设置背景的位置
        Integer slideIndex = 0;
        //设置渐变色背景颜色
        SlideBackground source = new SlideBackground();
        FillFormat fillFormat = new FillFormat();
        fillFormat.setType("GradientFill");
        source.setFillFormat(fillFormat);
        source.setType("GradientFill");//设置背景颜色类型
        ArrayList stops = new ArrayList();
        GradientFillStop stop1 = new GradientFillStop();
        stop1.setColor("red");
        stop1.setPosition(0d);
        GradientFillStop stop2 = new GradientFillStop();
        stop2.setColor("white");
        stop2.setPosition(1d);
        stops.add(stop1);
        stops.add(stop2);//声明渐变色颜色
        GradientFill fileformat = new GradientFill();
        fileformat.setDirection("FromCorner1");//设置颜色方向
        fileformat.setShape("Linear");//设置形状
        fileformat.setStops(stops);
        fileformat.setLinearAngle(30d);//设置角度
        fileformat.setIsScaled(false);
        fileformat.setTileFlip("Horizontal");
        source.setFillFormat(fileformat);
        //调用setSlideBackground方法设置渐变色背景颜色
        slidesApi.setSlideBackground(name, slideIndex, source, folder, password, storage);
    }
}

Spire.Cloud.PowerPoint 给 PowerPoint 文档设置背景颜色和背景图片

示例3、设置背景图片

import spire.cloud.powerpoint.sdk.ApiException;
import spire.cloud.powerpoint.sdk.Configuration;
import spire.cloud.powerpoint.sdk.api.SlidesApi;
import spire.cloud.powerpoint.sdk.model.*;

public class BackgroundImage {
    static String appId = "App ID";
    static String appKey = "App Key";
    static String baseUrl = "https://api.e-iceblue.cn";

    public static void main(String[] args) throws ApiException {
        //配置App ID和App Key
        Configuration configuration = new Configuration(appId, appKey, baseUrl);
        //初始化SlidesApi对象
        SlidesApi slidesApi = new SlidesApi(configuration);
        //示例文档名称
        String name = "Sample.pptx";
        //文档的打开密码,没有则为null
        String password = null;
        //存放示例文档的文件夹,如果没有文件夹则为null
        String folder = null;
        //使用冰蓝云配置的2G空间存贮文档,可设置为null
        String storage = null;
        //指定设置背景的位置
        Integer slideIndex = 0;
        //设置背景图片
        SlideBackground source = new SlideBackground();
        source.setType("PictureFill");
        FillFormat fillFormat = new FillFormat();
        fillFormat.setType("PictureFill");
        source.setFillFormat(fillFormat);//设置背景类型
        String imgPath = "Image.jpg";//背景图片添加路径
        ResourceUriElement image = new ResourceUriElement();
        ResourceUri url = new ResourceUri();
        url.setHref(imgPath);
        url.setRelation(null);
        url.setLinkType(null);
        url.setTitle(null);
        image.setUri(url);
        PictureFill pictureFill = new PictureFill();
        pictureFill.setCropBottom(2d);
        pictureFill.setCropLeft(5d);
        pictureFill.setCropRight(2d);
        pictureFill.setCropTop(5d);
        pictureFill.setDpi(50);
        pictureFill.setImage(image);
        pictureFill.setBase64Data(null);
        pictureFill.setSvgData(null);
        pictureFill.setPictureFillMode("Tile");
        source.setFillFormat(pictureFill);
        //调用setSlideBackground方法来设置背景图片
        slidesApi.setSlideBackground(name, slideIndex, source, folder, password, storage);
    }
}

Spire.Cloud.PowerPoint 给 PowerPoint 文档设置背景颜色和背景图片