Free Support Forum -

Find and replace Word document text in Java with Aspose.Words REST API

package com.aspose.words.examples;

    import java.nio.file.Path;
    import com.aspose.words.api.WordsApi;
    import com.aspose.words.model.ReplaceTextRequest;
    import com.aspose.words.model.ReplaceTextResponse;

public class FindReplaceTextExample {

public static void main(String[] args) {
    //ExStart: 1
    // Instantiate Aspose Storage API SDK
    StorageApi storageApi = new StorageApi(Configuration.apiKey, Configuration.appSID, true);
    // Instantiate Aspose Words API SDK
    WordsApi wordsApi = new WordsApi(Configuration.apiKey, Configuration.appSID, true);

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

    ReplaceTextRequest body = new ReplaceTextRequest();
    body.setOldValue ("aspose");
    body.setNewValue ( "");
    Path p1=Utils.getPath(FindReplaceTextExample.class, fileName);

    String revisionDateTime = "2017-02-20";
    String revisionAuthor = "Mateen";

        // 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");
    catch (Exception ex)
    //ExEnd: 1



Thank you for contacting support and sharing details.

You can download latest code from GitHub.

To run Java examples first of all you need to set your AppKey and AppSID in Configuration file.
Find and Replace example will replace text and save output file with name ‘SampleWordDocument.docx’ on cloud storage.

You can download output file from cloud storage on your local system using Download a Particular File example of StorageAPI. StorageAPI is already a part of Aspose.Words for Cloud Java project.

To setup project please visit link Aspose.Words Cloud SDK for Java.

If you have any further questions, please feel free to share.


Naeem Akram