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);
string signedURI = Sign(strURI);
//serialize the JSON request content
ReplaceText replacetext = new ReplaceText();
ReplaceText replacetext = new ReplaceText();
// set old string to replace
replacetext.OldValue = OldValue;
replacetext.OldValue = OldValue;
// set new string to replace
replacetext.NewValue = NewValue;
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();
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; }
}
{
public string OldValue { get; set; }
public string NewValue { get; set; }
public bool IsMatchCase { get; set; }
public bool IsMatchWholeWord { get; set; }
}
Overview: Saaspose.Words
Saaspose.Words is a platform independent REST API used for cloud based document creation, manipulation & conversion. It allows converting document to DOC, DOCX, XPS, TIFF, PDF, HTML, SWF & many other formats. It can be used languages like .NET, Java, PHP, Ruby, Rails, Python, jQuery & many others. It can also be integrated with other cloud services to process documents. Other features include Create & modify watermark, content & formatting manipulation, mail merge abilities, reporting features.
More about Saaspose.Words
- Homepage of Saaspose.Words
- More Technical Tips by Saaspose.Words
- Ask technical questions/queries from Saaspose Support Team
Contact Information
Aspose Pty Ltd, Suite 163,
79 Longueville Road
Lane Cove, NSW, 2066
Australia
Saaspose - Your File Format Experts 2.0
sales@aspose.com
Phone: 1.214.329.1520
Fax: 866.810.9465
79 Longueville Road
Lane Cove, NSW, 2066
Australia
Saaspose - Your File Format Experts 2.0
sales@aspose.com
Phone: 1.214.329.1520
Fax: 866.810.9465
No comments:
Post a Comment