Home > Code > C# > Find & Replace Text in Word Document Using REST API in .NET Application

Find & Replace Text in Word Document Using REST API in .NET Application

by sher azam   on Feb 21, 2013   Category: C#   |  Views: 1989    |  Points: 25   |  Gold 


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 = "http://api.saaspose.com/v1.0/words/input.docx/replaceText";
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<ReplaceTextResponse>(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);
}
responseStream.Close();

//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; }
}

More about Saaspose.Words

- Homepage of Saaspose.Words: http://saaspose.com/api/words

- More Technical Tips by Saaspose.Words: http://saaspose.com/docs/display/words/1.2+-+Saaspose.Words+Examples

- Ask technical questions/queries from Saaspose Support Team: http://saaspose.com/support/contact-us

Contact Information
Aspose Pty Ltd, Suite 163,
79 Longueville Road
Lane Cove, NSW, 2066
Australia
http://www.saaspose.com/
sales@saaspose.com
Phone: 1.214.329.1520
Fax: 866.810.9465



Post Code  |  Code Snippet Home

User Responses


  Re :Find & Replace Text in Word Document Using REST API in .NET Application   
Posted by Sherazam
on 29/08/2013
Points : 5

Please note that Saaspose is rebranded as “Aspose for Cloud”. Here is the blog post announcing the change: http://www.aspose.com/blogs/aspose-products/aspose-total-product-family/archive/2013/06/14/saaspose-is-now-aspose-for-cloud.html
To download and view detail information about this product please visit: http://www.aspose.com/cloud/word-api.aspx

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts