Java 添加、刪除、格式化Word中的圖片步驟詳解( 基于Spire.Cloud.SDK for Java )
本文介紹使用Spire.Cloud.SDK for Java提供的ImagesApi接口來操作Word中的圖片。具體可通過addImage()方法添加圖片、deleteImage()方法刪除圖片、updateImageFormat()格式化Word中的圖片以及getImageFormat()獲取Word中的圖片格式等。操作方法和代碼示例可參考下文中的步驟。
步驟1:導入jar文件
創(chuàng)建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:登錄冰藍云賬號,創(chuàng)建文件夾,上傳用于測試的源文檔

步驟3:創(chuàng)建應用程序,獲取App ID及App Key

完成以上步驟后,接下來可參考Java示例代碼進行Word文檔操作
示例1—添加圖片到Word
注:添加圖片時,可以將云端文件夾中的圖片添加到Word,也可以將本地路徑中的圖片添加到Word。
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中的圖片
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中的圖片格式
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中的圖片格式
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中的圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
mybatis執(zhí)行update批量更新時報錯的解決方案
這篇文章主要介紹了mybatis執(zhí)行update批量更新時報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
詳解Spring Data JPA動態(tài)條件查詢的寫法
本篇文章主要介紹了Spring Data JPA動態(tài)條件查詢的寫法 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
Java使用JDBC連接數(shù)據(jù)庫的實現(xiàn)方法
這篇文章主要介紹了Java使用JDBC連接數(shù)據(jù)庫的實現(xiàn)方法,包括了詳細的加載步驟以及完整實現(xiàn)示例,需要的朋友可以參考下2014-09-09
Spring MVC實現(xiàn)mysql數(shù)據(jù)庫增刪改查完整實例
這篇文章主要介紹了Spring MVC實現(xiàn)mysql數(shù)據(jù)庫增刪改查完整實例,從創(chuàng)建一個web項目開始,分享了項目結構以及具體Java代碼和前端頁面等相關內(nèi)容,具有一定借鑒價值,需要的朋友可以了解下。2017-12-12
Java中ThreadLocal避免內(nèi)存泄漏的方法詳解
ThreadLocal是Java中的一個線程本地存儲機制,它允許每個線程擁有一個獨立的本地存儲空間,用于存儲該線程的變量,本文主要介紹了ThreadLocal如何避免內(nèi)存泄漏,需要的朋友可以參考下2023-05-05
Java之Spring認證使用Profile配置運行環(huán)境講解
這篇文章主要介紹了Java之Spring認證使用Profile配置運行環(huán)境講解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07

