New to Telerik Document ProcessingStart a free 30-day trial

How to Remove a MERGEFIELD While Replacing the Placeholders with Values in RadWordsProcessing

Updated on Jun 9, 2026

Environment

VersionProductAuthor
2024.3.806RadWordsProcessingDesislava Yordanova

Description

When preparing a document for the MailMerge operation in RadWordsProcessing, you may need to remove a MERGEFIELD without losing its inserted value. This process keeps the document clean and ready for MailMerge without issues related to leftover MERGEFIELD entries.

This KB article also answers the following questions:

  • How can I delete MERGEFIELD fields but keep their content in the document?
  • What is the correct approach to prepare a document for MailMerge in RadWordsProcessing?
  • Is there a way to clean up MERGEFIELD entries from a document without affecting its content?

Solution

To remove a MERGEFIELD while keeping its values, use the RadFlowDocumentEditor's DeleteContent method. This approach deletes the field codes but leaves the field values as plain text in the document. The Find and Replace feature provides a straightforward way to handle any leftover text from the fields.

The following example shows how to accomplish this:

csharp
using Telerik.Windows.Documents.Flow.Model.Editing;
using Telerik.Windows.Documents.Flow.Model;
using Telerik.Windows.Documents.Flow.Model.Fields;
using Telerik.Windows.Documents.Flow.FormatProviders.Docx;
using System.Diagnostics;

RadFlowDocument document = new RadFlowDocument();
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
FieldInfo firstNameField = editor.InsertField("MERGEFIELD First_Name", "«first name»");
FieldInfo lastNameField = editor.InsertField("MERGEFIELD Last_Name", "«last name»");

//Get field characters of merged fields
var fieldCharacters = document.EnumerateChildrenOfType<FieldCharacter>().Where(ch => ch.FieldInfo.Field is MergeField).ToList();

//Get field infos of merged fields
var fieldInfos = fieldCharacters.Select(fc => fc.FieldInfo).Distinct();

//Iterate and delete merge fields
foreach (var fieldInfo in fieldInfos)
{
    editor.DeleteContent(fieldInfo.Start, fieldInfo.Separator, true);
    editor.DeleteContent(fieldInfo.End, fieldInfo.End, true);
}

//Replace the text that is left
editor.ReplaceText("«first name»", "John ");
editor.ReplaceText("«last name»", "Smith ");


//Export document
DocxFormatProvider provider = new DocxFormatProvider();
string outputFilePath = "output.docx";
File.Delete(outputFilePath);
using (Stream output = File.OpenWrite(outputFilePath))
{
    provider.Export(document, output);
}

//Open document
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });

This method removes the MERGEFIELD effectively, leaving only the value in the document.

BeforeAfter
Merge Fields BeforeMerge Fields After

See Also