本文介紹使用Spire.Cloud.SDK for Java提供的ImagesApi接口來操作Word中的圖片。具體可通過addImage()方法添加圖片、deleteImage()方法刪除圖片、updateImageFormat()格式化Word中的圖片以及getImageFormat()獲取Word中的圖片格式等。操作方法和代碼示例可參考下文中的步驟。
步驟1:導入jar文件
創建Maven項目程序,通過maven倉庫下載導入。以IDEA為例,新建Maven項目,在pom.xml文件中配置maven倉庫路徑,并指定spire.cloud.sdk的依賴,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
<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
注:添加圖片時,可以將云端文件夾中的圖片添加到Word,也可以將本地路徑中的圖片添加到Word。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
import spire.cloud.word.sdk.client.*; import spire.cloud.word.sdk.client.api.ImagesApi; import java.io.File; public class AddImage { 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 ImagesApi imagesApi = new ImagesApi(wordConfiguration); public static void main(String[] args) throws ApiException { String name = "testfile.docx" ; //源文檔 String imagePath = "input/Javalogo.png" ; //云端文件夾下的圖片 //File inputImage = new File("C:/Users/Administrator/Desktop/images/logo/Javalogo.png");//本地圖片 String paragraphPath = "Section/0/Body/0/Paragraph/0" ; //指定段落 String folder = "input" ; //源文檔和圖片所在的云端文件夾 String storage = null ; String password = null ; Integer indexInParagraph = 1 ; String destFilePath = "output/AddImageToWord.docx" ; //調用方法將云端圖片添加到Word imagesApi.addImage(name, imagePath, paragraphPath, destFilePath, folder, storage, password, indexInParagraph); //調用方法將本地圖片添加到Word //imagesApi.addImageInRequest(name,inputImage,paragraphPath,destFilePath,folder,storage,password,indexInParagraph); } } |
圖片添加效果:
示例2—刪除Word中的圖片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import spire.cloud.word.sdk.client.ApiException; import spire.cloud.word.sdk.client.Configuration; import spire.cloud.word.sdk.client.api.ImagesApi; public class DeleteImage { 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 ImagesApi imagesApi = new ImagesApi(wordConfiguration); public static void main(String[] args) throws ApiException { String name = "Sample.docx" ; //包含圖片的Word源文檔 String paragraphPath = "Section/0/Body/0/Paragraph/2" ; //指定段落 Integer index = 0 ; String folder = "input" ; //源文檔所在文件 String storage = null ; String password = null ; String destFilePath = "output/DeleteImageInWord.docx" ; //結果文檔路徑 //調用方法刪除掉段落中的圖片 imagesApi.deleteImage(name, paragraphPath, index, destFilePath, folder, storage, password); } } |
圖片刪除效果:
示例3—設置Word中的圖片格式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import spire.cloud.word.sdk.client.ApiException; import spire.cloud.word.sdk.client.Configuration; import spire.cloud.word.sdk.client.api.ImagesApi; import spire.cloud.word.sdk.client.model.ImageFormat; public class UpdateImageFormat { 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 ImagesApi imagesApi = new ImagesApi(wordConfiguration); public static void main(String[] args) throws ApiException { String name = "Sample.docx" ; //源文檔 String paragraphPath = "Section/0/Body/0/Paragraph/2" ; Integer index = 0 ; //格式化圖片,設置高度、寬度、旋轉角度、坐標位置等 ImageFormat format = new ImageFormat(); format.setWidth( 200 ); format.setHeight( 150 ); format.setRotation( 15 ); format.setVerticalPosition( 50 ); format.setHorizontalPosition( 350 ); String folder = "input" ; //源文檔所在文件夾 String storage = null ; String password = null ; String destFilePath = "output/UpdateImageFormat.docx" ; //調用方法格式化圖片 imagesApi.updateImageFormat(name, paragraphPath, index, destFilePath, format, password, folder, storage); } } |
圖片格式化效果:
示例4—獲取Word中的圖片格式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import spire.cloud.word.sdk.client.ApiException; import spire.cloud.word.sdk.client.Configuration; import spire.cloud.word.sdk.client.api.ImagesApi; public class GetImageFormat { 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 ImagesApi imagesApi = new ImagesApi(wordConfiguration); public static void main(String[] args) throws ApiException { String name = "Sample.docx" ; String paragraphPath = "Section/0/Body/0/Paragraph/2" ; Integer index = 0 ; String folder = "input" ; String storage = null ; String password = null ; System.out.println(imagesApi.getImageFormat(name, paragraphPath, index, password, folder, storage)); } } |
附:Java 操作Word中的文本可參考這篇文章。
到此這篇關于Java 添加、刪除、格式化Word中的圖片步驟詳解( 基于Spire.Cloud.SDK for Java )的文章就介紹到這了,更多相關Java 添加、刪除、格式化Word中的圖片內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/Yesi/p/13424724.html