New to Telerik Document ProcessingStart a free 30-day trial

Simulating Mail Merge with Formatted HTML Content by Using the Find and Replace Functionality

Updated on Jun 9, 2026

Environment

VersionProductAuthor
2025.2.520RadWordProcessingDesislava Yordanova

Description

This article shows how to simulate mail merge when formatted HTML content needs to replace placeholders in a DOCX template. When you perform a mail merge, the RadWordsProcessing library binds plain HTML text instead of rendering the HTML with its original formatting.

Solution

To insert the formatted HTML content, use the Find-and-Replace functionality instead. Replace placeholders with the styled HTML content by following these steps:

  1. Import the HTML content using HtmlFormatProvider.

  2. Import the DOCX template using DocxFormatProvider.

  3. Find placeholders in the template and replace them with the imported HTML content.

    Replace Placeholders with HTML content in DOCX template ><

Code Example

csharp
// Import HTML content
RadFlowDocument htmlFlowDocument;
using (Stream input = File.OpenRead(@"info.html"))
{
    Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider htmlProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider(); 
    htmlFlowDocument = htmlProvider.Import(input, TimeSpan.FromSeconds(10));
}

// Import DOCX template
RadFlowDocument templateFlowDocument;
Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider docxProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();

using (Stream input = File.OpenRead("template.docx"))
{
    templateFlowDocument = docxProvider.Import(input, TimeSpan.FromSeconds(10));
}

// Replace placeholder with HTML content
List<BlockBase> newContent = new List<BlockBase>();
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(templateFlowDocument);

foreach (Section section in htmlFlowDocument.Sections)
{
    Section clonedSection = section.Clone(templateFlowDocument);
    newContent.AddRange(clonedSection.Blocks);
}

editor.ReplaceText("<<EXSUSection>>", newContent, true, false);

// Export the modified document
string outputFilePath = "output.docx";
File.Delete(outputFilePath);
using (Stream output = File.OpenWrite(outputFilePath))
{
    docxProvider.Export(templateFlowDocument, output, TimeSpan.FromSeconds(10));
}

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

Notes

  • Replace <<EXSUSection>> with your placeholder text.
  • Modify the code to suit your template and requirements.
  • Ensure the provided HTML content is supported by HtmlFormatProvider.

See Also