Spire.Cloud.Word offers the replaceTextApi interface to replace old text in a Word document with new text. This tutorial will show you how to do it programmatically using Java codes.
import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.ReplaceTextApi;
public class ReplaceText {
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.cloudxdocs.com";
//Create a configuration object based on your App ID and App Key
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
//Create a replaceTextApi instance
static ReplaceTextApi replaceTextApi = new ReplaceTextApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
//Load a sample file
String name = "Sample.docx";
//Specify the old text to be replaced
String oldValue = "Doc";
//Specify the new text used to replace the old text in the sample
String newValue = "Word";
//Specify the password of the sample, it's null if nothing
String password = null;
//Specify the folder storing the sample, it's null if nothing
String folder = "input";
//Use the 2G storage provided by E-iceblue, it's null by default
String storage = null;
Boolean matchCase = false;
Boolean matchWholeWord = null;
Boolean replaceFirst = null;
//Specify the storage path of the resulting document
String destFilePath = "output/ReplaceWithText.docx"; ;
//Call the replaceWithText method to replace the old text in the sample with new text
replaceTextApi.replaceWithText(name, oldValue, newValue, destFilePath, password, folder, storage, matchCase, matchWholeWord, replaceFirst);
}
}
Output