Use a MergeField With a Formated (RTF) Text

4 posts, 0 answers
  1. Emmanuelli
    Emmanuelli avatar
    5 posts
    Member since:
    Jun 2009

    Posted 04 Jun 2014 Link to this post

    Hi,

    I search to merge a document with some special field formated with Bold and new line in a Document. This "special field" come from a RichTextBox.
    Is it possible?

    Thx in advance
  2. Boby
    Admin
    Boby avatar
    595 posts

    Posted 09 Jun 2014 Link to this post

    Hi Emmanuelli,
    By default, the data source of the merge fields is merged as plain text (using the current formatting). You can, however, create custom type of merge fields by inheriting MergeField class and overriding GetResultFragment method. This method returns DocumentFragment, which means that you can apply the desired formatting and inlude more than one paragraph (i.e. the new lines). Similar approach is demonstrated in this forum thread. Following is a sample implementation of the GetResultFragment method:
    protected override DocumentFragment GetResultFragment()
    {
        Person customer = this.Document.MailMergeDataSource.CurrentItem as Person;
     
        if (customer == null)
        {
            return null;
        }
     
        if (this.PropertyPath == "FirstName")
        {
            RadDocumentEditor editor = new RadDocumentEditor(new RadDocument());
     
            editor.Insert(Environment.NewLine);
            editor.Insert(customer.FirstName);
            editor.Insert(Environment.NewLine);
     
            editor.Document.Selection.SelectAll();
            editor.ChangeFontWeight(FontWeights.Bold);
     
            return new DocumentFragment(editor.Document);
        }
     
        return null;
    }


    You can insert such fields using the RadRichTextBox/RadDocumentEditor's InsertField method:

    this.radRichTextBox.InsertField(new CustomMergeField() { PropertyPath = "FirstName" });

    Don't hesitate to contact us if you have other questions.


    Regards,
    Boby
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. DevCraft banner
  4. Emmanuelli
    Emmanuelli avatar
    5 posts
    Member since:
    Jun 2009

    Posted 13 Jun 2014 in reply to Boby Link to this post

    thx for you answer but I search to insert a DocumentFragment coming from an other RadRichTextBox.

    I create a DocumentFragment Field with an imported XAML but when I merge with this field, nothing...
  5. Boby
    Admin
    Boby avatar
    595 posts

    Posted 16 Jun 2014 Link to this post

    Hello Emmanuelli,
    Could you explain in more details what is your scenario, as I am having hard time understanding what are you trying to achieve?

    Looking forward to your reply.

    Regards,
    Boby
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top