Large Documents
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 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 object gets its text and formatting first and is then assigned to the Selection 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
ServerTextControlhas no undo history and no screen to update. Loaded into the editor with Selection.Load, 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:
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
Selectioninserts its Text with its FontName, FontSize and Bold at the input position of theServerTextControl. Font sizes are in twips, 20 per point. - Save writes the finished part into a byte array in the TX Text Control format, which keeps all formatting.
Selection.Loadof 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# 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 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 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:
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 replaces the previous document of the instance, so one
ServerTextControlserves all files. - Find returns the position of the next occurrence from
starton, or -1 when there is none. Select selects it, and settingSelection.Textreplaces it. Task.Runkeeps the form responsive while the files are processed. UI Threading and Background Operations adds progress and cancellation.
Search and Replace 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 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 shows how to set the value.
Keep PDF Output Small
Images can take up most of a PDF file. SaveSettings.ImageCompressionQuality compresses them when a document is saved as PDF, and ImageMaxResolution 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 shows the code, and the blog article Best Practices for Image Compression when Exporting to PDF in .NET C# the measurements.
Next Steps
- Mail Merge Performance — merge results with many pages and long merge blocks.
- Memory Usage and Resource Disposal — what documents and instances hold in memory.
- Document Loading and Conversion — load and convert many documents efficiently.
- Undo and Redo — the undo buffer of the editor.