前言:
Spire.Cloud 在線編輯器是一款基于網頁的 Office 檔案編輯工具,支持在網頁中打開、編輯、列印 Word、Excel、PPT 檔案,支持將檔案保存到私有云盤,支持 IE、Chrome、FireFox、搜狗、遨游、360 等常見瀏覽器,Spire.Cloud Web API 能幫助開發人員能在任何時間、任何地點直接呼叫 SDK 介面對 Word、Excel、PPT、PDF 檔案進行操作,Spire.Cloud 支持 .NET、Java、PHP、Python、JavaScript 等多種編程語言,并提供了 1 萬次的免費呼叫次數及 2G 檔案記憶體,
本文將通過實體闡述,如何通過Spire.Cloud.Word API給開發人員提供的ShapesApi介面,在Word檔案中添加和洗掉形狀,
詳細步驟:
1、通過冰藍云官網( https://cloud.e-iceblue.cn/)注冊賬號并登陸,在“我的應用”版塊創建應用程式,獲取你的App ID及App Key,

2、上傳Word檔案至冰藍云官網的“檔案管理”版塊,為了便于檔案管理,您也可以先創建檔案夾“input”和“output”,然后將需要編輯的Word檔案上傳至input檔案夾下,output檔案夾用于存放生成的檔案,

3、創建Maven應用程式,通過Maven倉庫安裝Spire.Cloud.SDK jar包及依賴,詳細步驟可參考文章 ,
<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>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.18</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>logging-interceptor</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId> com.squareup.okio </groupId>
<artifactId>okio</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>io.gsonfire</groupId>
<artifactId>gson-fire</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threetenbp</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
4、新建Java class,輸入Java代碼來實作添加、洗掉Word形狀,
【示例1】添加形狀
import spire.cloud.word.sdk.client.*; import spire.cloud.word.sdk.client.api.ShapesApi; import spire.cloud.word.sdk.client.model.*; public class addShape { static String appId = "APP ID"; static String appKey = "APP Key"; static String baseUrl = "https://api.e-iceblue.cn"; //配置App ID和App Key static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl); //創建ShapesApi實體 static ShapesApi shapesApi = new ShapesApi(wordConfiguration); public static void main(String[] args) throws ApiException { //示例檔案名稱 String name = "addShape.docx"; //段落路徑 String paragraphPath = "Section/0/Body/0/Paragraph/0"; //需要添加形狀的段落 int indexInParagraph = 1; //存放示例檔案的檔案夾,沒有則為null String folder = "input"; //使用冰藍云配置的2G空間存貯檔案,可設定為null String storage = null; //示例檔案密碼,沒有則為null String password = null; //創建形狀,并設定其形狀型別、位置、填充顏色、旋轉角度、文字環繞方式等 ShapeFormat shapeFormat = new ShapeFormat(40f, 40f, ShapeFormat.ShapeTypeEnum.RECTANGLE); shapeFormat.setHorizontalOrigin(ShapeFormat.HorizontalOriginEnum.PAGE); shapeFormat.setVerticalOrigin(ShapeFormat.VerticalOriginEnum.PARAGRAPH); shapeFormat.setVerticalPosition(50f); shapeFormat.setHorizontalPosition(150f); Color color = new Color(0, 206, 209); shapeFormat.setFillColor(color); shapeFormat.setRotation(45f); shapeFormat.setStrokeWeight(2f); Color color_2 = new Color(173, 255, 47); shapeFormat.setStrokeColor(color_2); shapeFormat.setTextWrappingType(ShapeFormat.TextWrappingTypeEnum.BOTH); shapeFormat.setTextWrappingStyle(ShapeFormat.TextWrappingStyleEnum.INFRONTOFTEXT); shapeFormat.setZOrder(1); ShapeFormat shapeProperties = shapeFormat; //指定生成檔案的存放路徑 String destFilePath = "output/addShape_output.docx"; //呼叫addShape方法添加形狀到Word檔案 shapesApi.addShape(name, paragraphPath, shapeFormat, destFilePath,folder, storage, indexInParagraph, password); } }
使用Spire.Cloud在線編輯查看添加形狀后的Word檔案效果圖:

【示例2】洗掉形狀
import spire.cloud.word.sdk.client.*; import spire.cloud.word.sdk.client.api.ShapesApi; public class deleteShape { static String appId = "APP ID"; static String appKey = "APP Key"; static String baseUrl = "https://api.e-iceblue.cn"; //配置App ID和App Key static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl); //創建ShapesApi實體 static ShapesApi shapesApi = new ShapesApi(wordConfiguration); public static void main(String[] args) throws ApiException { //示例檔案名稱 String name = "getShape.docx"; //段落路徑 String paragraphPath = "Section/0/Body/0/Paragraph/0"; //指定需要洗掉形狀的索引 Integer index = 1; //存放示例檔案的檔案夾,沒有則為null String folder = "input"; //使用冰藍云配置的2G空間存貯檔案,可設定為null String storage = null; //示例檔案密碼,沒有則為null String password = null; //指定生成檔案的存放路徑 String destFilePath = "output/deleteShape.docx"; //呼叫deleteShape方法洗掉Word檔案中的形狀 shapesApi.deleteShape(name, paragraphPath, index, destFilePath, folder, storage, password); } }

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/59810.html
標籤:Java
下一篇:Set介面
