当前位置 博文首页 > 文章内容

    Java 设置、删除、获取Word文档背景(基于Spire.Cloud.SDK for Java)

    作者: 栏目:未分类 时间:2020-08-07 14:00:42

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    本文介绍使用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);
        }
    }

    背景色读取效果:

     

    (完)