Spire.Cloud.Word provides the ReplaceApi interface, which is used for replacing old strings in Word documents with new strings. This article demonstrates the same.
using System;
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
namespace ReplaceText
{
class Program
{
static String appId = "App ID";
static String appKey = "App Key";
static string basePath = " https://api.cloudxdocs.com";
//Create a Configuration object based on App ID and App Key
static Configuration wordConfiguration = new Configuration(appId, appKey, basePath);
//Create a ReplaceTextApi object
static ReplaceTextApi replaceTextApi = new ReplaceTextApi(wordConfiguration);
static void Main(string[] args)
{
//Specify the source document name
string name = "sample.docx";
//Specify the text to be replaced
string oldValue = "Spire.Doc";
//Specify new text that'll be replaced in
string newValue = "SPIRE.DOC FOR .NET";
//Specify the path and name of the generated document
string destFilePath = "output/ReplaceWithText.docx";
//Specify the open password of the source document, set to null if there’s no password
string password = null;
//Specify the folder where the source document is stored
string folder = "input";
//Use the default storage, set to null
string storage = null;
//Specify whether it is case sensitive
bool? matchCase = false;
//Specify whether it will match the whole word
bool? matchWholeWord = true;
//Specify whether it will only replace the first text
bool? replaceFirst = false;
//Call ReplaceWithText method to replace old text with new text
replaceTextApi.ReplaceWithText(name, oldValue, newValue, destFilePath, password, folder, storage, matchCase, matchWholeWord, replaceFirst);
}
}
}