Spire.Cloud.SDK for Java提供了TextRangesApi接口可通过addTextRange()添加文本、deleteTextRange()删除文本、updateTextRangeText()替换文本、updateTextRangeFormat()格式化文本等。本文将从以上方法介绍如何来实现对文本的操作。可参考以下步骤进行准备:

一、导入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,可参考这里的导入方法。

导入结果:

二、登录冰蓝云账号,创建文件夹,上传文档

三、创建应用程序,获取App ID及App Key

完成以上步骤后,可参考以下代码,进行文档操作。

用于测试的Word源文档如下:

1. 添加文本到Word

import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.TextRangesApi; public class AddTextRange {
//配置App账号信息
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 TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration); public static void main(String[] args) throws ApiException {
String name = "testfile.docx";//用于测试的Word源文档
String paragraphPath = "Section/0/Body/0/Paragraph/0";//获取文档中的段落
Integer indexInParagraph = 0;
String text = "新添加的文本内容!";//指定需要添加的文本内容
String folder = "input";//源文档所在的云端文件夹
String storage = null;//冰蓝云存储空间
String password = null;//源文档密码
String destFilePath = "output/AddTextRange.docx";//结果文档路径 //调用方法添加文本内容到Word段落
textRangesApi.addTextRange(name, paragraphPath, text, destFilePath, folder, storage, indexInParagraph, password);
}
}

文本添加效果:

2. 删除Word中的文本

import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.TextRangesApi; public class DeleteTextRange {
//配置App账号信息
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 TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration); public static void main(String[] args) throws ApiException {
String name = "testfile.docx";//源文档
String paragraphPath = "Section/0/Body/0/Paragraph/0";//获取段落
Integer index = 0;
String folder = "input";//源文档所在文件夹
String storage = null;//冰蓝云存储空间
String password = null;//源文档密码
String destFilePath = "output/DeleteTextRange.docx";//结果文档路径 //调用方法删除Word第一段文本
textRangesApi.deleteTextRange(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.TextRangesApi; public class UpdateTextRange {
//配置App账号信息
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 TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration); public static void main(String[] args) throws ApiException {
String name = "testfile.docx";//源文档
String paragraphPath = "Section/0/Body/0/Paragraph/0";//获取段落
Integer index = 0;
String text = "新替换文本";//指定新文本
String folder = "input";//源文档所在文件夹
String storage = null;
String password = null;
String destFilePath = "output/UpdateTextRangeText.docx";//结果文档路径 //调用方法更新(替换)原有的文本
textRangesApi.updateTextRangeText(name, paragraphPath, index, text, destFilePath, folder, storage, password);
}
}

文本替换效果:

4. 格式化Word中的文本

import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.TextRangesApi;
import spire.cloud.word.sdk.client.model.Color;
import spire.cloud.word.sdk.client.model.Font;
import spire.cloud.word.sdk.client.model.TextRangeFormat; public class UpdateTextRangeFormat {
//配置App账号信息
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 TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration); public static void main(String[] args) throws ApiException {
String name = "testfile.docx";//源文档
String paragraphPath = "Section/0/Body/0/Paragraph/0";//获取段落
Integer index = 0; //创建文本样式,指定字体、颜色、字号,并应用到文本
TextRangeFormat format = new TextRangeFormat();
Color color = new Color(34,139,34);
Font font = new Font("宋体", 20f, color);
format.setFont(font);
TextRangeFormat textRange = format; String folder = "input";//源文档所在文件夹
String storage = null;
String password = null;
String destFilePath = "output/UpdateTextRangeFormat.docx";//结果文档路径 //调用方法更新(应用)文本样式
textRangesApi.updateTextRangeFormat(name, paragraphPath, index, textRange, destFilePath, folder, storage, password);
}
}

文本格式设置效果:

