RichTextBox "template" and dynamic regions

2 posts, 1 answers
  1. Axelle
    Axelle avatar
    13 posts
    Member since:
    Mar 2014

    Posted 14 Apr 2014 Link to this post


    I'm looking to implement a form of "print preview" function using RichTextBox : I want to edit by hand document template (at the moment they are in XAML, but any format would be acceptable) and to insert paragraphs or specific words coming from my software data.

    I've successfully used DocumentVariable to update Specifific fields, but I don't understand how to place DocumentVariableFields in a template (I currently do it programattically). Moreover, I'm not sure they allow to insert whole formatted paragraphs. 

    Is there a "right" way to achieve that using Telerik ? 

    Thanks in advance, 

  2. Answer
    Yancho avatar
    72 posts

    Posted 16 Apr 2014 Link to this post


    Thank you for your interest in Telerik RadRichTextBox!

    First, can you explain what do you mean by "don't understand how to place DocumentVariableFields in a template"? 

    To your other question about formatted paragraphs - since you are already using document variable fields you can you to handle the DocumentVariableEvaluating event pf the RadDocument class. There you have full control over what the result of the field should be. In the event handler you can set the Result property of the DocumentVariableEvaluatingEventArgs to a DocumetnFragment you've created and this fragment will be used as a result of the field. This way you can have formatted content and even multiple paragraphs as a result of the document variable field.
    Here is a code snippet that illustrates this approach. The result of the document variable field will be document fragment created form the document variable, but with heading style applied.
    //Add a document variable
    this.radRichTextBox.Document.DocumentVariables.Add("SpecificVariableName", "Custom result");
    this.radRichTextBox.Document.DocumentVariableEvaluating += Document_DocumentVariableEvaluating;
    //Insert a document variable field into the document
    this.radRichTextBox.InsertField(new DocumentVariableField() { VariableName = "SpecificVariableName" });
    void Document_DocumentVariableEvaluating(object sender, DocumentVariableEvaluatingEventArgs e)
    if (e.VariableName == "SpecificVariableName")
    object variable = e.Document.DocumentVariables["SpecificVariableName"];
    RadDocumentEditor editor = new RadDocumentEditor(new RadDocument());
    // Change the style of the paragraph to Heading1
    e.Result = new DocumentFragment(editor.Document);
    e.Handled = true;

    I hope this helps! Let me know how it goes.


    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

Back to Top