Skip to content

Added new kb article simulating-mail-merge-with-html-content #602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 87 additions & 0 deletions knowledge-base/simulating-mail-merge-with-html-content.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
title: Simulating Mail Merge with HTML content by Utilizing the Find and Replace Functionality
description: Learn how to render HTML content with formatting options during mail merge using Telerik Document Processing Library.
type: how-to
page_title: Simulating Mail Merge with HTML content by Utilizing the Find and Replace Functionality
meta_title: Simulating Mail Merge with HTML content by Utilizing the Find and Replace Functionality
slug: simulating-mail-merge-with-html-content
tags: words, processing,telerik, document, mail, merge, html, content, find, replace
res_type: kb
ticketid: 1694621
---

## Environment

| Version | Product | Author |
| ---- | ---- | ---- |
| 2025.2.520| RadWordProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|

## Description

This article demonstrates a sample approach how to simulate [mail merge]({%slug radwordsprocessing-editing-mail-merge%}), where HTML content needs to replace placeholders in a DOCX template. When performing mail merge, the WordProcessing library binds plain HTML text instead of rendering the HTML with formatting.

![Replace Placeholders with HTML content in DOCX template ><](images/simulating-mail-merge-with-html-content.png)

## Solution

To render HTML content during mail merge, use the [Find-and-Replace]({%slug radwordsprocessing-editing-find-and-replace%}) functionality instead of the default mail merge engine. Replace placeholders with styled HTML content using the following steps:

1. Import the HTML content using [HtmlFormatProvider]({%slug radwordsprocessing-formats-and-conversion-html-htmlformatprovider%}).
2. Import the DOCX template using [DocxFormatProvider]({%slug radwordsprocessing-formats-and-conversion-docx-docxformatprovider%}).
3. Find placeholders in the template and replace them with the imported HTML content.

### 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]({%slug radwordsprocessing-formats-and-conversion-html-supported-elements%}) by HtmlFormatProvider.

## See Also

- [HtmlFormatProvider]({%slug radwordsprocessing-formats-and-conversion-html-htmlformatprovider%})
- [DocxFormatProvider]({%slug radwordsprocessing-formats-and-conversion-docx-docxformatprovider%})
- [Mail Merge Documentation]({%slug radwordsprocessing-editing-mail-merge%})
- [Find-and-Replace]({%slug radwordsprocessing-editing-find-and-replace%})
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ __RadFlowDocumentEditor__ gives you the ability to format all occurrences of a s
* [RadFlowDocumentEditor]({%slug radwordsprocessing-editing-radflowdocumenteditor%})
* [CharacterProperties]({%slug radwordsprocessing-concepts-style-properties%})
* [RadFlowDocument]({%slug radwordsprocessing-model-radflowdocument%})
* [RadFlowDocumentEditor API Reference](https://docs.telerik.com/devtools/document-processing/api/Telerik.Windows.Documents.Flow.Model.Editing.RadFlowDocumentEditor.html)
* [Simulating Mail Merge with HTML content by Utilizing the Find and Replace Functionality]({%slug simulating-mail-merge-with-html-content%})
3 changes: 2 additions & 1 deletion libraries/radwordsprocessing/editing/mail-merge.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,5 @@ If you want to separate the items into several rows you need to close the group
* [Populate a Table with Data using Nested Mail Merge Functionality]({%slug populate-table-data-mail-merge%})
* [Generating a Word Document Template with Data Using MailMerge in RadWordsProcessing]({%slug generate-doc-template-and-populate-with-collection-data-mail-merge%})
* [How to Remove a MERGEFIELD While Replacing the Placeholders with Values in RadWordsProcessing]({%slug remove-mergefields-retain-values-radwordsprocessing%})
* [Performing Nested MailMerge with Multiple Levels in RadWordsProcessing]({%slug nested-mailmerge-radwordsprocessing%})
* [Performing Nested MailMerge with Multiple Levels in RadWordsProcessing]({%slug nested-mailmerge-radwordsprocessing%})
* [Simulating Mail Merge with HTML content by Utilizing the Find and Replace Functionality]({%slug simulating-mail-merge-with-html-content%})