I want to delete a merge field how to do that?

1 Answer 156 Views
WordsProcessing
Faraz
Top achievements
Rank 1
Faraz asked on 27 Dec 2022, 12:16 PM

Basically i am trivarsing whole word document template and updating the text of merge field run.
But after updating the text i want to delete the merge field. How to do that?

var currentDocument = document.EnumerateChildrenOfType<DocumentElementBase>().ToList();
                    int indexElement = 0;
                    foreach (var currentElement in currentDocument)
                    { 

if (currentElement.GetType() == typeof(Run))
                        {
                            currentRun = (Run)currentElement;

 if (currentRun.Text.StartsWith("«") && currentRun.Text.Length > 2)
                            {

string runText= currentRun.Text.Substring(1, currentRun.Text.Length - 2);
if(runText == "name")
currentRun.Text = "faraz";

}

Now after updating the text i want to delete the merge field and maintain the run text. How to do that?

1 Answer, 1 is accepted

Sort by
1
Yoan
Telerik team
answered on 29 Dec 2022, 01:47 PM

Hello Faraz,

The deletion of a merge field can be achieved in two steps by utilizing RadFlowDocumentEditor's DeleteContent method. The first step is passing the Start and Separator field characters of an InfoField as parameters to the method, which will delete the scope between and including those characters. The second step is passing the End field character twice to the same method, which removes the rest, and all that is left is the value of the field as text.

When it comes to replacing text, there is a more convenient and easier approach, which is our Find and Replace Text and Style functionality. In your case, it can be used to replace the leftover text from the fields with the content you desire.

I have attached an example project that I used, in case you want to examine and test it yourself.

Hope this helps.

Regards, Yoan Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Faraz
Top achievements
Rank 1
commented on 29 Dec 2022, 02:04 PM

Thank you sir. It worked :)
Tags
WordsProcessing
Asked by
Faraz
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Share this question
or