# Large Documents

> Handle long documents in Windows Forms: 64-bit builds, Selection objects and Selection.Load in one undo step, text replacement in large DOCX files, PDF size.

- **Product:** TX Text Control .NET for Windows Forms
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for Windows Forms > Performance > Large Documents
- **2822 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/performance/large-documents/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/performance/large-documents/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/performance/large-documents/llms-full.txt

---

A contract of several hundred pages, a report built from thousands of formatted paragraphs or a folder of long DOCX files puts more load on a Windows Forms application than a one-page letter. TX Text Control handles all of them with the same API; what changes is how your code builds, loads and saves them. The measures below keep the editor responsive and the processing short, each with the measurement Text Control published for it and the conditions it was measured under.

### Run the Application as 64-Bit

The native libraries of TX Text Control exist in a 32-bit and a 64-bit version, and your application loads the set that matches its process. A 64-bit application has more memory available, so it can open and save much larger documents than a 32-bit one. [32-bit and 64-bit Applications](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/installation/requirements/32-bit-64-bit/llms-full.txt) shows how to choose the build configuration.

### Build Large Content in One Step

Code that inserts a piece of text, selects it and then formats it does three things for every piece, and in the editor each of them is a step in the undo buffer. Two techniques avoid this:

- **Selection objects.** A new [Selection](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/llms-full.txt) object gets its text and formatting first and is then assigned to the [Selection](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/selection-property/llms-full.txt) property, which inserts formatted text in one operation. In the editor, this is one undo step and shows no selection on the screen.
- **A ServerTextControl for the whole part.** Content built in a `ServerTextControl` has no undo history and no screen to update. Loaded into the editor with [Selection.Load](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/load-method/llms-full.txt), it arrives as one undo step.

The following handler in *Form1.cs* combines both: it builds 200 sections in a `ServerTextControl` and inserts them at the input position of `textControl1`:

```csharp
using System;
using TXTextControl;

private void insertSectionsButton_Click(object sender, EventArgs e)
{
    byte[] part;

    using (ServerTextControl tx = new ServerTextControl())
    {
        tx.Create();

        for (int i = 1; i <= 200; i++)
        {
            // Text and formatting are inserted in one assignment
            tx.Selection = new Selection
            {
                Text = $"Section {i}\r\n",
                FontName = "Arial",
                FontSize = 280, // 14 points, in twips
                Bold = true
            };

            tx.Selection = new Selection
            {
                Text = "The terms of this section apply to all orders.\r\n",
                FontName = "Arial",
                FontSize = 220
            };
        }

        tx.Save(out part, BinaryStreamType.InternalUnicodeFormat);
    }

    // Replaces the current selection of the editor: one undo step
    textControl1.Selection.Load(part, BinaryStreamType.InternalUnicodeFormat);
}
```

The code works as follows:

- Each assignment of a new `Selection` inserts its [Text](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/text-property/llms-full.txt) with its [FontName](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/fontname-property/llms-full.txt), [FontSize](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/fontsize-property/llms-full.txt) and [Bold](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/bold-property/llms-full.txt) at the input position of the `ServerTextControl`. Font sizes are in twips, 20 per point.
- [Save](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/save-method/llms-full.txt) writes the finished part into a byte array in the TX Text Control format, which keeps all formatting.
- `Selection.Load` of the editor exchanges the current selection with the loaded part. The user undoes the whole insertion with one Ctrl+Z.

