Java 添加、刪除、替換、格式化Word中的文本的步驟詳解(基于Spire.Cloud.SDK for Java)
Spire.Cloud.SDK for Java提供了TextRangesApi接口可通過addTextRange()添加文本、deleteTextRange()刪除文本、updateTextRangeText()替換文本、updateTextRangeFormat()格式化文本等。本文將從以上方法介紹如何來實(shí)現(xiàn)對(duì)文本的操作??蓞⒖家韵虏襟E進(jìn)行準(zhǔn)備:
一、導(dǎo)入jar文件
創(chuàng)建Maven項(xiàng)目程序,通過maven倉(cāng)庫(kù)下載導(dǎo)入。以IDEA為例,新建Maven項(xiàng)目,在pom.xml文件中配置maven倉(cāng)庫(kù)路徑,并指定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>
完成配置后,點(diǎn)擊“Import Changes” 即可導(dǎo)入所有需要的jar文件。如果使用的是Eclipse,可參考這里的導(dǎo)入方法。
導(dǎo)入結(jié)果:

二、登錄冰藍(lán)云賬號(hào),創(chuàng)建文件夾,上傳文檔

三、創(chuàng)建應(yīng)用程序,獲取App ID及App Key

完成以上步驟后,可參考以下代碼,進(jìn)行文檔操作。
用于測(cè)試的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賬號(hào)信息
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";//用于測(cè)試的Word源文檔
String paragraphPath = "Section/0/Body/0/Paragraph/0";//獲取文檔中的段落
Integer indexInParagraph = 0;
String text = "新添加的文本內(nèi)容!";//指定需要添加的文本內(nèi)容
String folder = "input";//源文檔所在的云端文件夾
String storage = null;//冰藍(lán)云存儲(chǔ)空間
String password = null;//源文檔密碼
String destFilePath = "output/AddTextRange.docx";//結(jié)果文檔路徑
//調(diào)用方法添加文本內(nèi)容到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賬號(hào)信息
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;//冰藍(lán)云存儲(chǔ)空間
String password = null;//源文檔密碼
String destFilePath = "output/DeleteTextRange.docx";//結(jié)果文檔路徑
//調(diào)用方法刪除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賬號(hào)信息
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";//結(jié)果文檔路徑
//調(diào)用方法更新(替換)原有的文本
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賬號(hào)信息
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;
//創(chuàng)建文本樣式,指定字體、顏色、字號(hào),并應(yīng)用到文本
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";//結(jié)果文檔路徑
//調(diào)用方法更新(應(yīng)用)文本樣式
textRangesApi.updateTextRangeFormat(name, paragraphPath, index, textRange, destFilePath, folder, storage, password);
}
}
文本格式設(shè)置效果:

到此這篇關(guān)于Java 添加、刪除、替換、格式化Word中的文本的步驟詳解(基于Spire.Cloud.SDK for Java)的文章就介紹到這了,更多相關(guān)Java 添加、刪除、替換、格式化Word中的文本內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot整合spring-retry實(shí)現(xiàn)接口請(qǐng)求重試機(jī)制及注意事項(xiàng)
今天通過本文給大家介紹我們應(yīng)該如何使用SpringBoot來整合spring-retry組件實(shí)現(xiàn)重試機(jī)制及注意事項(xiàng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-08-08
spring boot整合spring-kafka實(shí)現(xiàn)發(fā)送接收消息實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于spring-boot整合spring-kafka實(shí)現(xiàn)發(fā)送接收消息的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來看看吧。2017-06-06
SMBMS超市訂單管理系統(tǒng)的網(wǎng)站源碼
這篇文章主要介紹了SMBMS超市訂單管理系統(tǒng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05
深入理解Java中的final關(guān)鍵字_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
Java中的final關(guān)鍵字非常重要,它可以應(yīng)用于類、方法以及變量。這篇文章中我將帶你看看什么是final關(guān)鍵字以及使用final的好處,具體內(nèi)容詳情通過本文學(xué)習(xí)吧2017-04-04
解決mybatis plus 一對(duì)多分頁(yè)查詢問題
這篇文章主要介紹了解決mybatis plus 一對(duì)多分頁(yè)查詢問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
關(guān)于通過java調(diào)用datax,返回任務(wù)執(zhí)行的方法
今天小編就為大家分享一篇關(guān)于通過java調(diào)用datax,返回任務(wù)執(zhí)行的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
SpringBoot前后端接口對(duì)接常見錯(cuò)誤小結(jié)
SpringBoot前后端接口對(duì)接工作時(shí),經(jīng)常遇到請(qǐng)求500,400等問題,本文主要介紹了SpringBoot前后端接口對(duì)接常見錯(cuò)誤小結(jié),感興趣的可以了解一下2022-01-01
Java Hibernate使用SessionFactory創(chuàng)建Session案例詳解
這篇文章主要介紹了Java Hibernate使用SessionFactory創(chuàng)建Session案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08

