# Document Object Model

> How TextControl and ServerTextControl organize a document: text parts, IFormattedText, frames, sections, the input focus rule, positions and twips.

- **Product:** TX Text Control .NET for Windows Forms
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for Windows Forms > Document Processing > Document Object Model
- **3708 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/object-model/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/object-model/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/object-model/llms-full.txt

---

Code that changes a document has to know where things are: which object holds the tables of a header, why `Tables` sometimes returns the tables of a text frame, and whether a position counts from 0 or from 1. TX Text Control .NET for Windows Forms organizes every document as a tree of objects that is the same in the `TextControl` on your form and in a `ServerTextControl`. Read on for its parts, the rules for positions and units, and the one difference between the two classes that surprises most developers.

### Two Classes, One Document Model

A document lives in one of two classes:

- **[TextControl](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/llms-full.txt)** is the editor on your form. Your code and the user work on the same document, and every change is visible at once. [TextControl](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/overview/components/textcontrol/llms-full.txt) in the Overview introduces it.
- **[ServerTextControl](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/llms-full.txt)** holds a document without a user interface. It is created in code, initialized with `Create` and disposed when the work is done. [Using ServerTextControl in Desktop Applications](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/servertextcontrol/llms-full.txt) covers it in detail.

Both classes give you the same collections, the same `Selection` and the same `Load` and `Save` methods. Code that builds a table or formats a paragraph works with either one, so the pages of this section show most examples on `textControl1`, the `TextControl` of your form, and use a `ServerTextControl` where the user shouldn't see the work.

### What a Document Consists of

A document is made of *text parts*: the main text, and the text frames, headers, footers and footnote sections beside it. Each text part has its own paragraphs, tables, images and fields. Sections and pages belong to the document as a whole.

```text
TextControl / ServerTextControl
│
├── TextParts ──────────── MainText            the body of the document
│                          TextFrame ...       rectangles of text placed on a page
│                          HeaderFooter ...    headers and footers of the sections
│                          FootnoteSection ... footnotes
│
│   Each text part (IFormattedText) has:
│   ├── Selection          the selected text, or the input position
│   ├── Paragraphs, Lines, TextChars
│   ├── Tables, Images, Frames, Barcodes, Drawings
│   ├── ApplicationFields, TextFields, FormFields, SubTextParts
│   └── Comments, TrackedChanges
│
├── Sections ───────────── page size, margins, headers and footers of each section
├── ParagraphStyles, InlineStyles
├── DocumentSettings        title, author and other properties
└── Charts                  Windows Forms only
```

[TextParts](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/textparts-property/llms-full.txt) returns a [TextPartCollection](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textpartcollection/llms-full.txt) with the main text first, then the text frames and finally the headers and footers, each group in the order of their position in the document. Every object in it implements [IFormattedText](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/iformattedtext/llms-full.txt), the interface of everything a text part has. `TextControl`, `ServerTextControl`, [MainText](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/maintext/llms-full.txt), [TextFrame](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textframe/llms-full.txt), [HeaderFooter](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/headerfooter/llms-full.txt) and [FootnoteSection](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/footnotesection/llms-full.txt) implement it.

#### Frames

Images, text frames, charts, barcodes and drawings are *frames*: objects that are positioned in the text like a character, anchored to a paragraph or fixed on a page. They derive from [FrameBase](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/framebase/llms-full.txt), which provides the name, ID, size, location and text distances. Each kind has its own collection, and [Frames](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/frames-property/llms-full.txt) returns a [FrameCollection](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/framecollection/llms-full.txt) with all of them. [Images, Text Frames, and Shapes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/images-shapes/llms-full.txt) and [Charts and Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/charts-barcodes/llms-full.txt) show how to add them.

### The Class Hierarchy

The graphic of Text Control's class hierarchy shows every class of the library with the class it derives from, in groups such as Windows Forms Components, WPF Components, Server Components, Document Formatting, Programmable Objects, Text Fields, Tables and Drawings, Collections, DocumentServer and Exceptions. Its colors mark the classes that exist only for Windows Forms, only for WPF or only for ASP.NET, and the classes new in version 34.0:

