diff --git a/knowledge-base/images/simulating-mail-merge-with-html-content.png b/knowledge-base/images/simulating-mail-merge-with-html-content.png new file mode 100644 index 00000000..23a14e5f Binary files /dev/null and b/knowledge-base/images/simulating-mail-merge-with-html-content.png differ diff --git a/knowledge-base/simulating-mail-merge-with-html-content.md b/knowledge-base/simulating-mail-merge-with-html-content.md new file mode 100644 index 00000000..c0d776df --- /dev/null +++ b/knowledge-base/simulating-mail-merge-with-html-content.md @@ -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 newContent = new List(); +RadFlowDocumentEditor editor = new RadFlowDocumentEditor(templateFlowDocument); + +foreach (Section section in htmlFlowDocument.Sections) +{ + Section clonedSection = section.Clone(templateFlowDocument); + newContent.AddRange(clonedSection.Blocks); +} + +editor.ReplaceText("<>", 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 `<>` 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%}) diff --git a/libraries/radwordsprocessing/editing/find-and-replace/find-and-replace-text.md b/libraries/radwordsprocessing/editing/find-and-replace/find-and-replace-text.md index 536d3d68..1b7e1804 100644 --- a/libraries/radwordsprocessing/editing/find-and-replace/find-and-replace-text.md +++ b/libraries/radwordsprocessing/editing/find-and-replace/find-and-replace-text.md @@ -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%}) diff --git a/libraries/radwordsprocessing/editing/mail-merge.md b/libraries/radwordsprocessing/editing/mail-merge.md index 76c34557..5eb304e0 100644 --- a/libraries/radwordsprocessing/editing/mail-merge.md +++ b/libraries/radwordsprocessing/editing/mail-merge.md @@ -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%})