本文介紹使用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的依賴,如下:
<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。
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中的文字可參考這篇文章。
(完)