Skip to content

Commit 259fc87

Browse files
author
KB Bot
committed
Added new kb article simulating-mail-merge-with-html-content
1 parent 49b248b commit 259fc87

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
title: Simulating Mail Merge with HTML content by Utilizing the Find and Replace Functionality
3+
description: Learn how to render HTML content with formatting options during mail merge using Telerik Document Processing Library.
4+
type: how-to
5+
page_title: Simulating Mail Merge with HTML content by Utilizing the Find and Replace Functionality
6+
meta_title: Simulating Mail Merge with HTML content by Utilizing the Find and Replace Functionality
7+
slug: simulating-mail-merge-with-html-content
8+
tags: wordsprocessing,telerik document processing,mail merge,html content,html format provider
9+
res_type: kb
10+
ticketid: 1694621
11+
---
12+
13+
## Environment
14+
15+
| Version | Product | Author |
16+
| ---- | ---- | ---- |
17+
| 2025.2.520| RadWordProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
18+
19+
## Description
20+
21+
I want to perform mail merge using Telerik Document Processing Library, where HTML content needs to replace placeholders in a DOCX template. When performing mail merge, the library binds plain HTML text instead of rendering the HTML with formatting. In some cases, using the HtmlFormatProvider results in corrupted documents.
22+
23+
This knowledge base article also answers the following questions:
24+
- How to insert formatted HTML content in mail merge using Telerik Document Processing?
25+
- How to replace placeholders with styled HTML content in RadFlowDocument?
26+
- How to use HtmlFormatProvider correctly in Telerik WordsProcessing?
27+
28+
## Solution
29+
30+
To render HTML content during mail merge, use the find-and-replace functionality instead of the default mail merge engine. Replace placeholders with styled HTML content using the following steps:
31+
32+
1. Import the HTML content using HtmlFormatProvider.
33+
2. Import the DOCX template using DocxFormatProvider.
34+
3. Find placeholders in the template and replace them with the imported HTML content.
35+
36+
### Code Example
37+
38+
```csharp
39+
// Import HTML content
40+
RadFlowDocument htmlFlowDocument;
41+
using (Stream input = File.OpenRead(@"info.html"))
42+
{
43+
Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider htmlProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider();
44+
htmlFlowDocument = htmlProvider.Import(input, TimeSpan.FromSeconds(10));
45+
}
46+
47+
// Import DOCX template
48+
RadFlowDocument templateFlowDocument;
49+
Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider docxProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();
50+
51+
using (Stream input = File.OpenRead("template.docx"))
52+
{
53+
templateFlowDocument = docxProvider.Import(input, TimeSpan.FromSeconds(10));
54+
}
55+
56+
// Replace placeholder with HTML content
57+
List<BlockBase> newContent = new List<BlockBase>();
58+
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(templateFlowDocument);
59+
60+
foreach (Section section in htmlFlowDocument.Sections)
61+
{
62+
Section clonedSection = section.Clone(templateFlowDocument);
63+
newContent.AddRange(clonedSection.Blocks);
64+
}
65+
66+
editor.ReplaceText("<<EXSUSection>>", newContent, true, false);
67+
68+
// Export the modified document
69+
string outputFilePath = "output.docx";
70+
File.Delete(outputFilePath);
71+
using (Stream output = File.OpenWrite(outputFilePath))
72+
{
73+
docxProvider.Export(templateFlowDocument, output, TimeSpan.FromSeconds(10));
74+
}
75+
76+
// Open the output file
77+
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
78+
```
79+
80+
### Notes:
81+
- Replace `<<EXSUSection>>` with your placeholder text.
82+
- Modify the code to suit your template and requirements.
83+
- Ensure the provided HTML content is supported by HtmlFormatProvider.
84+
85+
## See Also
86+
87+
- [HtmlFormatProvider Documentation](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/formats-and-conversion/html/htmlformatprovider)
88+
- [DocxFormatProvider Documentation](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/formats-and-conversion/word-file-formats/docx/docxformatprovider)
89+
- [Mail Merge Documentation](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/editing/mail-merge)
90+
- [Replace Text with Document Elements](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/editing/find-and-replace/replace-document-elements)

0 commit comments

Comments
 (0)