VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Java教程 >
  • Java 设置、删除、获取Word文档背景(基于Spire.Cloud.SDK for Java)

本文介绍使用Spire.Cloud.SDK for Java 提供的BackgroundApi接口来操作Word文档背景的方法,可设置背景,包括设置颜色背景setBackgroundColor()、图片背景setBackgroundImage(),删除背景deleteBackground()和获取背景颜色getBackgroundColor()等。可参照以下步骤来操作:

步骤1:导入jar文件

创建Maven项目程序,通过maven   http://repo.e-iceblue.cn/#browse/browse:maven-public仓库下载导入。以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);
    }
}
复制代码

背景色读取效果:

 

 

出  处:https://www.cnblogs.com/Yesi/p/13452439.html

 


相关教程