# Create a Document Programmatically

> Build a report in Windows Forms with ServerTextControl and Selection: fonts, a table with a repeating header row, a PNG logo and a page header, saved as DOCX.

- **Product:** TX Text Control .NET for Windows Forms
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for Windows Forms > Getting Started > Document Processing > Create a Document Programmatically
- **3063 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-processing/create/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-processing/create/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-processing/create/llms-full.txt

---

Reports, letters and confirmations are often built by the application rather than typed by a user. With `ServerTextControl`, your code writes a document the same way a user would in the editor, from formatted headings and body text to tables, images and headers, and then saves it or hands it to the editor. The steps below build a short report in a button handler, save it as DOCX and show it in the `TextControl` of your form.

### Prerequisites

- The application from [Create Your First Windows Forms Application](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-editor/first-application/llms-full.txt), with a `TextControl` named `textControl1` on `Form1`.
- A `Button` named `createButton` on the form. Double-click it in the designer to create its `Click` handler.
- An image named *logo.png* in the project, with its **Copy to Output Directory** property set to **Copy always**, so that it is next to the application when it runs.

### How Code Builds a Document

A [ServerTextControl](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/llms-full.txt) holds a document without showing it. Like a user in the editor, your code works at the current input position: [Selection](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/selection-property/llms-full.txt) sets the formatting and inserts text there, and new tables and images are added there as well. The input position moves on with each insertion, so the document grows from top to bottom in the order of your code.

[ServerTextControl](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/overview/components/servertextcontrol/llms-full.txt) in the Overview explains the component.

### Build the Document

Add the following code to *Form1.cs*:

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

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void createButton_Click(object sender, EventArgs e)
    {
        byte[] document;

        using (ServerTextControl tx = new ServerTextControl())
        {
            // Initialize the new instance before using it
            tx.Create();

            // Heading: Arial, 24 pt, bold, centered
            tx.Selection.FontName = "Arial";
            tx.Selection.FontSize = 480; // 480 twips = 24 pt
            tx.Selection.Bold = true;
            tx.Selection.ParagraphFormat = new ParagraphFormat()
            {
                Alignment = TXTextControl.HorizontalAlignment.Center
            };
            tx.Selection.Text = "Quarterly Report\r\n";

            // Body text: 12 pt, regular, left-aligned
            tx.Selection.FontSize = 240; // 240 twips = 12 pt
            tx.Selection.Bold = false;
            tx.Selection.ParagraphFormat = new ParagraphFormat()
            {
                Alignment = TXTextControl.HorizontalAlignment.Left
            };
            tx.Selection.Text = "The following table lists the results of the quarter.\r\n";

            // Table with 4 rows, 3 columns and the ID 10
            tx.Tables.Add(4, 3, 10);
            Table table = tx.Tables.GetItem(10);

            TableRow headerRow = table.Rows.GetItem(1);
            headerRow.IsHeader = true;
            headerRow.CellFormat = new TableCellFormat()
            {
                BackColor = System.Drawing.Color.LightGray
            };

            foreach (TableCell cell in table.Cells)
            {
                cell.Text = "Row " + cell.Row.ToString() + ", Column " + cell.Column.ToString();
            }

            // A paragraph and an image below the table
            tx.Selection.Text = "Prepared by the sales department.\r\n";

            TXTextControl.Image logo = new TXTextControl.Image("logo.png", 4); // 4 = PNG
            tx.Images.Add(logo, -1); // -1 = current input position

            // Header on every page
            tx.HeadersAndFooters.Add(HeaderFooterType.Header);
            HeaderFooter header = tx.HeadersAndFooters.GetItem(HeaderFooterType.Header);
            header.Selection.Text = "Sample Company Inc.";

            // Save as DOCX, and as a byte array for the editor
            tx.Save("report.docx", StreamType.WordprocessingML);
            tx.Save(out document, BinaryStreamType.InternalUnicodeFormat);
        }

        // Show the document in the editor
        textControl1.Load(document, BinaryStreamType.InternalUnicodeFormat);
    }
}
```

Keep the `namespace` that Visual Studio created around the class.

### What the Code Does

- **Create.** [Create](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/create-method/llms-full.txt) initializes the `ServerTextControl`. It must be called before any other member. The `using` statement disposes the instance when the document is done.
- **Text and formatting.** [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) set the character formatting, and [ParagraphFormat](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/paragraphformat-property/llms-full.txt) with its [Alignment](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/paragraphformat/alignment-property/llms-full.txt) the paragraph formatting of the text that [Selection.Text](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/text-property/llms-full.txt) inserts next. Font sizes are given in twips, 20 twips per point. `\r\n` ends a paragraph.
- **Qualified names.** `HorizontalAlignment` and `Image` exist in Windows Forms and in TX Text Control. In a form, write `TXTextControl.HorizontalAlignment` and `TXTextControl.Image`.
- **The table.** [Tables.Add](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/tablecollection/add-method/llms-full.txt) inserts a table at the input position with an ID of your choice, and `Tables.GetItem` with that ID returns it. Rows are numbered from 1. [IsHeader](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/tablerow/isheader-property/llms-full.txt) makes the first row repeat at the top of each page, and [CellFormat](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/tablerow/cellformat-property/llms-full.txt) gives it a background color. [TableCell.Text](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/tablecell/text-property/llms-full.txt) fills each cell. After the table is added, the input position is below it.
- **The image.** The [Image](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/image/llms-full.txt) constructor takes the file name and the format, where 4 is PNG. [Images.Add](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/imagecollection/add-method/llms-full.txt) with -1 inserts it at the input position, like a character in the text.
- **The header.** [HeadersAndFooters.Add](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/headerfootercollection/add-method/llms-full.txt) adds a header to the document, and the [Selection](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/headerfooter/selection-property/llms-full.txt) of the [HeaderFooter](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/headerfooter/llms-full.txt) inserts its text.
- **Saving.** [Save](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/save-method/llms-full.txt) writes *report.docx* to the output folder, and a second time to a byte array in the TX Text Control format. [TextControl.Load](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/load-method/llms-full.txt) shows that byte array in the editor.

### Run the Application

Press **F5** and click **createButton**. The editor shows the report: the centered heading, the text, the table with its gray header row, the closing line and the image. In page view, the header "Sample Company Inc." appears at the top of the page. The file *report.docx* in the output folder contains the same document.

### Build the Document in the Editor Instead

The same calls work on the `TextControl` itself, where the user sees the document grow:

```csharp
textControl1.Selection.FontSize = 480;
textControl1.Selection.Text = "Quarterly Report\r\n";
textControl1.Tables.Add(4, 3, 10);
textControl1.HeadersAndFooters.Add(HeaderFooterType.Header);
```

A `ServerTextControl` keeps the editor free while the document is built and can run where no form exists. Headers and footers appear in the editor only in page view.

### Next Steps

- [Save as DOCX and PDF](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-processing/save-docx-pdf/llms-full.txt): save and convert the document in other formats.
- [Print a Document](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-processing/print/llms-full.txt): print the document with or without the editor.
- [Creating Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/creating/llms-full.txt): text, tables, images, sections and fields in code in detail.
- [Create a Template](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/mail-merge/template/llms-full.txt): fill documents with data instead of building them in code.

---

## Nearby

- **Previous:** [Document Processing](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-processing/llms-full.txt)
- **Next:** [Save as DOCX and PDF](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-processing/save-docx-pdf/llms-full.txt)
