We're sorry AsposeCloud doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.cloud

Replace multiple text items in a single API call using replaceText API of Aspose.Words Cloud

Is there a way to replace multiple fields in aspose.words cloud api? I would like to avoid having to make a call for every field I need to replace.

@appdevWEI

Thank you for contacting Aspose Support.

Please help us in understanding your use case. POST /words/{name}/replaceText API let you replace only a single text item in a document. You are looking for an API that let you replace multiple text items in a document? If yes, unfortunately, there is no such API but I have logged a request WORDSCLOUD-409 for our Development team to implement this API at the earliest.

The issues you have found earlier (filed as WORDSCLOUD-409) have been fixed in this update. This message was posted using Bugs notification tool by Ivanov_John

@appdevWEI

We have good news for you, in the recent SDKs of Aspose.Words Cloud we have implemented batch requests feature. It will help you to replace multiple text items in a batch request. Please find the sample code snippet for purpose.

            string sourceFile = @"....source.docx";
            string remoteName = "source.docx";
            string resultFile = @"....result.docx";

            // old and new texts
            string[] oldTexts = { "Old", "2020" };
            string[] newTexts = { "New", "2021" };

            // add all replacements as batch part requests
            var requests = new BatchPartRequest[oldTexts.Length + 2];
            int i = 0;

            // create requests
            using (var source = File.OpenRead(sourceFile))
            {
                requests[i++] = new BatchPartRequest(new UploadFileRequest
                {
                    FileContent = source,
                    Path = remoteName
                });

                for (; i <= oldTexts.Length; i++)
                {
                    requests[i] = new BatchPartRequest(new ReplaceTextRequest
                    {
                        Name = remoteName,
                        ReplaceText = new ReplaceTextParameters
                        {
                            OldValue = oldTexts[i - 1],
                            NewValue = newTexts[i - 1],
                            IsMatchCase = false,
                        }
                    });

                    // set dependecy tree to run request one by one.
                    requests[i].DependsOn(requests[i-1]);
                }

                requests[i] = new BatchPartRequest(new DownloadFileRequest
                {
                    Path = remoteName,
                });

                requests[i].DependsOn(requests[i - 1]);

                // run uploading, all replacements, and downloading as one batch request
                var results = this.WordsApi.Batch(requests);

                using (var fs = new FileStream(resultFile, FileMode.OpenOrCreate))
                {
                    // save the result to resultFile
                    (results.Last() as Stream).CopyTo(fs);
                }
            }