Spire.Cloud.Word API 给开发者提供了BackgroundApi类用于设置或删除 Word 文档背景。本文将介绍如何使用 Spire.Cloud.Word API 给 Word 文档设置背景,包括背景颜色和背景图片。
详细步骤如下:
1、首先,请在 cloud.e-iceblue.cn 网站上注册一个账户并登录,然后点击导航栏“我的应用” ,创建一个应用,获得 App ID 和 App Key。
2、点击导航栏“文档管理”,将 Word 文档和背景图片上传至“我的文档”。
3. 创建 Maven 应用程序,在 pom.xml 文件中添加以下 Spire.Cloud.Word 的 Maven 依赖。详细步骤参考文章 通过 Maven 仓库安装 Spire.Cloud Web API。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>cloud</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> cloud </groupId>
<artifactId>spire.cloud.sdk</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.18</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>logging-interceptor</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId> com.squareup.okio </groupId>
<artifactId>okio</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>io.gsonfire</groupId>
<artifactId>gson-fire</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threetenbp</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
4. 新建 Java class,调用 Spire.Cloud.Word API 给 Word 文档设置背景颜色和背景图片。
设置背景颜色示例代码
import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.BackgroundApi;
import spire.cloud.word.sdk.client.model.Color;
public class BackgroundColor {
private static String appId = "APP ID";
private static String appKey = "APP Key";
public static void main(String[] args) throws ApiException {
//配置App ID和App Key
Configuration wordConfiguration = new Configuration(appId, appKey);
//创建BackgroundApi实例
BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration);
//原文档
String name = "template.docx";
//背景颜色
Color color = new Color(17, 181, 128);
//文档密码
String password = null;
//存放原文档的文件夹,没有则为null
String folder = null;
//使用冰蓝云配置的2G空间存贮文档,可设置为null
String storage = null;
//给文档设置背景颜色并保存到指定路径
String destFilePath = "output/setBackgroundColor.docx";
backgroundApi.setBackgroundColor(name, color, destFilePath, folder, storage, password);
}
}
设置背景图片示例代码
import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.BackgroundApi;
public class BackgroundImage {
private static String appId = "APP ID";
private static String appKey = "APP Key";
public static void main(String[] args) throws ApiException {
//配置App ID和App Key
Configuration wordConfiguration = new Configuration(appId, appKey);
//创建BackgroundApi实例
BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration);
//原文档
String name = "template.docx";
//背景图片
String imagePath = "background.png";
//文档密码
String password = null;
//存放原文档的文件夹,没有则为null
String folder = null;
//使用冰蓝云配置的2G空间存贮文档,可设置为null
String storage = null;
//给文档设置背景图片并保存到指定路径
String destFilePath = "output/setBackgroundImage.docx";
backgroundApi.setBackgroundImage(name, imagePath, destFilePath, folder, storage, password);
}
}