# Creating Documents

> Create documents in a Windows Forms app: reset the editor, set page size and margins, insert Selection objects, append files, set properties and turn off undo.

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

---

A document your application creates starts empty, in the editor or in a `ServerTextControl`, and grows from text, snippets and other documents. The button handlers below start a new document in the `TextControl` of a form, set its page format, assemble it from pieces, combine several files, store its properties and keep the undo history from slowing down large documents.

[Create a Document Programmatically](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-processing/create/llms-full.txt) is the first tutorial on the subject: it inserts formatted text, a table, an image and a header. How the parts of a document are organized is described in [Document Object Model](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/object-model/llms-full.txt). The examples assume a `TextControl` named `textControl1` on `Form1` and these `using` directives in *Form1.cs*:

```csharp
using System;
using System.Windows.Forms;
using TXTextControl;
```

### Start a New Document

In the editor, a new document means removing the current one. [ResetContents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/resetcontents-method/llms-full.txt) deletes the entire contents of the `TextControl`. The page format is a setting of the control, so reset it as well when the new document should start with the defaults:

```csharp
private void newButton_Click(object sender, EventArgs e)
{
    // Remove the contents and start with the default page format
    textControl1.ResetContents();
    textControl1.ResetPageSize();
    textControl1.ResetPageMargins();
}
```

[ResetPageSize](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/resetpagesize-method/llms-full.txt) sets US letter pages (8.5 x 11 inches), and [ResetPageMargins](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/resetpagemargins-method/llms-full.txt) sets margins of 1 inch. Asking the user before unsaved changes are lost is shown in [Load and Save Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-editor/load-save/llms-full.txt).

A [ServerTextControl](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/llms-full.txt) starts with an empty document. [Create](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/create-method/llms-full.txt) must be called before any other member and returns `false` if the instance couldn't be initialized; a `using` statement disposes it 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) describes its lifecycle.

### Set the Page Format

[PageSize](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/pagesize-property/llms-full.txt) and [PageMargins](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/pagemargins-property/llms-full.txt) are measured in the unit of `PageUnit`, by default 1/100 inch. The following handler shows the current format and then switches to narrow margins and landscape pages:

```csharp
private void pageFormatButton_Click(object sender, EventArgs e)
{
    // Values in 1/100 inch, the default PageUnit
    MessageBox.Show($"Page size: {textControl1.PageSize.Width} x {textControl1.PageSize.Height}\r\n" +
        $"Margins: {textControl1.PageMargins.Left}, {textControl1.PageMargins.Top}, " +
        $"{textControl1.PageMargins.Right}, {textControl1.PageMargins.Bottom}");

    // Margins of 0.5 inch on every side and landscape orientation
    textControl1.PageMargins = new PageMargins(50, 50, 50, 50);
    textControl1.Landscape = true;
}
```

The [PageMargins](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/pagemargins/constructor/llms-full.txt) constructor takes the left, top, right and bottom margin. [Landscape](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/landscape-property/llms-full.txt) turns the pages. The same properties exist on `ServerTextControl`. When parts of a document need their own page format, such as one landscape page in a portrait report, divide it into sections, as [Sections, Pages, Headers, and Footers](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/sections-headers/llms-full.txt) describes.

### Build a Document From Pieces

The following handler writes a heading and a paragraph with a placeholder and appends an HTML snippet:

```csharp
private void buildButton_Click(object sender, EventArgs e)
{
    textControl1.ResetContents();

    // Prepare text and formatting in a Selection object, then insert it in one step
    textControl1.Selection = new Selection()
    {
        Text = "Invoice INV-2025-001\r\n",
        FontName = "Arial",
        FontSize = 480, // 24 pt
        Bold = true
    };

    textControl1.Selection = new Selection()
    {
        Text = "Terms: [TERMS]\r\n",
        FontName = "Arial",
        FontSize = 240 // 12 pt
    };

    // Append an HTML snippet in a new paragraph at the end of the document
    textControl1.Append("<p>Thank you for your <strong>order</strong>.</p>",
        StringStreamType.HTMLFormat,
        AppendSettings.StartWithNewParagraph);
}
```

The code works as follows:

- Assigning a new [Selection](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/llms-full.txt) object inserts its text with all its attributes at the input position in one step, instead of inserting the text and formatting it afterward. 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/) measures the difference with `ServerTextControl`.
- [Append](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/append-method/llms-full.txt) adds a document from a file, a byte array or a string to the end of the existing one. `StringStreamType.HTMLFormat` reads the string as HTML.

