本文介绍使用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中的图片的更多相关文章

  1. Java 添加、提取PDF中的图片

    Spire.Cloud.SDK for Java提供了PdfImagesApi接口可用于添加图片到PDF文档addImage().提取PDF中的图片extractImages(),具体操作步骤和Jav ...

  2. Java 添加、删除、替换、格式化Word中的文本(基于Spire.Cloud.SDK for Java)

    Spire.Cloud.SDK for Java提供了TextRangesApi接口可通过addTextRange()添加文本.deleteTextRange()删除文本.updateTextRang ...

  3. Java利用poi生成word(包含插入图片,动态表格,行合并)

    转(小改): Java利用poi生成word(包含插入图片,动态表格,行合并) 2018年12月20日 09:06:51 wjw_11093010 阅读数:70 Java利用poi生成word(包含插 ...

  4. word中更改图片和标题之间的垂直距离

    word中插入图片后.往往须要给图片加上标题. 你插入图片和给图片插入标题时,word用的是默认的格式给你插入的图片和标题. 假如原来的paragraph是2倍行距.你的图片和标题之间的距离也是2倍行 ...

  5. 利用POI抽取word中的图片并保存在文件中

    利用POI抽取word中的图片并保存在文件中 poi.apache.org/hwpf/quick-guide.html 1.抽取word doc中的图片 package parse; import j ...

  6. 如何把word中的图片怎么导出来呢?

    在办公使用word的过程中你可能经常会遇到这个问题:插入到word中的图片找不到导出来的方法,是不是很郁闷呢,别急,今天咱们研究一下把word中的图片导出来的方法(把"我的"变成你 ...

  7. 写带有清晰图片的博客:如何将word中的图片复制到windows live writer保持大小不变--清晰度不变

    写blog的习惯,先在word写了,复制到windows live writer,再发布到博客园.word中的文章,图片有缩放比例,复制到windows live writer后图片变得不清晰.除了一 ...

  8. 如何将word中的图片和文字导入自己的博客中

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  9. 怎样将word中的图片插入到CSDN博客中

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

随机推荐

  1. 从零开始学Electron笔记(二)

    在之前的文章我们简单介绍了一下Electron可以用WEB语言开发桌面级应用,接下来我们继续说一下Electron的菜单创建和事件绑定. 我们接上一章的代码继续编写,上一章代码 https://www ...

  2. 【XCTF】ics-05

    信息: 题目来源:XCTF 4th-CyberEarth 标签:PHP.伪协议 题目描述:其他破坏者会利用工控云管理系统设备维护中心的后门入侵系统 解题过程 题目给了一个工控管理系统,并提示存在后门, ...

  3. Maven 专题(九):后记

    尚硅谷视频链接:https://www.bilibili.com/video/av84877781/看视频的时候,根据自己的需要,访问量多的不一定是好的,适合自己的才是最好的,总的来说,尚硅谷的视频质 ...

  4. Linux如何用脚本监控Oracle发送警告日志ORA-报错发送邮件

    Linux如何用脚本监控Oracle发送警告日志ORA-报错发送邮件 前言 公司有购买的监控软件北塔系统监控,由于购买的版权中只包含了有限台数据库服务器的监控,所以只监控了比较重要的几台服务器. 后边 ...

  5. C与lua交互--lua调用栈分析(2)

    0 预备知识: 至少对Lua手册C API有简单的了解.lua5.3手册中文 1  Lua虚拟机的栈,如图: 假设虚拟机的栈有n个数据: 解释: 两种解释方式: A,栈顶开始, -1 ...-n B, ...

  6. educoder SML程序设计题线下编译环境搭建

    背景 最近<串并行数据结构与算法设计>的老师在educoder上布置了一些SML程序设计题,虽然网站上有在线编译功能,但还是在线下编译调试方便,特记录编译环境过程如下(我用的GVIM,但N ...

  7. ASP.NET Core3.1使用Identity Server4建立Authorization Server-2

    前言 建立Web Api项目 在同一个解决方案下建立一个Web Api项目IdentityServer4.WebApi,然后修改Web Api的launchSettings.json.参考第一节,当然 ...

  8. 软件测试中的微信小程序怎么测试?

    1.没有需求文档时,如何测试小程序?现在大多数公司的开发模式是:敏捷模式(用户故事) ,即以什么身份做什么事情会出现什么样的结果.那实际测试过程中,没有需求文档时,测试可以采用以下方式更好的完成测试工 ...

  9. P1866 编号

    大致题意: 求太郎的n只兔子的编号的排列种数 基本思路: 每只兔子都有一个范围,那么每只兔子的范围就是这只兔子选择编号的种数. 以5 8为例,第一只兔子有5种,第二只兔子有8种,而第一只兔子选择后,第 ...

  10. CodeForces - 722C Destroying Array (并查集/集合的插入和删除)

    原题链接:https://vjudge.net/problem/511814/origin Description: You are given an array consisting of n no ...