![The class hierarchy of the TX Text Control .NET class library, in columns from Windows Forms Components to Exceptions, with Windows Forms only classes in cyan, WPF only classes in green, ASP.NET only classes in yellow and a class new in version 34.0 in red.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/class-hierarchy.2cdfa3b6.webp)

`TextControl` derives from `System.Windows.Forms.Control` and `ServerTextControl` from `System.ComponentModel.Component`. The classes of a document, such as `Selection`, `Paragraph`, `Section` and `Table`, derive from `System.Object` and are shared by all products; the WPF and ASP.NET classes of the graphic aren't part of TX Text Control .NET for Windows Forms.

### The Input Focus Decides What a Collection Contains

In a `TextControl`, the user can click into a header or a text frame. The text part with the *input focus* is the one the cursor is in, and many properties of `TextControl` refer to that text part, not to the main text:

| Property of `TextControl` | Refers to |
|---|---|
| [Selection](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/selection-property/llms-full.txt), [Paragraphs](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/paragraphs-property/llms-full.txt), [Tables](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/tables-property/llms-full.txt), [Images](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/images-property/llms-full.txt), [TextFrames](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/textframes-property/llms-full.txt), [Frames](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/frames-property/llms-full.txt), [ApplicationFields](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/applicationfields-property/llms-full.txt), [Comments](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/comments-property/llms-full.txt), [TrackedChanges](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/trackedchanges-property/llms-full.txt) | The text part with the input focus: the main text, a text frame, a header or a footer |
| [Sections](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/sections-property/llms-full.txt), [FormFields](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/formfields-property/llms-full.txt), [TextParts](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/textparts-property/llms-full.txt) | The whole document |

A `ServerTextControl` has no user and no input focus: its [Selection](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/selection-property/llms-full.txt) always refers to the main text.

When your code runs from a button while the cursor is in a header, `textControl1.Tables.Add` inserts the table into that header. To work on the main text whatever the user has clicked, get it with [GetMainText](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textpartcollection/getmaintext-method/llms-full.txt). The collections of a `MainText` object don't depend on the input focus:

```csharp
private void mainTextButton_Click(object sender, EventArgs e)
{
    // Depends on the input focus: main text, text frame, header or footer
    int tablesWithFocus = textControl1.Tables.Count;

    // Always the main text
    MainText mainText = textControl1.TextParts.GetMainText();
    int tablesInMainText = mainText.Tables.Count;

    MessageBox.Show($"Text part with the input focus: {tablesWithFocus} tables\r\n" +
                    $"Main text: {tablesInMainText} tables");
}
```

[TextPartCollection.Activate](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textpartcollection/activate-method/llms-full.txt) moves the input focus to a text part, for example back to the main text with `textControl1.TextParts.Activate(textControl1.TextParts.GetMainText())`, and scrolls the document to it.

### Positions and Units

- **Text positions start at 0.** [Selection.Start](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/start-property/llms-full.txt) is the position of the first selected character, and 0 is the first position of the text. [Selection.Length](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/length-property/llms-full.txt) is the number of selected characters; with a length of 0, `Start` is the input position.
- **Elements are numbered from 1.** The first paragraph is `Paragraphs[1]`, the first table row is row 1, and so are sections, pages and lines. [Paragraph.Start](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraph/start-property/llms-full.txt) is one-based as well, so subtract 1 before you select from it.
- **Font sizes and distances are in twips**, 1/1440 inch, or 20 twips per point. A font size of 240 is 12 pt.
- **Page sizes and margins** are measured in the unit that [PageUnit](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/pageunit-property/llms-full.txt) sets, by default 1/100 inch.

### Example

The following handler lists the text parts of the document in the editor with their paragraphs, tables and images, and then the sections, pages and frames. *Form1.cs* needs `using System.Text;` and `using TXTextControl;`:

