Forum Home
Welcome to the new
A message board system written in pure python using the web2py framework.
pyForum is sporting a new, completely re-designed interface and back-end
Download pyForum from
Find & Replace Text in Word Document in .NET Apps
By: Saaspose
Rank: Starfleet Lieutenant, Junior Grade
Postings: 38
Posted: Sun, 24 February, 2013 - 03:25 PM

This technical tip allows developers to replace text in Word document using Saaspose.Words REST API in your .NET applications. Some important steps for performing this task are to build URI to replace text, serialize the JSON request content, set old string to replace, set new string to replace, Parse the json string to JObject, sign URI, get response stream and the ReplaceText class is also given in detail.

Sample Code for replacing text in Word document

//build URI to replace text
string strURI = "";
string signedURI = Sign(strURI);
//serialize the JSON request content
ReplaceText replacetext = new ReplaceText();
// set old string to replace
replacetext.OldValue = OldValue;
// set new string to replace
replacetext.NewValue = NewValue;
// True indicates case-sensitive comparison, false indicates case-insensitive comparision.
replacetext.IsMatchCase = IsMatchCase;
// True indicates the oldValue must be a standalone word.
replacetext.IsMatchWholeWord = IsMatchWholeWord;
string strJSON = JsonConvert.SerializeObject(replacetext);
Stream responseStream = ProcessCommand(signedURI, "POST", strJSON);
StreamReader reader = new StreamReader(responseStream);
string strResponse = reader.ReadToEnd();
//Parse the json string to JObject
JObject pJSON = JObject.Parse(strResponse);
ReplaceTextResponse baseResponse = JsonConvert.DeserializeObject (pJSON.ToString());
//sign URI
signedURI = Sign(baseResponse.DocumentLink.Href + "?format=doc");
//get response stream
responseStream = ProcessCommand(signedURI, "GET");
using (Stream fileStream = System.IO.File.OpenWrite(outputPath))
CopyStream(responseStream, fileStream);
//Here is the ReplaceText class
public class ReplaceText
public string OldValue { get; set; }
public string NewValue { get; set; }
public bool IsMatchCase { get; set; }
public bool IsMatchWholeWord { get; set; }

This topic contains no responses.

System Announcements
Latest Posts
Support pyForum