Free Support Forum - aspose.cloud

How to replace multiple strings in Word Document in Java using Aspose.Words REST API

Hello,


I have found an example to replace one string. But I need an example for multiple strings (words). Please give me an example code for Java that works with Aspose Words Cloud for multiple strings.

Thank you,
Sven
Dear sven,

Thank you for contacting Aspose Support.

Aspose.Words for Cloud allows you to replace multiple occurrences of a word within a word document. You can see the Java code below. As you can see, in this example we are replacing all occurrences of the word aspose with "aspose.com".


String fileName = "SampleWordDocument.docx";
String storage = null;
String folder = null;

ReplaceTextRequest body = new ReplaceTextRequest();
body.setOldValue ("aspose");
body.setNewValue ( "aspose.com");
body.setIsOldValueRegex(false);
Path p1=Utils.getPath(FindReplaceTextExample.class, fileName);
String revisionDateTime = "2017-02-20";
String revisionAuthor = "Mateen";
try
{
// Upload the file
storageApi.PutCreate(fileName, "", "", p1.toFile());
// Invoke Aspose.Words Cloud SDK API to replace text
ReplaceTextResponse apiResponse = wordsApi.PostReplaceText(fileName, fileName, storage, folder, body,revisionAuthor,revisionDateTime);

if (apiResponse != null && apiResponse.getStatus().equals("OK"))
{
System.out.println("Text replaced successfully");
}
}

Aspose.Words for Cloud also supports replacing words by using regex expressions. You can use this by using setIsOldValueRegex with true/false to indicate whether the old value you have entered is a Regular Expression or a plain word.

We currently do not support replacing multiple words that do not match a single regular expression.

You can find complete working examples by reading Working with Find and Replace documentation

Should you have any further query, please feel free to contact.

Mateen Sajjad
Aspose – File Format APIs

Got a Complaint? File it!

Keep in touch! We're on Twitter and Facebook