Updating SubTextParts using JavaScript Promises
When updating text while looping through SubTextParts using the given forEach method, the text will be replaced at wrong locations caused by concurrency. This article describes how to use async functions and promises to replace text in given SubTextParts.

When updating text while looping through Sub
Consider the following document with 3 SubTextParts that have been inserted with the following code:
TXTextControl.subTextParts.add("MySubTextPart1",1,21,14);
TXTextControl.subTextParts.add("MySubTextPart2",11,57,14);
TXTextControl.subTextParts.add("SubTextPart3",12,93,12);
Synchronous ForEach
The goal is to replace all SubTextParts that begin with the string "My". The obvious way to loop through the collection of SubTextParts would be by utilizing the forEach method in order to compare the name and to select and replace the text:
TXTextControl.subTextParts.forEach(stp => {
stp.getName(stpName => {
if (stpName.startsWith("My")) {
stp.getStart(start => {
stp.getLength(length => {
TXTextControl.select(start - 1, length, () => {
TXTextControl.selection.setText("New Text")
});
})
})
}
})
});
The problem with this code is that each function in the forEach loop is executed synchronously and results in the following output:
The replaced text is inserted after the last found SubTextPart.
Using Promises
The solution is the usage of asynchronous functions with a JavaScript Promise we can wait for until the text has been replaced for each SubTextPart individually. The asynchronous function replaceSubTextParts that is shown in the following code uses a for loop to call the replaceText function sequentially.
async function replaceSubTextParts(startsWithText) {
TXTextControl.subTextParts.getCount(async function(count) {
for (var i = count - 1; i >= 0; i--) {
await replaceText(i, startsWithText);
}
})
}
function replaceText(i, startsWithText) {
return new Promise(resolve => {
TXTextControl.subTextParts.elementAt(i, function(stp) {
stp.getName(function(name) {
if (name.startsWith(startsWithText)) {
stp.getStart(function(start) {
stp.getLength(function(length) {
TXTextControl.select(start - 1, length - 1, function() {
TXTextControl.selection.setText("Replaced SubTextPart Text");
TXTextControl.focus();
TXTextControl.clear();
resolve(true);
})
})
})
} else resolve(false);
})
})
});
}
After the text has been replaced, the Promise is resolved successfully and the next SubTextPart can be checked. The following screenshot shows that the first two SubTextParts have been replaced successfully.
ASP.NET
Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.
- Angular
- Blazor
- React
- JavaScript
- ASP.NET MVC, ASP.NET Core, and WebForms
Related Posts
Add JavaScript to PDFs with TX Text Control in C# .NET: Time-Based Alerts…
In this article, we explore how to enrich PDF documents with JavaScript using TX Text Control in C# .NET. Read on to learn how to create time-based alerts that trigger actions based on specific…
Observe When the Reporting Preview Tab is Active Using MutationObserver
This article shows how to observe when the Reporting Preview tab is active using MutationObserver. The Reporting Preview tab is a feature of the TX Text Control Document Editor that allows you to…
Loading Documents from Azure Blob Storage into the TX Text Control Document…
This article shows how to load documents from Azure Blob Storage into the TX Text Control Document Editor using pure JavaScript. It shows how to create a storage container and how to download…
Building an ASP.NET Core Backend Application to Host the Document Editor and…
This article explains how to create an ASP.NET Core backend application to host the Document Editor and Document Viewer. This backend application is required to provide the required functionality…
MailMerge: Working with Image Placeholders
The MailMerge class is able to merge form fields, merge fields and objects such as barcodes or images. This article shows how to use image placeholders and how to insert them programmatically.