|
| 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 | + |
| 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%}) |
0 commit comments