This is a migrated thread and some comments may be shown as answers.

MailMerge ConditionalMergeFields, 1:n Relations, Application Resource Styles

1 Answer 111 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Wolfgang
Top achievements
Rank 1
Wolfgang asked on 24 Jul 2012, 09:46 AM

Hi Telerik team,

We are evaluating your RichTextBox and MailMerge functionality in order to display read-only views.

Some questions had arisen we would like to address to you:

We use our own Condiational MergeFields to hide, for example, a paragraph if the value of a Mergefield is empty. This is relevant in label-value situations where you, in addition to the empty value field, want to hide the Label. The paragraphs with conditional MergeFields are removed after loading the document. However, this procedure requires the reload of the document after a MailMergeDataSource change. Do you have a different approach to implement this behavior, perhaps to hide a section or paragraph instead of removing?
public static class ConditionalMergeFields
{
    
public static void RemoveEmptyMergeFields(RadDocument document)
    {
        
foreach (var section in document.Sections)
        {
            
// enumerate paragraphs which contain empty ConditionalMergeFields
            foreach (var paragraph in section.EnumerateChildrenOfType<Paragraph>().ToList()
                  .Where(paragraph => paragraph.EnumerateChildrenOfType<
FieldRangeStart>()
                        .Any(f => f.Field 
is IConditionalMergeField && ((IConditionalMergeField) f.Field).IsEmpty)))
            {
                section.Blocks.Remove(paragraph);
            }
        }
    }
}

 

In order to implement lists and 1:n relationships in Richtext Templates we use custom List MergeFields. In the overloaded GetResultFragment method a new RadDocument is created, mailmerged and returned as DocumentFragment. As the amount of List items grows, the MailMerge method of the RadDocument consumes some time (4 times about 1.2 seconds). Would you suggest a different approach?

protected override DocumentFragment GetResultFragment()
{
    var subList = getList();
 
    var document = (RadDocument)SubDataDocument.CreateDeepCopy();
 
    document.MailMergeDataSource.ItemsSource = subList;
 
    ConditionalMergeFields.RemoveEmptyMergeFields(document);
 
    document.ChangeAllFieldsDisplayMode(FieldDisplayMode.Result);
 
    document.MailMergeDataSource.MoveToFirst();
 
    if (subList.Count > 1)
    {
        document = document.MailMerge(false);
    }
 
    if (document.Sections.First.Headers != null && document.Sections.First.Headers.Default != null && document.Sections.First.Headers.Default.Body != null)
    {
        document.Sections.AddBefore(document.Sections.First, 
                              (Section)document.Sections.First.Headers.Default.Body.Sections.First.CreateDeepCopy());
    }
 
    return new DocumentFragment(document);
}
 

Text Styling like font size and font color are specified to a predetermined value in the RadDocument Template. Is there a way to bind them to application resources in order to style and change them on the fly? StaticResource is not working since the XamlFormatProvider does not find the Resource during the XAML parse Process.

 

Thank you in advance,

Wolfgang

1 Answer, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 27 Jul 2012, 10:44 AM
Hello Wolfgang,

Thank you for your interest in RadRichTextBox. Please see below the answers to your questions:

1. Document elements (such as paragraphs and sections) cannot be hidden. The only solution with the current version would be to remove them when they should not be shown.

We have, however, considered the label-merge field scenario you have described and we believe that it would be nice to have the option to "hide" the paragraph if the value of the field is empty. In MS Word for example, this can be achieved using the TextBeforeIfNotEmpty and TextAfterIfNotEmpty properties of the merge field. We have logged this feature and will probably implement it in one of the next releases.

2. As for the styling of the merge fields, each field's formatting is saved using the respective properties of the Span between the FieldRangeStart and FieldRangeEnd elements:

<t:FieldRangeStart AnnotationID="1">
  <t:MergeField DisplayMode="Code" PropertyPath="FirstName" />
</t:FieldRangeStart>
<t:Span FontFamily="Courier New" FontSize="22.6666669845581" ForeColor="#FFFF0000" Text="{}{MERGEFIELD FirstName}" />
<t:FieldRangeEnd AnnotationID="1" />

In order to apply some formatting to all merge fields in the document, you could iterate through the field range starts (when the field is MergeField), select the merge fields and change their formatting using the methods and commands of the editor. You can find more information on the mail-merge related API in this article.

I hope this answers your questions.

All the best,
Iva Toteva
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
RichTextBox
Asked by
Wolfgang
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Share this question
or