本文介绍使用Spire.Cloud.SDK for Java 提供的BackgroundApi接口来操作Word文档背景的方法,可设置背景,包括设置颜色背景setBackgroundColor()、图片背景setBackgroundImage(),删除背景deleteBackground()和获取背景颜色getBackgroundColor()等。可参照以下步骤来操作:
步骤1:导入jar文件
创建Maven项目程序,通过maven仓库下载导入。以IDEA为例,新建Maven项目,在pom.xml文件中配置maven仓库路径,并指定spire.cloud.sdk的依赖,如下:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>cloud</name>
<url>http://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> com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId> com.squareup.okhttp</groupId>
<artifactId>logging-interceptor</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId> com.squareup.okhttp </groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId> com.squareup.okio </groupId>
<artifactId>okio</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId> io.gsonfire</groupId>
<artifactId>gson-fire</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.18</version>
</dependency>
<dependency>
<groupId> org.threeten </groupId>
<artifactId>threetenbp</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
完成配置后,点击“Import Changes” 即可导入所有需要的jar文件。如果使用的是Eclipse,可参考这里的导入方法。
导入结果:
![2ac3abd159a19fddb6509661a61960e7.png](https://i-blog.csdnimg.cn/blog_migrate/42018dcc68d311aab3b1b612b6d9e787.png)
步骤2:登录冰蓝云账号,创建文件夹,上传用于测试的源文档
![1548a796b17042db07274d198a042af4.png](https://i-blog.csdnimg.cn/blog_migrate/39fd57f1d21fcf39f4de8f0024d61576.jpeg)
步骤3:创建应用程序,获取App ID及App Key
![400470f41905e96d755a6330c7a94366.png](https://i-blog.csdnimg.cn/blog_migrate/4ce6ba1102502ac5f6a4c4c4eaf83f25.jpeg)
完成以上步骤后,接下来可参考Java示例代码进行Word文档操作
示例1——设置Word背景颜色
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.*;
import spire.cloud.word.sdk.client.model.*;
public class BackgroundColor {
//配置App账号信息
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.e-iceblue.cn";
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
static BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration);
public static void main(String[] args) throws ApiException{
String name = "Test.docx";//Word源文档
Color color = new Color(245,245,220);//指定背景颜色
String password = null;//源文档密码
String folder = "input";//源文档所在的云端文件夹
String destFilePath = "output/setBackgroundColor.docx";//结果文档路径
String storage = null;
//调用方法设置背景颜色
backgroundApi.setBackgroundColor(name, color, destFilePath, folder, storage, password);
}
}
背景色设置效果:
![328674b29a39f7b15e8831d4f92d05a7.png](https://i-blog.csdnimg.cn/blog_migrate/da982d7f2eb73ca7cadad8f481130e3c.jpeg)
示例2——设置Word图片背景
可将云端图片或者本地路径图片设置为背景。
import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.BackgroundApi;
import java.io.File;
public class ImageBackground {
//配置App账号信息
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.e-iceblue.cn";
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
static BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
String name = "Test.docx";//Word源文档
String imagePath = "input/tp.png";//背景图片路径(云端input文件夹下)
//File inputImage = new File("inputFile/Background.png");//本地图片路径
String password = null;//源文档密码
String folder = "input";//源文档所在云端文件夹
String destFilePath = "output/setBackgroundImage.docx";//结果文档路径(云端output文件夹下)
String storage = null;
//调用方法将云端图片设置为背景图片
backgroundApi.setBackgroundImage(name, imagePath, destFilePath, folder, storage, password);
//将本地图片设置为背景图片
//backgroundApi.setBackgroundImageInRequest(name, inputImage, destFilePath, folder, storage, password);
}
}
图片背景设置效果:
![47c7cf6295747c75bae021262ebd9b1d.png](https://i-blog.csdnimg.cn/blog_migrate/b50dc69a468d4903637eed312766c0a2.jpeg)
示例3——删除Word背景
import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.BackgroundApi;
public class DeleteBackground {
//配置App账号信息
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.e-iceblue.cn";
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
static BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
String name = "setBackgroundColor.docx";//Word源文档
String password = null;//源文档密码
String folder = "output";//源文档所在云端文件夹
String destFilePath = "output/deleteBackground.docx";//结果文档路径
String storage = null;
//调用方法删除背景
backgroundApi.deleteBackground(name, destFilePath, password, folder, storage);
}
}
背景删除效果:
![c975972cc57455c15afab5eb445f4bb6.png](https://i-blog.csdnimg.cn/blog_migrate/5767bd1513524b52f47911c1573520aa.jpeg)
示例4——获取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 GetBackgroundColor {
//配置App账号信息
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.e-iceblue.cn";
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
static BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
String name = "setBackgroundColor.docx";//Word源文档
String password = null;//源文档密码
String folder = "output";//源文档所在云端文件夹
String storage = null;
//获取背景颜色
Color response = backgroundApi.getBackgroundColor(name, password, folder, storage);
System.out.println(response);
}
}
背景色读取效果:
![dee29fdc38863effabd65d3c6ad8981f.png](https://i-blog.csdnimg.cn/blog_migrate/b6478b6fdf6702c298f038e7729011a6.png)
(完)