# Create a Document Programmatically

> Build a report in a WPF click handler with ServerTextControl: formatted text, a table with a header row, an image and a header, saved as DOCX and then shown.

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

---

A WPF application often produces documents that nobody types: a report built from figures, a confirmation or a cover letter. `ServerTextControl` builds such a document in code with the same building blocks a user has in the editor, formatted paragraphs, a table, an image and a page header, without touching the document in the editor. In a few steps, a button of your window creates a short quarterly report, writes it to a DOCX file and opens it in the `TextControl` of the window.

### Prerequisites

- The application from [Create Your First WPF Application](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-editor/first-application/llms-full.txt), with a `TextControl` named `textControl1` in *MainWindow.xaml*.
- An image file named *logo.png* in the project. In its properties, set **Build Action** to **Content** and **Copy to Output Directory** to **Copy if newer**, so that the file is next to the application when it runs.

Add a button to the `DockPanel` of *MainWindow.xaml*, before the `TextControl` element, so that it docks above the editor:

```xml
<Button x:Name="createButton" DockPanel.Dock="Top" Content="Create Report"
        Click="createButton_Click" />
```

### How Code Builds a Document

A [ServerTextControl](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/llms-full.txt) holds a document in memory and has no user interface. Your code writes into it at the current input position, the way typing does: its [Selection](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/selection-property/llms-full.txt) sets the formatting of the next text and inserts it, and tables and images are inserted at the same position. Every insertion moves the input position behind the new content, so the document grows in the order in which the code runs.

The component itself is described in [ServerTextControl](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/overview/components/servertextcontrol/llms-full.txt) in the Overview.

### Build the Document

Replace the content of *MainWindow.xaml.cs* with the following code, keeping the namespace of your project:

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

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

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

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

            // Title: 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, not bold, 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 = "Sales by region for the second quarter.\r\n";

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

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

            foreach (TXTextControl.TableCell cell in table.Cells)
            {
                cell.Text = $"R{cell.Row} C{cell.Column}";
            }

            // Text and a logo below the table
            tx.Selection.Text = "Source: sales department.\r\n";

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

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

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

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

### What the Code Does

- **Initializing.** [Create](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/create-method/llms-full.txt) has to be called before any other member of the `ServerTextControl`. The `using` statement releases the instance as soon as the report is saved.
- **Formatting text.** [FontName](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/selection/fontname-property/llms-full.txt), [FontSize](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/selection/fontsize-property/llms-full.txt) and [Bold](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/selection/bold-property/llms-full.txt) set the character formatting, and [ParagraphFormat](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/selection/paragraphformat-property/llms-full.txt) with its [Alignment](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/paragraphformat/alignment-property/llms-full.txt) the paragraph formatting. Both apply to the text that the next assignment to [Selection.Text](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/selection/text-property/llms-full.txt) inserts. Font sizes are in twips, 20 twips per point, and `\r\n` ends a paragraph.
- **Qualified names.** WPF has types of the same name: `HorizontalAlignment` in `System.Windows` and `Image` in `System.Windows.Controls`. Write `TXTextControl.HorizontalAlignment` and `TXTextControl.Image` in a window's code so that the compiler takes the TX Text Control types.
- **The table.** [Tables.Add](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/tablecollection/add-method/llms-full.txt) inserts a table with an ID that you choose, and [GetItem](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/tablecollection/getitem-method/llms-full.txt) returns it by that ID. Rows count from 1. [IsHeader](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/tablerow/isheader-property/llms-full.txt) turns the first row into a header row that repeats on each page, [CellFormat](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/tablerow/cellformat-property/llms-full.txt) with [BackColor](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/tablecellformat/backcolor-property/llms-full.txt) shades it, and [TableCell.Text](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/tablecell/text-property/llms-full.txt) fills the cells. The colors of TX Text Control are `System.Drawing.Color` values, not WPF brushes. After the table is inserted, the input position is below it.
- **The image.** The [Image](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/image/llms-full.txt) constructor takes a file name and the index of the image format, 4 for PNG. [Images.Add](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/imagecollection/add-method/llms-full.txt) with the position -1 places the image at the input position, inline with the text.
- **The header.** [HeadersAndFooters.Add](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfootercollection/add-method/llms-full.txt) creates the header, [GetItem](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfootercollection/getitem-method/llms-full.txt) returns it, and the [Selection](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfooter/selection-property/llms-full.txt) of the [HeaderFooter](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfooter/llms-full.txt) writes its text.
- **Saving and showing.** [Save](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/save-method/llms-full.txt) writes *report.docx* into the output folder, and a second call copies the document into a byte array in the TX Text Control format. [TextControl.Load](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/load-method/llms-full.txt) opens that byte array in the editor.

### Run the Application

Press **F5** and click **Create Report**. The editor shows the centered title, the paragraph, the table with its shaded header row, the line below it and the logo. *report.docx* in the output folder, *bin\\Debug\\net8.0-windows* for a .NET 8 project, contains the same document and opens in Microsoft Word.

The header "Sample Company Inc." is part of the document, but the editor shows headers and footers only when its [ViewMode](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/viewmode-property/llms-full.txt) is `PageView`. Set the attribute `ViewMode="PageView"` on the `TextControl` element in XAML to see it.

### Build the Document in the Editor Instead

The `TextControl` of the window has the same `Selection`, `Tables`, `Images` and `HeadersAndFooters` members, so the calls also work on the document the user sees:

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

Call such code after WPF has loaded the `TextControl`, for example in its `Loaded` event or in a button handler, not in the constructor of the window. [Control Initialization](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/troubleshooting/initialization/llms-full.txt) explains why. A `ServerTextControl` remains the better choice for documents that are not meant for editing: it builds them without touching the document the user works on.

### Next Steps

- [Save as DOCX and PDF](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-processing/save-docx-pdf/llms-full.txt): save and convert documents in other formats.
- [Print a Document](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-processing/print/llms-full.txt): print the report with or without a dialog.
- [Creating Documents](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/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-wpf/getting-started/mail-merge/template/llms-full.txt): fill a template with data instead of building each document in code.

---

## Nearby

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