To replace the placeholder with the contents of another document, select it and call `Selection.Load`, as [Load, Append or Insert](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/loading/#load-append-or-insert) shows; the same section lists the `AppendSettings` values.

### Combine Several Documents

A cover page and several chapters become one document when you load the first file and append the others. A `ServerTextControl` does this without showing the intermediate steps, and the editor receives the finished result:

```csharp
private void combineButton_Click(object sender, EventArgs e)
{
    string[] files = { "cover.docx", "chapter1.docx", "chapter2.docx" };
    byte[] document;

    using (ServerTextControl tx = new ServerTextControl())
    {
        // Initialize the new instance before using it
        if (!tx.Create())
        {
            MessageBox.Show("ServerTextControl could not be initialized.");
            return;
        }

        // The first document replaces the empty contents
        tx.Load(files[0], StreamType.WordprocessingML);

        // Every further document is appended in a new section
        for (int i = 1; i < files.Length; i++)
        {
            tx.Append(files[i], StreamType.WordprocessingML, AppendSettings.StartWithNewSection);
        }

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

    // Show the combined document in the editor
    textControl1.Load(document, BinaryStreamType.InternalUnicodeFormat);
    MessageBox.Show($"Sections: {textControl1.Sections.Count}, pages: {textControl1.Pages}");
}
```

The code works as follows:

- [ServerTextControl.Load](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/load-method/llms-full.txt) replaces the empty contents with the first file, and [ServerTextControl.Append](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/append-method/llms-full.txt) adds each further file in a section of its own, which keeps its page format.
- [ServerTextControl.Save](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/save-method/llms-full.txt) writes the result to a byte array in the TX Text Control format, and [TextControl.Load](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/load-method/llms-full.txt) shows it in the editor.

Documents from a database are appended from a byte array in the same way, for example `tx.Append(bytes, BinaryStreamType.WordprocessingML, AppendSettings.StartWithNewSection);`. [Loading Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/loading/llms-full.txt) compares `Load`, `Append` and `Selection.Load`.

### Store Document Properties

[DocumentSettings](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/documentsettings-property/llms-full.txt) holds the title, author and other general information of the document, which is stored when the document is saved. [Set the Properties in Code](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/properties/#set-the-properties-in-code) sets them, and [Override Values When Saving](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/properties/#override-values-when-saving) replaces them with `SaveSettings` for one save.

### Build Large Documents Without Undo

The editor records each change so that the user can undo it. When your code inserts hundreds of paragraphs, that history costs time and memory: set [AllowUndo](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/allowundo-property/llms-full.txt) to `false` while the code runs, preferably in a `try` block whose `finally` sets it back to `true`, and empty the buffer with [ClearUndo](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/clearundo-method/llms-full.txt) afterward if the user shouldn't undo the document step by step. [Undo and Redo](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/editing-text/undo-redo/#switch-the-buffer-off) shows both. A `ServerTextControl` keeps no undo history at all.

### Check the Fonts of a Document

TX Text Control formats text with the fonts available for the formatting device, the printer that [FormattingPrinter](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/formattingprinter-property/llms-full.txt) names. [GetSupportedFonts](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/getsupportedfonts-method/llms-full.txt) returns their names:

```csharp
private void fontsButton_Click(object sender, EventArgs e)
{
    string[] fonts = textControl1.GetSupportedFonts();
    MessageBox.Show($"{fonts.Length} fonts available");
}
```

When a document uses a font that isn't supported, TX Text Control replaces it and, with [FontSettings.AdaptFontEvent](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/fontsettings/adaptfontevent-property/llms-full.txt) set to `true`, raises the [AdaptFont](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/adaptfont-event/llms-full.txt) event. [Choose the Replacement Fonts](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/pdf/fonts/#choose-the-replacement-fonts) shows how to find out which fonts were replaced and how to choose the replacements. The syntax block of the reference spells the property `AdaptFontsEvent`, but the compiled name is `AdaptFontEvent`. [Fonts](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/troubleshooting/fonts/llms-full.txt) in the Troubleshooting section helps when text looks different than expected.

### Next Steps

- [Loading Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/loading/llms-full.txt): load existing documents with all their options.
- [Text, Paragraphs, and Stylesheets](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/text-paragraphs/llms-full.txt): format the text of the new document.
- [Saving and Converting Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/saving-converting/llms-full.txt): save the finished document in the format you need.
- [Sections, Pages, Headers, and Footers](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/sections-headers/llms-full.txt): give parts of a document their own page format.

---

## Nearby

- **Previous:** [Document Object Model](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/object-model/llms-full.txt)
- **Next:** [Loading Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/loading/llms-full.txt)
