# Formatting API

If you want to format the content of **RadRichTextBox** at run time, you have to use the API exposed by **RadRichTextBox**. This is essential, as the main purpose of **RadRichTextBox** is to allow the users to format their input via UI. The UI should call the respective API methods of the control.

> To learn more about the methods exposed by the API take a look at [this topic](http://www.telerik.com/help/wpf/allmembers_t_telerik_windows_controls_radrichtextbox.html).

## Changing the text formatting

The **RadRichTextBox** exposes methods that change the style of the selected text or the paragraph. When a method is called, the respective style is applied to the selected text. If there is no selection available, the style is applied to the word in which the caret is located.

> tip RadRichTextBox provides a fully functional formatting UI out of the box. To learn more about it read the [RadRichTextBoxRibbonUI](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/ui-for-applying-rich-text-formatting/features-radrichtextboxribbonui) topic.

Here is an example of a toggle button that upon checking should make the selection or the current word bold. In the handler of the **Click** event of the **RadToggleButton**, the **ToggleBold()** method of **RadRichTextBox** is called.

```XAML
    <StackPanel>
        <telerik:RadToggleButton x:Name="BoldButton" Content="B" Padding="5" HorizontalAlignment="Left" Click="BoldButton_Click" />
        <telerik:RadRichTextBox x:Name="radRichTextBox" LayoutMode="Paged" Height="200" />
    </StackPanel>
```

```C#
private void BoldButton_Click(object sender, RoutedEventArgs e)
{
    this.radRichTextBox.ToggleBold();
}
```

```VB.NET
Private Sub BoldButton_Click(sender As Object, e As RoutedEventArgs)
    Me.radRichTextBox.ToggleBold()
End Sub
```

![WPF RadRichTextBox Bolded Text](https://www.telerik.com/products/wpf/documentation/assets/ceea2c4b2b698b62ca5ed808b855dd3d/radrichtextbox_formatting_01.png)

## Using the active editor

**RadRichTextBox** supports headers and footers. They are represented through separate instances of  **RadRichTextBox**. When a document has headers and footers you can use the **ActiveDocumentEditor** property           of **RadRichTextBox** to get the editor instance where the caret is currently situated.

> tip You can find more about the Header and Footer functionality in [this article](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/features/headers-and-footers).

The following example inserts the word "text" at the CaretPosition.

```C#
    this.radRichTextBox.ActiveDocumentEditor.Insert("text");
```

```VB.NET
	Me.radRichTextBox.ActiveDocumentEditor.Insert("text")
```

## Creating a DocumentFragment

One of the common uses of the API is creating and inserting a **DocumentFragment**. Currently you can create a fragment in two ways:

* through **DocumentFragment**'s constructor;

* through selection.

Both approaches can be used to insert content at the caret position with the **InsertFragment** method:

```C#
    this.radRichTextBox.InsertFragment(fragment);
```

### Using the constructor of DocumentFragment

If you create a fragment in this way, it will end with a new paragraph. This is convenient when you want to separate the inserted fragment and end it with a new line. Furthermore, in this way if the last paragraph is in a list, it will appear properly in the new document.

```C#
    DocumentFragment fragmentFromDocument = new DocumentFragment(radDocument);
```

```VB.NET
Dim fragment As New DocumentFragment(radDocument)
```

This is also the suggested approach when merging several documents into one.

### Using the selection

If you choose to use the document selection when creating a **DocumentFragment**, there will be no additional paragraph after the fragment.

```C#
DocumentFragment fragmentFromSelection = radDocument.Selection.CopySelectedDocumentElements();
```

```VB.NET
Dim fragment As DocumentFragment = document.Selection.CopySelectedDocumentElements()
```

> For merging RadDocument instances, you can use the [RadDocumentMerger class](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/features/merge-documents/raddocumentmerger).

## See Also

* [Import/Export](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/import-export/features-import-export)
* [RadDocumentMerger](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/features/merge-documents/raddocumentmerger)
