首页 技术 正文
技术 2022年11月20日
0 收藏 506 点赞 2,489 浏览 4991 个字

本文介绍使用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,可参考这里的导入方法。

导入结果:

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

背景色读取效果:

(完)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894