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

Workaround for HTML not supporting merge codes

4 Answers 71 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 14 Nov 2012, 09:32 PM
According to both of these links:
http://www.telerik.com/community/forums/wpf/richtextbox/mailmerge-export-to-html.aspx
http://www.telerik.com/community/forums/silverlight/richtextbox/mail-merge-and-export-as-html-with-merged-tags.aspx

Merge codes are not supported in HTML.  These are over a year old now.  Will this change?  If not, how can I programatically process a document to fix this?

4 Answers, 1 is accepted

Sort by
0
Paul
Top achievements
Rank 1
answered on 15 Nov 2012, 03:32 PM
I managed to figure out a work around myself.  If anyone else should find this helpful, I will post it here.  If anyone from Telerik knows a better way or sees flaws, please feel free to post as well!

This will simply find and fix merge codes when opening an HTML document.  This code is in the DocumentChanged event handler.

// Fix all merge codes for HTML, start by moving to the top of the document
editor.Document.CaretPosition.MoveToFirstPositionInDocument();
 
// Grab all properties in the merge codes - My object type is "clsMergeCodeExample".
PropertyInfo[] properties = typeof(clsMergeCodeExample).GetProperties();
 
// Setup a search query
DocumentTextSearch search = new DocumentTextSearch(editor.Document);
List<TextRange> rangesTrackingDocumentChanges = new List<TextRange>();
 
// Define a caret position
DocumentPosition d = editor.Document.CaretPosition;
 
// Find the first text range.
TextRange t = search.Find("{MERGEFIELD", d);
 
while (t != null)
{
   // Find the end of the merge code
   TextRange tEnd = search.Find("}", d);
 
   // Combined range
   TextRange tFinal = new TextRange(t.StartPosition, tEnd.EndPosition);
    
   // Find out what the merge code is and remove the } character
   string code = tEnd.StartPosition.GetCurrentInlineBox().Text.Trim().TrimEnd('}');
 
   // Grab this field
   PropertyInfo p = properties.FirstOrDefault(fi => fi.Name.Equals(code, StringComparison.CurrentCultureIgnoreCase));
 
   // Adjust the code as needed. Can be skipped.
   code = (p == null ? "UnknownMergeField" : p.Name);
 
   // Highlight the selection we're about to replace to keep styling in tact
   tFinal.SetSelection(editor.Document);
 
   // Finally insert the merge field as needed replacing the selection.
   editor.Document.InsertField(new MergeField() { PropertyPath = code }, FieldDisplayMode.Code);
 
   // Advance to the next one
   t = search.Find("{MERGEFIELD", d);
}
0
Accepted
Mihail
Telerik team
answered on 19 Nov 2012, 04:15 PM
Hello Paul,

We are happy to hear you have found a solution to your problem. At this point, the export of fields in HTML is not planned, so it is not clear when and if it will be implemented.

If you have any other questions, feel free to contact us.

Kind regards,
Mihail
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Thilo
Top achievements
Rank 1
Iron
answered on 15 Aug 2014, 08:03 AM
Hello Paul !
this works fine for me !
I changed it to Docvariable

But Question to Telerik:
a Problem is, that RadDocument.InsertField ist declared as Deprecated.
When will it removed and are there any plan to replace it ?

My Code:
private RadDocument ReactivateFieldDOCVARIABLEInHtml(RadDocument document)
{
    string fieldname = "DOCVARIABLE";
 
    // Fix all merge codes for HTML, start by moving to the top of the document
    document.CaretPosition.MoveToFirstPositionInDocument();
 
    // Setup a search query
    DocumentTextSearch search = new DocumentTextSearch(document);
 
    List<TextRange> rangesTrackingDocumentChanges = new List<TextRange>();
 
    // Define a caret position
    DocumentPosition d = document.CaretPosition;
 
    // Find the first text range.
    TextRange t = search.Find("{" + fieldname, d);
 
    while (t != null)
    {
        // Find the end of the merge code
        TextRange fieldEnd = search.Find("}", d);
 
        // Combined range
        TextRange fieldPosition = new TextRange(t.StartPosition, fieldEnd.EndPosition);
 
        // Find out what the merge code is and remove the } character
        string varName = fieldEnd.StartPosition.GetCurrentInlineBox().Text.Trim().TrimEnd('}');
 
        // Highlight the selection we're about to replace to keep styling in tact
        fieldPosition.SetSelection(document);
 
        // Finally insert the merge field as needed replacing the selection.
        document.InsertField(new DocumentVariableField() { VariableName = varName });
 
        // Advance to the next one
        t = search.Find("{" + fieldname, d);
    }
 
    return document;
}
       
0
Petya
Telerik team
answered on 19 Aug 2014, 03:20 PM
Hi Thilo,

Indeed, the Insert~() methods of RadDocument are deprecated and we advise you against using them, although I cannot say when they are going to be removed exactly . As to an alternative, you can use the InsertField() method of RadRichTextBox. If for some reason you do not have access to RadRichTextBox (the document is not shown in a control or you are working from your view-model), you could always use the RadDocumentEditor class instead.

I hope this helps.

Regards,
Petya
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.
 
Tags
RichTextBox
Asked by
Paul
Top achievements
Rank 1
Answers by
Paul
Top achievements
Rank 1
Mihail
Telerik team
Thilo
Top achievements
Rank 1
Iron
Petya
Telerik team
Share this question
or