When processing documents on a large scale, performance matters. Whether you're generating thousands of contracts, reports, or certificates, quickly and reliably manipulating large DOCX files is critical. TX Text Control is a solution that excels in flexibility and performance, especially when it comes to document automation.
TX Text Control is well-known for its robust, template-based document generation with merge fields. It seamlessly merges structured data, such as JSON, into pre-designed templates to generate output in DOCX, PDF, and other formats. But what if a document doesn't use merge fields?
In many real-world scenarios, documents are either legacy files or were created manually without merge fields. In these cases, developers still need to replace specific placeholder text with actual values dynamically at runtime.
Benchmark: 80 Pages, 30,000 Words
To demonstrate the raw performance of TX Text Control for text replacement, we conducted a test using an 80-page Office Open XML (DOCX) document containing approximately 30,000 words. In the document, we replaced five unique placeholders with real content.
The total time taken to:
- Load the document
- Replace all occurrences of the placeholders
- Save the document as DOCX
was just 0.46 seconds on a standard Ryzen 7 Eight-Core processor.
The document is loaded 50, 200, and 400 times in a loop, and the placeholders are replaced accordingly. This demonstrates the efficiency of TX Text Control in handling large-scale document processing tasks, as the performance remains consistently high.
Number of Documents | Total Pages | Total Words | Total Replacements | Total Time | Avg. Time per Document |
---|---|---|---|---|---|
1 | 80 | 30,000 | 5 | 0.46 seconds | 0.46 seconds |
50 | 4,000 | 1.5 million | 250 | 10.18 seconds | 0.20 seconds |
200 | 16,000 | 6 million | 1,000 | 37.21 seconds | 0.19 seconds |
400 | 32,000 | 12 million | 2,000 | 73.32 seconds | 0.18 seconds |
The diagram below shows how TX Text Control performs when processing multiple documents in a loop.
TX Text Control scales efficiently, improving performance as batch sizes increase. For example, processing a single document takes an average of 0.46 seconds, whereas processing 400 documents takes an average of just 0.18 seconds per document.
How to Replace Text
To replace text in a DOCX document using TX Text Control, you can use the Find ╰ TX Text Control .NET Server for ASP.NET
╰ TXTextControl Namespace
╰ ServerTextControl Class
╰ Find Method
Finds a text string in the main text of the document. method of the Server ╰ TX Text Control .NET Server for ASP.NET
╰ TXTextControl Namespace
╰ ServerTextControl Class
The ServerTextControl class implements a component that provide high-level text processing features for server-based applications. component. This method allows you to search for specific text within the document and replace it with new content.
The following example demonstrates how to use the Find method to replace text:
using System.Diagnostics; | |
using TXTextControl; | |
public static class Program | |
{ | |
public static void Main() | |
{ | |
var stopwatch = Stopwatch.StartNew(); | |
DocumentProcessor.ProcessDocument( | |
inputFilePath: "sample_80_pages.docx", | |
searchText: "%%company_name%%", | |
replacementText: "Text Control, LLC", | |
repeatCount: 50, | |
outputFolder: "saved" | |
); | |
stopwatch.Stop(); | |
Console.WriteLine($"\nTotal processing time: {stopwatch.Elapsed.TotalSeconds:F2} seconds"); | |
} | |
} | |
public static class DocumentProcessor | |
{ | |
public static void ProcessDocument(string inputFilePath, string searchText, string replacementText, int repeatCount, string outputFolder) | |
{ | |
EnsureDirectoryExists(outputFolder); | |
using var textControl = new ServerTextControl(); | |
textControl.Create(); | |
for (int i = 0; i < repeatCount; i++) | |
{ | |
textControl.Load(inputFilePath, StreamType.WordprocessingML); | |
ReplaceText(textControl, searchText, replacementText); | |
var outputPath = Path.Combine(outputFolder, $"{searchText}_replaced_{i}.docx"); | |
textControl.Save(outputPath, StreamType.WordprocessingML); | |
Console.WriteLine($"Processed document {i + 1} of {repeatCount} -> {outputPath}"); | |
} | |
} | |
private static void ReplaceText(ServerTextControl textControl, string searchText, string replacementText) | |
{ | |
var start = 0; | |
int index; | |
while ((index = textControl.Find(searchText, start, FindOptions.MatchWholeWord)) != -1) | |
{ | |
textControl.Selection.Text = replacementText; | |
start = index + replacementText.Length; | |
Console.WriteLine($"Replaced '{searchText}' with '{replacementText}' at index {index}."); | |
} | |
} | |
private static void EnsureDirectoryExists(string folderPath) | |
{ | |
if (!Directory.Exists(folderPath)) | |
{ | |
Directory.CreateDirectory(folderPath); | |
} | |
} | |
} |
Use Cases
Plain text replacement is a powerful technique, especially when merge fields are unavailable or impractical. It is useful for documents not designed for mail merge, for personalizing marketing or sales templates, and for building server-side automation pipelines requiring flexible text injection. Although merge fields offer greater control and structure, plain text replacement is a fast, efficient alternative for static documents or quick content transformations.
Key Takeaways
Thanks to its optimized internal handling and file I/O operations, TX Text Control scales efficiently and even better than linearly under certain conditions. With an average processing time of just 0.20 seconds per document in these benchmark scenarios, TX Text Control is ideal for high-throughput workloads such as document automation, content personalization, and server-side rendering.