In Text Control's measurements, Selection objects were about twice as fast as inserting, selecting and formatting: documents with 10 sections and 10 to 100 paragraphs per section took 5.7 to 42.9 ms each with Selection objects and 10.6 to 88.9 ms the other way. They were measured with `ServerTextControl` of TX Text Control .NET Server and its Core engine, after a warm-up, on hardware the article doesn't name. The blog article [Speed Up Document Generation by Factor 2 Using Selection Objects in .NET C#](https://www.textcontrol.com/blog/2026/07/17/speed-up-document-generation-selection-objects-dotnet-csharp/) contains the benchmark.

When code builds the document directly in the editor, switch the undo buffer off while it runs, as [Switch the Buffer Off](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/editing-text/undo-redo/#switch-the-buffer-off) shows. According to the reference, this saves time and memory.

### Replace Text in Many Large Files

`ServerTextControl` works on long documents with the same members as on short ones. In a test of Text Control on an eight-core Ryzen 7 processor, loading an 80-page DOCX file with about 30,000 words, replacing five placeholders and saving it took 0.46 seconds. Repeated with one `ServerTextControl` that loaded and saved one file after another, 400 such documents, 32,000 pages in total, took 73.32 seconds, 0.18 seconds per document. The blog article [High-Performance Text Replacement in Large DOCX Files using C# .NET](https://www.textcontrol.com/blog/2025/07/30/high-performance-text-replacement-in-large-docx-files-using-csharp-dotnet/) doesn't name the product or the engine.

The following handler replaces a placeholder in all DOCX files of the folder *Documents* on a background thread, with one `ServerTextControl` for all files:

```csharp
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using TXTextControl;

private async void replaceButton_Click(object sender, EventArgs e)
{
    string searchText = "%%company_name%%";
    string replacementText = "Text Control, LLC";

    string[] files = Directory.GetFiles("Documents", "*.docx");
    Directory.CreateDirectory("Output");

    await Task.Run(() =>
    {
        using (ServerTextControl tx = new ServerTextControl())
        {
            tx.Create();

            foreach (string file in files)
            {
                tx.Load(file, StreamType.WordprocessingML);

                int start = 0;
                int position;

                while ((position = tx.Find(searchText, start, FindOptions.MatchCase)) != -1)
                {
                    tx.Select(position, searchText.Length);
                    tx.Selection.Text = replacementText;

                    // Continue behind the inserted text
                    start = position + replacementText.Length;
                }

                tx.Save(Path.Combine("Output", Path.GetFileName(file)), StreamType.WordprocessingML);
            }
        }
    });

    MessageBox.Show(this, $"{files.Length} documents processed.");
}
```

The code works as follows:

- [Load](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/load-method/llms-full.txt) replaces the previous document of the instance, so one `ServerTextControl` serves all files.
- [Find](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/find-method/llms-full.txt) returns the position of the next occurrence from `start` on, or -1 when there is none. [Select](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/select-method/llms-full.txt) selects it, and setting `Selection.Text` replaces it.
- `Task.Run` keeps the form responsive while the files are processed. [UI Threading and Background Operations](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/performance/threading/llms-full.txt) adds progress and cancellation.

[Search and Replace](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/editing-text/search-replace/llms-full.txt) describes the search options.

### Preview Large PDF Files

Loading a PDF file rebuilds a document from its pages. When the user only needs to see what a PDF contains, [PDFImportSettings](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/pdfimportsettings/enumeration/llms-full.txt) `GenerateTextFramesFirstPageOnly` loads its first page only, which the reference suggests for previews. `GenerateLines` imports text only and discards images and paragraph formatting, for searching. [PDF Import and Its Limitations](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/pdf/import/#choose-how-the-document-is-rebuilt) shows how to set the value.

### Keep PDF Output Small

Images can take up most of a PDF file. [SaveSettings.ImageCompressionQuality](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/savesettings/imagecompressionquality-property/llms-full.txt) compresses them when a document is saved as PDF, and [ImageMaxResolution](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/savesettings/imagemaxresolution-property/llms-full.txt) reduces images with a higher resolution. In a test of Text Control with a 10-page document and two images of 2400 × 1350 pixels, the PDF took 14.68 MB with a quality of 100, which is lossless, and 1.48 MB with a quality of 70. [Reduce the Size of PDF Files](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/saving-converting/#reduce-the-size-of-pdf-files) shows the code, and the blog article [Best Practices for Image Compression when Exporting to PDF in .NET C#](https://www.textcontrol.com/blog/2025/08/22/best-practices-for-image-compression-when-exporting-to-pdf-in-dotnet-csharp/) the measurements.

### Next Steps

- [Mail Merge Performance](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/performance/mail-merge/llms-full.txt) — merge results with many pages and long merge blocks.
- [Memory Usage and Resource Disposal](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/performance/memory-usage/llms-full.txt) — what documents and instances hold in memory.
- [Document Loading and Conversion](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/performance/loading-conversion/llms-full.txt) — load and convert many documents efficiently.
- [Undo and Redo](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/editing-text/undo-redo/llms-full.txt) — the undo buffer of the editor.

---

## Nearby

- **Previous:** [UI Threading and Background Operations](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/performance/threading/llms-full.txt)
- **Next:** [Memory Usage and Resource Disposal](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/performance/memory-usage/llms-full.txt)
