Insert Merge Field with Formatting programatically

2 posts, 0 answers
  1. colin
    colin avatar
    1 posts
    Member since:
    Jun 2010

    Posted 19 Jun 2012 Link to this post

    I want to insert a merge field programatically into my RadDocument.
    When I insert the field I want it to have a specific style (font, size) and ignore the current style for the current position of the document.

    I can insert the field user the Insertfield() function on a RadDocument but how can I then change the style for that merge field afterwards?
  2. Iva Toteva
    Iva Toteva avatar
    1319 posts

    Posted 22 Jun 2012 Link to this post

    Hi Colin,

    You can insert a merge field with specific formatting in the following way:

    RadDocument document = new RadDocument();
    Section section = new Section();
    Paragraph paragraph = new Paragraph();
    FieldRangeStart start = new FieldRangeStart();
    FieldRangeEnd end = new FieldRangeEnd();
    end.Start = start;
    start.Field = new MergeField() { PropertyPath = "FirstName" };
    Span span = new Span("{MERGEFIELD FirstName}");
    span.FontSize = Unit.PointToDip(18);
    span.ForeColor = Colors.Green;
    DocumentFragment fragment = document.Selection.CopySelectedDocumentElements();

    The other option would be to change the formatting of the merge field after you have inserted it. However, in this way the actions will be registered separately in the history stack and will be undone one by one:
    this.editor.InsertField(new MergeField() { PropertyPath = "FirstName" });
    DocumentPosition position = new DocumentPosition(this.editor.Document.CaretPosition);
    FieldRangeEnd fieldEnd = position.GetCurrentInline() as FieldRangeEnd;
    if (fieldEnd != null)
        FieldRangeStart start = (FieldRangeStart)fieldEnd.Start;

    I hope this answers your question.

    Iva Toteva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft R3 2016 release webinar banner
Back to Top