Java 設定、刪除、獲取Word文件背景(基於Spire.Cloud.SDK for Java)

iceblue發表於2020-08-07

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

背景色讀取效果:

 

(完)

 

相關文章