(完)

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

  1. Java 设置、删除、获取Word文档背景(基于Spire.Cloud.SDK for Java)

    本文介绍使用Spire.Cloud.SDK for Java 提供的BackgroundApi接口来操作Word文档背景的方法,可设置背景,包括设置颜色背景setBackgroundColor().图 ...

  2. Java 添加、下载、读取PDF附件信息(基于Spire.Cloud.SDK for Java)

    Spire.Cloud.SDK for Java提供了PdfAttachmentsApi接口添加附件addAttachment().下载附件downloadAttachment().获取附件信息get ...

  3. Java 将PDF/XPS转为Word/html /SVG/PS/PCL/PNG、PDF和XPS互转(基于Spire.Cloud.SDK for Java)

    Spire.Cloud.SDK for Java提供了接口PdfConvertApi通过convert()方法将PDF文档以及XPS文档转为指定文档格式,如转PDF为Word(支持Docx.Doc). ...

  4. Java 设置Excel单元格格式—基于Spire.Cloud.SDK for Java

    本文介绍使用Spire.Cloud.SDK for Java来设置Excel单元格格式,包括字体.字号.单元格背景.字体下滑线.字体加粗.字体倾斜.字体颜色.单元格对齐方式.单元格边框等 一.下载SD ...

  5. C# 设置、删除、读取Word文档背景——基于Spire.Cloud.Word

    Spire.Cloud.Word.Sdk提供了接口SetBackgroudColor().SetBackgroudImage().DeleteBackground().GetBackgroudColo ...

  6. Java 添加、删除、格式化Word中的图片

    本文介绍使用Spire.Cloud.SDK for Java提供的ImagesApi接口来操作Word中的图片.具体可通过addImage()方法添加图片.deleteImage()方法删除图片.up ...

  7. Spire.Cloud.SDK for Java 合并、拆分Excel单元格

    Spire.Cloud.SDK for Java 是Spire.Cloud云产品系列中,用于处理Word.Excel.PowerPoint以及PDF文档的JAR文件,可执行文档编辑.转换.保存等操作. ...

  8. OCR/Vote disk 维护操作: (添加/删除/替换/移动) (文档 ID 1674859.1)

    适用于: Oracle Database - Enterprise Edition - 版本 10.2.0.1 到 11.2.0.1.0 [发行版 10.2 到 11.2]本文档所含信息适用于所有平台 ...

  9. Java 提取Word中的文本和图片

    本文将介绍通过Java来提取或读取Word文档中文本和图片的方法.这里提取文本和图片包括同时提取文档正文当中以及页眉.页脚中的的文本和图片. 使用工具:Free Spire.Doc for Java ...

随机推荐

  1. JS动画三剑客——setTimeout、setInterval、requestAnimationFrame

    一.前言 前端实现动画效果主要有以下几种方法:CSS3中的transition 和 animation ,Javascript 中可以通过定时器 setTimeout.setinterval,HTML ...

  2. object detection api调参详解(兼SSD算法参数详解)

    一.引言 使用谷歌提供的object detection api图像识别框架,我们可以很方便地重新训练一个预训练模型,用于自己的具体业务.以我所使用的ssd_mobilenet_v1预训练模型为例,训 ...

  3. ant design pro: protable控件隐藏【收起】按钮

    [collapseRender:()=>false] [效果] [参考ProTable源码]

  4. 【Oracle】如何让一个用户能够访问另外一个用户下所有的表

    根据需求的不同,也分为好几种方法,且看下文. 先构造基本的环境:创建两个用户AA,BB,基本需求为用户AA能够访问用户BB下所有的表,即用户AA有对BB下所有的表有"select on&qu ...

  5. springmvc json乱码问题

    在方法上加上:produces属性即可 @RequestMapping(produces = "application/json;charset=utf-8")

  6. 如何嵌套一个网页html到另一个html中

    在常规网页开发中(单页应用除外哈),经常会遇到把一些通用内容的页面集中到一个页面中,需要使用这些页面只需要包含引入即可,这样有利于维护和修改,当通用页面修改时只需更改一个文件就可以了,不需要每个文件单 ...

  7. Golang从合并链表聊递归

    从合并链表聊递归 递归是工程师最常见的一种解决问题的方式,但是有时候不容易真正掌握.有人说是看起来很简单,自己写起来会费点劲. 最著名的例子就是斐波那契数列(Fibonacci sequence),通 ...

  8. 常用API - 时间日期类

    Date类 概述 java.util.Date类 表示特定的瞬间,精确到毫秒. 继续查阅Date类的描述,发现Date拥有多个构造函数,只是部分已经过时,但是其中有未过时的构造函数可以把毫秒值转成日期 ...

  9. thinkphp将对象变成一维数组?

    thinkphp使用select查询出的数据为二维数组,如果想将二维数组转一维,可以使用: $x为二维数组对象 php版本要大于5.5.0 $userid_array = array_column($ ...

  10. Mybatis源码初探——优雅精良的骨架

    @ 目录 前言 精良的Mybatis骨架 宏观设计 基础支撑 日志 日志的加载 日志的使用 数据源 数据源的创建 池化技术原理 数据结构 获取连接 回收连接 缓存 缓存的实现 CacheKey 反射 ...