本文介绍使用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. appium移动端自动化测试环境搭建windows-appium-android

    第一步:安装jdk 安装jdk(1.7版本以上) 安装完成设置jdk环境变量(百度查询) cmd命令下键入 java -version命令检查jdk是否安装成功且设置环境变量成功,如下图所示: 第二步 ...

  2. dotNetCore阅读源码-CreateDefaultBuilder及ConfigureWebHostDefaults内部

    版本:DotNetCore 3.1 CreateDefaultBuilder内部源码: public static IHostBuilder CreateDefaultBuilder(string[] ...

  3. 08 jwt源码剖析

    08 jwt源码剖析 目录 08 jwt源码剖析 1. jwt认证流程 2.jwt创建token 2.1 原理 2.2 jwt校验token 3. jwt使用 4. 源码剖析 总结: JSON Web ...

  4. Python Ethical Hacking - ARP Spoofing

    Typical Network ARP Spoofing Why ARP Spoofing is possible: 1. Clients accept responses even if they ...

  5. OSCP Learning Notes - Post Exploitation(3)

    Post-Exploit Password Attacks 1. Crack using the tool - john (Too slow in real world) Locate the roc ...

  6. 【Docker】Redis 安装使用教程

    1.安装 1.1 拉取镜像 docker pull redis redis:4.0 1.2 创建redis容器名"redistest1",并开启持久化 docker run -d ...

  7. 设计模式:singleton模式

    目的:限制类的实例个数只能是一个 例子: #define AGT_DECLARE_SINGLETON(ClassName) \ public: \ static ClassName *Instance ...

  8. 小白在使用ISE编写verilog代码综合时犯得错误及我自己的解决办法

    一:错误原因,顶层信号声明类别错误 错误前 更改后 二:综合时警告 更改前: 错误原因:调用子模块时 输出端口只能用wire类型变量进行映射 这是verilog语法规定的 tx_done在uart_t ...

  9. C#计算数组的算术平均数、几何平均数、调和平均数、平方平均数和中位数

    1.函数实现 0)打印数组 /// <summary> /// 打印数组 /// </summary> /// <param name="arr"&g ...

  10. placeholder CSS设置

    IE似乎一个冒号才生效,而chrome则是两个冒号才生效 input::-webkit-input-placeholder{ color:red; } input:-ms-input-placehol ...