本文介绍使用Spire.Cloud.SDK for Java 提供的BackgroundApi接口来操作Word文档背景的方法,可设置背景,包括设置颜色背景setBackgroundColor()、图片背景setBackgroundImage(),删除背景deleteBackground()和获取背景颜色getBackgroundColor()等。可参照以下步骤来操作:
步骤1:导入jar文件
创建Maven项目程序,通过maven仓库下载导入。以IDEA为例,新建Maven项目,在pom.xml文件中配置maven仓库路径,并指定spire.cloud.sdk的依赖,如下:
com.e-iceblue
cloud
http://repo.e-iceblue.cn/repository/maven-public/
cloud
spire.cloud.sdk
3.5.0
com.google.code.gson
gson
2.8.1
com.squareup.okhttp
logging-interceptor
2.7.5
com.squareup.okhttp
okhttp
2.7.5
com.squareup.okio
okio
1.6.0
io.gsonfire
gson-fire
1.8.0
io.swagger
swagger-annotations
1.5.18
org.threeten
threetenbp
1.3.5
完成配置后,点击“Import Changes” 即可导入所有需要的jar文件。如果使用的是Eclipse,可参考这里的导入方法。
导入结果:
步骤2:登录冰蓝云账号,创建文件夹,上传用于测试的源文档
步骤3:创建应用程序,获取App ID及App Key
完成以上步骤后,接下来可参考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);
}
}
背景色设置效果:
示例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);
}
}
图片背景设置效果:
示例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);
}
}
背景删除效果:
示例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);
}
}
背景色读取效果:
(完)