Java 添加、删除、格式化Word中的图片步骤详解( 基于Spire.Cloud.SDK for Java )
本文介绍使用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中的文本可参考这篇文章。
到此这篇关于Java 添加、删除、格式化Word中的图片步骤详解( 基于Spire.Cloud.SDK for Java )的文章就介绍到这了,更多相关Java 添加、删除、格式化Word中的图片内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)