Java 添加、删除、格式化Word中的图片(基于Spire.Cloud.SDK for Java)

您所在的位置:网站首页 java的word转图片 Java 添加、删除、格式化Word中的图片(基于Spire.Cloud.SDK for Java)

Java 添加、删除、格式化Word中的图片(基于Spire.Cloud.SDK for Java)

2024-01-31 03:21| 来源: 网络整理| 查看: 265

本文介绍使用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的依赖,如下:

com.e-iceblue cloud http://repo.e-iceblue.cn/repository/maven-public/ cloud spire.cloud.sdk 3.5.0 com.google.code.gson gson 2.8.1 com.squareup.okhttp logging-interceptor 2.7.5 com.squareup.okhttp okhttp 2.7.5 com.squareup.okio okio 1.6.0 io.gsonfire gson-fire 1.8.0 io.swagger swagger-annotations 1.5.18 org.threeten threetenbp 1.3.5

完成配置后,点击“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)); } }

 

(完)

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3