Skip to content

Commit 191f2a8

Browse files
authored
Merge pull request #602 from telerik/new-kb-simulating-mail-merge-with-html-content-18dae465a8884de29626ca63ccab0341
Added new kb article simulating-mail-merge-with-html-content
2 parents 80f91f3 + 9defa2a commit 191f2a8

File tree

4 files changed

+90
-2
lines changed

4 files changed

+90
-2
lines changed
60.3 KB
Loading
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: Simulating Mail Merge with formatted HTML content by Utilizing the Find and Replace Functionality
3+
description: Learn how to simulate Mail Merge with formatted HTML content using Telerik Document Processing Library.
4+
type: how-to
5+
page_title: Simulating Mail Merge with formatted HTML content by Utilizing the Find and Replace Functionality
6+
meta_title: Simulating Mail Merge with formatted HTML content by Utilizing the Find and Replace Functionality
7+
slug: simulating-mail-merge-with-html-content
8+
tags: words, processing, telerik, document, mail, merge, html, content, find, replace
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+
This article demonstrates a sample approach how to simulate [mail merge]({%slug radwordsprocessing-editing-mail-merge%}), where formatted HTML content needs to replace placeholders in a DOCX template. When performing a mail merge, the WordProcessing library binds plain HTML text, instead of rendering the HTML with its original formatting.
22+
23+
## Solution
24+
25+
To successfully insert the formatted HTML content, you can use the [Find-and-Replace]({%slug radwordsprocessing-editing-find-and-replace%}) functionality instead. Replace placeholders with the styled HTML content using the following steps:
26+
27+
1. Import the HTML content using [HtmlFormatProvider]({%slug radwordsprocessing-formats-and-conversion-html-htmlformatprovider%}).
28+
2. Import the DOCX template using [DocxFormatProvider]({%slug radwordsprocessing-formats-and-conversion-docx-docxformatprovider%}).
29+
3. Find placeholders in the template and replace them with the imported HTML content.
30+
31+
![Replace Placeholders with HTML content in DOCX template ><](images/simulating-mail-merge-with-html-content.png)
32+
33+
### Code Example
34+
35+
```csharp
36+
// Import HTML content
37+
RadFlowDocument htmlFlowDocument;
38+
using (Stream input = File.OpenRead(@"info.html"))
39+
{
40+
Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider htmlProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider();
41+
htmlFlowDocument = htmlProvider.Import(input, TimeSpan.FromSeconds(10));
42+
}
43+
44+
// Import DOCX template
45+
RadFlowDocument templateFlowDocument;
46+
Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider docxProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();
47+
48+
using (Stream input = File.OpenRead("template.docx"))
49+
{
50+
templateFlowDocument = docxProvider.Import(input, TimeSpan.FromSeconds(10));
51+
}
52+
53+
// Replace placeholder with HTML content
54+
List<BlockBase> newContent = new List<BlockBase>();
55+
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(templateFlowDocument);
56+
57+
foreach (Section section in htmlFlowDocument.Sections)
58+
{
59+
Section clonedSection = section.Clone(templateFlowDocument);
60+
newContent.AddRange(clonedSection.Blocks);
61+
}
62+
63+
editor.ReplaceText("<<EXSUSection>>", newContent, true, false);
64+
65+
// Export the modified document
66+
string outputFilePath = "output.docx";
67+
File.Delete(outputFilePath);
68+
using (Stream output = File.OpenWrite(outputFilePath))
69+
{
70+
docxProvider.Export(templateFlowDocument, output, TimeSpan.FromSeconds(10));
71+
}
72+
73+
// Open the output file
74+
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
75+
```
76+
77+
### Notes:
78+
- Replace `<<EXSUSection>>` with your placeholder text.
79+
- Modify the code to suit your template and requirements.
80+
- Ensure the provided HTML content is [supported]({%slug radwordsprocessing-formats-and-conversion-html-supported-elements%}) by HtmlFormatProvider.
81+
82+
## See Also
83+
84+
- [HtmlFormatProvider]({%slug radwordsprocessing-formats-and-conversion-html-htmlformatprovider%})
85+
- [DocxFormatProvider]({%slug radwordsprocessing-formats-and-conversion-docx-docxformatprovider%})
86+
- [Mail Merge Documentation]({%slug radwordsprocessing-editing-mail-merge%})
87+
- [Find-and-Replace]({%slug radwordsprocessing-editing-find-and-replace%})

libraries/radwordsprocessing/editing/find-and-replace/find-and-replace-text.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,4 @@ __RadFlowDocumentEditor__ gives you the ability to format all occurrences of a s
9999
* [RadFlowDocumentEditor]({%slug radwordsprocessing-editing-radflowdocumenteditor%})
100100
* [CharacterProperties]({%slug radwordsprocessing-concepts-style-properties%})
101101
* [RadFlowDocument]({%slug radwordsprocessing-model-radflowdocument%})
102-
* [RadFlowDocumentEditor API Reference](https://docs.telerik.com/devtools/document-processing/api/Telerik.Windows.Documents.Flow.Model.Editing.RadFlowDocumentEditor.html)
102+
* [Simulating Mail Merge with HTML content by Utilizing the Find and Replace Functionality]({%slug simulating-mail-merge-with-html-content%})

libraries/radwordsprocessing/editing/mail-merge.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,5 @@ If you want to separate the items into several rows you need to close the group
227227
* [Populate a Table with Data using Nested Mail Merge Functionality]({%slug populate-table-data-mail-merge%})
228228
* [Generating a Word Document Template with Data Using MailMerge in RadWordsProcessing]({%slug generate-doc-template-and-populate-with-collection-data-mail-merge%})
229229
* [How to Remove a MERGEFIELD While Replacing the Placeholders with Values in RadWordsProcessing]({%slug remove-mergefields-retain-values-radwordsprocessing%})
230-
* [Performing Nested MailMerge with Multiple Levels in RadWordsProcessing]({%slug nested-mailmerge-radwordsprocessing%})
230+
* [Performing Nested MailMerge with Multiple Levels in RadWordsProcessing]({%slug nested-mailmerge-radwordsprocessing%})
231+
* [Simulating Mail Merge with HTML content by Utilizing the Find and Replace Functionality]({%slug simulating-mail-merge-with-html-content%})

0 commit comments

Comments
 (0)