本文介紹使用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); } }
背景色讀取效果:

(完)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/98213.html
標籤:Java
下一篇:swoole是多行程還是多執行緒
