# Text, Paragraphs, and Stylesheets

> Format characters and paragraphs in the WPF TextControl, replace placeholders in headers and text frames, and define paragraph and inline styles and lists.

- **Product:** TX Text Control .NET for WPF
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for WPF > Document Processing > Text, Paragraphs, and Stylesheets
- **2310 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/text-paragraphs/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/text-paragraphs/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/text-paragraphs/llms-full.txt

---

The text of a document is where most processing code starts: it replaces placeholders, emphasizes amounts, lays out paragraphs and keeps all headings alike through styles. In a WPF window, this code works on the `Selection` and the `Paragraphs` of the `TextControl`, and on the paragraph and inline styles of the document. What follows shows each task as a `Click` handler, along with the two places where WPF names collide with those of TX Text Control.

The user's side of the same features, with the ribbon and the dialog boxes, is in [Text and Paragraph Formatting](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/formatting/llms-full.txt), [Stylesheets](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/stylesheets/llms-full.txt) and [Bullets and Numbered Lists](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/lists/llms-full.txt). *MainWindow.xaml.cs* needs these `using` directives:

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

The colors of a document are `System.Drawing.Color` values in WPF as well, not `System.Windows.Media.Color`.

### Select and Format Characters

[Text](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/text-property/llms-full.txt) gets or sets the whole text of the control. To format part of it, select the characters with [Select](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/select-method/llms-full.txt), which takes a zero-based start position and a number of characters, or assign a new `Selection` with a start and a length, then set the attributes of [Selection](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/selection-property/llms-full.txt), such as `Bold`, `FontSize` in twips or `ForeColor`. [Format Characters](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/formatting/#format-characters) in Text and Paragraph Formatting lists the attributes, and [Format a Range Your Code Inserted](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/formatting/#format-a-range-your-code-inserted) formats text in one step. Text that is formatted before it is inserted is described in [Creating Documents](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/creating/#build-a-document-from-pieces).

### Find and Replace Text

A placeholder is replaced in a loop: `Find` returns the zero-based position of the text, or -1, `Select` and `Selection.Text` replace it, and the next search starts behind the inserted text. [Replace All Occurrences](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/editing-text/search-replace/#replace-all-occurrences) in Search and Replace shows the loop and the `FindOptions`. `Find` of the `TextControl` looks only in the text part with the input focus; to replace a placeholder in headers, footers and text frames as well, run the loop for each [IFormattedText](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/iformattedtext/llms-full.txt) of [TextParts](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/textparts-property/llms-full.txt), with the `Find` and `Selection` of the text part, as [Search Headers, Footers and Text Frames](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/editing-text/search-replace/#search-headers-footers-and-text-frames) does. Templates filled with data are better served by merge fields than by placeholder text; see [Mail Merge](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/mail-merge/llms-full.txt).

### Format Paragraphs

Paragraphs end with `\r\n`. A line break inside a paragraph, the one Shift+Enter inserts, is the vertical tab character, `"\v"` in C#: `textControl1.Selection.Text = "Text Control, LLC\vCharlotte, NC";` inserts two lines that share one paragraph and its format, and [Keyboard Shortcuts](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/editing-text/keyboard-shortcuts/#insert-a-line-break-instead-of-a-paragraph-on-enter) makes the Enter key do the same.

[Selection.ParagraphFormat](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/selection/paragraphformat-property/llms-full.txt) formats the paragraph the user is in, as [Format Paragraphs](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/formatting/#format-paragraphs) in Text and Paragraph Formatting shows with all properties of [ParagraphFormat](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/paragraphformat/llms-full.txt). Code that builds a document formats paragraphs without selecting them: [Paragraphs](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/paragraphs-property/llms-full.txt) holds those of the text part with the input focus, numbered from 1, and [Paragraph.Format](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/paragraph/format-property/llms-full.txt) takes a `ParagraphFormat` object:

```csharp
private void FormatParagraphsButton_Click(object sender, RoutedEventArgs e)
{
    textControl1.Text = "Terms and Conditions\r\n" +
                        "These terms apply to all orders placed with our company.\r\n" +
                        "Appendix";

    // Heading: centered, 12 pt space after it, on the same page as the next paragraph
    textControl1.Paragraphs[1].Format = new ParagraphFormat(TXTextControl.HorizontalAlignment.Center)
    {
        BottomDistance = 240, // 240 twips = 12 pt
        KeepWithNext = true
    };

    // Appendix: always starts at the top of a page
    textControl1.Paragraphs[3].Format = new ParagraphFormat() { PageBreakBefore = true };
}
```

`System.Windows` has a `HorizontalAlignment` of its own, for the layout of WPF elements, so the code writes `TXTextControl.HorizontalAlignment`.

### Use Paragraph and Inline Styles

Named styles keep documents consistent. A [ParagraphStyle](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/paragraphstyle/llms-full.txt) holds character attributes and paragraph formatting for whole paragraphs, and an [InlineStyle](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/inlinestyle/llms-full.txt) holds character attributes for words within a paragraph. [Stylesheets](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/stylesheets/llms-full.txt) shows how to create styles in code, apply them with `FormattingStyle`, update a style across the document and copy styles from a template. Stylesheet programming, creating and changing styles in code, is a feature of the Enterprise edition, according to Text Control's feature comparison of the WPF editions.

A document loaded from Word comes with its styles, such as "Heading 1", which [Paragraph.FormattingStyle](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/paragraph/formattingstyle-property/llms-full.txt) returns by name. [Selection.RemoveInlineStyles](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/selection/removeinlinestyles-method/llms-full.txt) takes the selected text back to the attributes of its paragraph style.

### Create Bulleted and Numbered Lists

A paragraph becomes a list item when it gets a [ListFormat](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/listformat/llms-full.txt) of the [ListType](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/listtype/enumeration/llms-full.txt) `Bulleted`, `Numbered` or `Structured`. [Bullets and Numbered Lists](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/lists/llms-full.txt) creates each kind, restarts the numbering and reads the list numbers. Code that builds a document assigns the format to [Paragraph.ListFormat](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/paragraph/listformat-property/llms-full.txt) of each item, for example `textControl1.Paragraphs[2].ListFormat = new ListFormat(ListType.Bulleted);`, instead of selecting the paragraphs.

### Read the Text of a Document

The following handler lists the paragraphs of the main text with their style, lines, alignment and list number:

```csharp
private void ReadTextButton_Click(object sender, RoutedEventArgs e)
{
    StringBuilder text = new StringBuilder();

    foreach (TXTextControl.Paragraph paragraph in textControl1.TextParts.GetMainText().Paragraphs)
    {
        text.AppendLine($"#{paragraph.Number} \"{paragraph.FormattingStyle}\", " +
            $"{paragraph.Lines} line(s), {paragraph.Format.Alignment}: " +
            $"{paragraph.ListNumberText}{paragraph.Text}");
    }

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

For each paragraph, [Lines](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/paragraph/lines-property/llms-full.txt) is the number of lines, [Text](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/paragraph/text-property/llms-full.txt) the text and [ListNumberText](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/paragraph/listnumbertext-property/llms-full.txt) the list number with the text around it. [Lines](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/lines-property/llms-full.txt) of the `TextControl` returns the lines, each with the [Page](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/line/page-property/llms-full.txt) it's on. Because the handler reads the paragraphs of `GetMainText`, it gets the main text even while the cursor is in a header.

### Next Steps

- [Tables and Calculations](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/tables/llms-full.txt): arrange text in rows and columns.
- [Fields and Subtext Parts](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/fields/llms-full.txt): mark text that your code finds and changes later.
- [Sections, Pages, Headers, and Footers](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/sections-headers/llms-full.txt): set the page format and repeat text on every page.
- [Stylesheets](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/stylesheets/llms-full.txt): let users apply and edit styles.

---

## Nearby

- **Previous:** [Saving and Converting Documents](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/saving-converting/llms-full.txt)
- **Next:** [Tables and Calculations](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/tables/llms-full.txt)
