TX Text Control .NET for Windows Forms

Text, Paragraphs, and Stylesheets

Topic

Placeholders to replace, amounts to highlight, headings that should look alike in every document: most document processing works on text. Your code selects text by its position, finds and replaces it in every part of the document, formats characters and paragraphs, defines paragraph and inline styles and reads the text back. The sections below show how these tasks fit together in code and where each one is explained in full.

Formatting with the ribbon, the dialog boxes and in code is described in Text and Paragraph Formatting, Stylesheets and Bullets and Numbered Lists; the same members work in a ServerTextControl. The examples work on the TextControl named textControl1, and Form1.cs needs these using directives:

using System;
using System.Text;
using System.Windows.Forms;
using TXTextControl;

Select and Format Characters

To format text, select it and set the properties of Selection, such as Bold, FontSize or ForeColor. Select takes the zero-based position of the first character and the number of characters, as Select and Replace Text in Code shows, and Format Characters lists the character attributes. To insert text that is already formatted, assign a new Selection object, as Creating Documents shows.

Find and Replace Text

TextControl.Find searches only the text part with the input focus, so a placeholder in a header or a text frame is replaced only when your code searches every text part: TextParts contains them all, each with its own Find and Selection. Replace All Occurrences shows the replace loop and the FindOptions, and Search Headers, Footers and Text Frames runs a search through all text parts. For templates that are filled with data, merge fields are the structured alternative to placeholder text; see Mail Merge.

Format Paragraphs

A paragraph ends with \r\n. Paragraphs contains the paragraphs of the text part with the input focus, numbered from 1, and Paragraph.Format takes a ParagraphFormat object, so your code formats a paragraph by its number without selecting it:

private void formatParagraphsButton_Click(object sender, EventArgs e)
{
    textControl1.ResetContents();
    textControl1.Selection.Text = "Terms and Conditions\r\nThese terms apply to all orders.";

    // Heading: centered and on the same page as the next paragraph
    textControl1.Paragraphs[1].Format = new ParagraphFormat(TXTextControl.HorizontalAlignment.Center)
    {
        KeepWithNext = true
    };
}

HorizontalAlignment exists in Windows Forms as well, so the code writes TXTextControl.HorizontalAlignment. Format Paragraphs lists the properties of ParagraphFormat, and Selection.ParagraphFormat formats all paragraphs the selection touches.

Use Paragraph and Inline Styles

A style gives a set of attributes a name. A ParagraphStyle applies to whole paragraphs and an InlineStyle to words within a paragraph, and text that uses a style changes when the style changes. Create Styles in Code, Apply a Style and Change a Style Everywhere show the code. Creating and changing styles in code is part of the Enterprise edition, see Editions and Feature Availability.

Create Bulleted and Numbered Lists

A ListFormat assigned to Paragraph.ListFormat or Selection.ListFormat makes a paragraph a list item. Bullets and Numbered Lists creates bulleted, numbered and multilevel lists, restarts the numbering and reads the list numbers.

Read the Text of a Document

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

private void readTextButton_Click(object sender, EventArgs e)
{
    StringBuilder text = new StringBuilder();

    foreach (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());
}

Paragraph.Lines is the number of lines of a paragraph, Text its text and ListNumberText its list number with the text around it. TextControl.Lines gives the lines with the Page each one is on. GetMainText makes sure the handler reads the main text, even when the user's cursor is in a header.

Next Steps