TX Text Control .NET for WPF

Text, Paragraphs, and Stylesheets

Topic

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, Stylesheets and Bullets and Numbered Lists. MainWindow.xaml.cs needs these using directives:

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 gets or sets the whole text of the control. To format part of it, select the characters with Select, 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, such as Bold, FontSize in twips or ForeColor. Format Characters in Text and Paragraph Formatting lists the attributes, and Format a Range Your Code Inserted formats text in one step. Text that is formatted before it is inserted is described in Creating Documents.

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 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 of TextParts, with the Find and Selection of the text part, as Search Headers, Footers and Text Frames does. Templates filled with data are better served by merge fields than by placeholder text; see Mail Merge.

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 makes the Enter key do the same.

Selection.ParagraphFormat formats the paragraph the user is in, as Format Paragraphs in Text and Paragraph Formatting shows with all properties of ParagraphFormat. Code that builds a document formats paragraphs without selecting them: Paragraphs holds those of the text part with the input focus, numbered from 1, and Paragraph.Format takes a ParagraphFormat object:

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 holds character attributes and paragraph formatting for whole paragraphs, and an InlineStyle holds character attributes for words within a paragraph. Stylesheets 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 returns by name. Selection.RemoveInlineStyles 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 of the ListType Bulleted, Numbered or Structured. Bullets and Numbered Lists creates each kind, restarts the numbering and reads the list numbers. Code that builds a document assigns the format to Paragraph.ListFormat 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:

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 is the number of lines, Text the text and ListNumberText the list number with the text around it. Lines of the TextControl returns the lines, each with the Page 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