```csharp
private void structureButton_Click(object sender, EventArgs e)
{
    StringBuilder report = new StringBuilder();

    // The main text, text frames, footnote sections, headers and footers
    foreach (IFormattedText textPart in textControl1.TextParts)
    {
        report.AppendLine($"{textPart.GetType().Name}: {textPart.Paragraphs.Count} paragraphs, " +
            $"{textPart.Tables.Count} tables, {textPart.Images.Count} images");
    }

    report.AppendLine($"Sections: {textControl1.Sections.Count}, pages: {textControl1.Pages}");
    report.AppendLine($"All frames: {textControl1.Frames.Count}");

    MessageBox.Show(report.ToString());
}
```

What the code does:

- `textControl1.TextParts` returns the text parts of the whole document, whatever has the input focus. `GetType().Name` prints the class of each, such as `MainText` or `HeaderFooter`.
- [IFormattedText.Paragraphs](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/iformattedtext/paragraphs-property/llms-full.txt), [Tables](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/iformattedtext/tables-property/llms-full.txt) and [Images](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/iformattedtext/images-property/llms-full.txt) are the collections of that one text part.
- [Pages](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/pages-property/llms-full.txt) is the number of pages of the document.

Because every text part implements `IFormattedText`, one method can process them all. The following method takes the editor, a `ServerTextControl`, a header or a text frame alike:

```csharp
// Works with TextControl, ServerTextControl and every other text part
private static void CountElements(IFormattedText document)
{
    MessageBox.Show($"{document.Paragraphs.Count} paragraphs, {document.Tables.Count} tables, " +
                    $"{document.Images.Count} images, {document.Frames.Count} frames");
}
```

### Where Each Element Is Described

| Element | Collection | Page |
|---|---|---|
| Text, characters, paragraphs, styles | `Selection`, `Paragraphs`, `ParagraphStyles`, `InlineStyles` | [Text, Paragraphs, and Stylesheets](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/text-paragraphs/llms-full.txt) |
| Tables and formulas | `Tables` | [Tables and Calculations](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/tables/llms-full.txt) |
| Images, text frames, drawings | `Images`, `TextFrames`, `Drawings` | [Images, Text Frames, and Shapes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/images-shapes/llms-full.txt) |
| Charts and barcodes | `Charts`, `Barcodes` | [Charts and Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/charts-barcodes/llms-full.txt) |
| Sections, pages, headers and footers | `Sections`, `HeadersAndFooters` | [Sections, Pages, Headers, and Footers](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/sections-headers/llms-full.txt) |
| Fields and subtext parts | `ApplicationFields`, `TextFields`, `SubTextParts` | [Fields and Subtext Parts](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/fields/llms-full.txt) |
| Form fields | `FormFields` | [Forms and Conditional Instructions](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/form-fields/llms-full.txt) |
| Comments and tracked changes | `Comments`, `TrackedChanges` | [Comments and Tracked Changes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/comments-changes/llms-full.txt) |

### Key Members

| Member | Purpose |
|---|---|
| [TextParts](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/textparts-property/llms-full.txt) | All text parts of the document: main text, text frames, headers, footers and footnote sections. |
| [GetMainText](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textpartcollection/getmaintext-method/llms-full.txt) | The main text, independent of the input focus. |
| [Selection](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/selection-property/llms-full.txt) | The selected text or the input position in the text part with the input focus. |
| [InputPosition](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/inputposition-property/llms-full.txt) | The current input position, with its page, line and column. |
| [Frames](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/frames-property/llms-full.txt) | All images, text frames, charts, barcodes and drawings of the text part with the input focus. |
| [Sections](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/sections-property/llms-full.txt) | The sections of the document, with their page formats. |
| [IFormattedText](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/iformattedtext/llms-full.txt) | The interface shared by the controls and all text parts. |

### Next Steps

- [Creating Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/creating/llms-full.txt): start a new document and build it from pieces.
- [Text, Paragraphs, and Stylesheets](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/text-paragraphs/llms-full.txt): select, find and format text.
- [Using ServerTextControl in Desktop Applications](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/servertextcontrol/llms-full.txt): process documents without showing them.
- [Selections and Caret Position](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/editing-text/selections/llms-full.txt): the selection from the user's side.

---

## Nearby

- **Previous:** [Document Processing](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/llms-full.txt)
- **Next:** [Creating Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/creating/llms-full.txt)
