How to: Implement the Format Painter functionality

  • 2 minutes to read

Currently, ASPxRichEdit doesn't provide the Format Painter feature out of the box. This example illustrates a possible solution to implement this functionality in a custom way by using custom ribbon buttons and the ASPxRichEdit's client-side API.

For this purpose, handle the editor's client-side CustomCommandExecuted event to check if a custom ribbon item is clicked, and get the font and paragraph formatting applied to the currently selected content via the getState, getState commands.

To achieve the behavior similar to that the MS Word Format Painter's provides, the ASPxRichEdit selection is changed programmatically to select only the selected text's first symbol before calling corresponding commands in order to get its formatting as the formatting of the selected interval. After retrieving formatting information for this symbol, the control's selection is restored to its original state.

After that, the saved formatting can be applied to the new selected content on a ribbon item click via the changeFontFormatting and changeParagraphFormatting commands.

View Example

using DevExpress.Web;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ASPxRichEdit1.Open(Server.MapPath("CharacterFormatting.rtf"));
        }

        ASPxRichEdit1.CreateDefaultRibbonTabs(true);

        var copyItem = new RibbonButtonItem("Copy format");
        copyItem.LargeImage.IconID = "edit_copy_32x32office2013";
        copyItem.Size = RibbonItemSize.Large;
        ASPxRichEdit1.RibbonTabs[1].Groups[1].Items.Add(copyItem);

        var pasteItem = new RibbonButtonItem("Paste format");
        pasteItem.LargeImage.IconID = "edit_paste_32x32office2013";
        pasteItem.Size = RibbonItemSize.Large;
        ASPxRichEdit1.RibbonTabs[1].Groups[1].Items.Add(pasteItem);
    }
}