Products Technologies Demo Docs Blog Support Company

High-Performance Text Replacement in Large DOCX Files using C# .NET

Learn how to efficiently replace text in large DOCX files using C# .NET and the ServerTextControl component from Text Control. This article demonstrates the performance benefits of using the ServerTextControl component for high-volume text replacement.

High-Performance Text Replacement in Large DOCX Files using C# .NET

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.

Text Replacement in Large DOCX Documents

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 method of the ServerTextControl 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.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

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.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETASP.NET CoreDocument Viewer

Document Viewer 33.2.1 Released: New Event and Bug Fixes

This service pack includes important bug fixes and improvements to enhance the stability and performance of the Document Viewer. In addition, a new event has been introduced to provide developers…


ASP.NETASP.NET CoreDocument Viewer

Document Viewer: Long Polling Support for Loading Documents

The Document Viewer now supports long polling for loading documents. This allows an asynchronous loading of documents and is especially useful for large documents or slow connections.


ASP.NETASP.NET CoreDocument Viewer

Use PDF.js to Render PDF Documents within the Document Viewer

With the release of the latest release candidate of the Document Viewer, we released the ability to use PDF.js to render PDF documents. This article shows how to use this new feature.


ASP.NETASP.NET CoreDocument Viewer

Document Viewer 31.2.2 RC1 Released with New Features

We have just released the first release candidate (RC) of the next version of the Document Viewer. This article lists the new features and improvements in this major release.


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…