Inert Images using Mail Merge

1 Answer 23 Views
WordsProcessing
RainMaker
Top achievements
Rank 1
RainMaker asked on 20 Feb 2024, 10:37 PM
I have a DOCX Template that has multiple merge fields defined. One of those fields is for an image. How can I get MailMerge to insert images into a merge field.

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 23 Feb 2024, 04:09 PM

Hi,

Currently, RadWordsProcessing doesn't support MailMerge functionality for images. However, it is a reasonable request. 

I have logged it in our feedback portal by creating a public thread on your behalf. You can track its progress, subscribe for status changes, and add your comments on the following link - feedback item.

I have also updated your Telerik points.

Currently, the possible solution that I can suggest is to use specific text as a placeholder for the image. Then, with the help of the Find and replace functionality you can insert the image. I have prepared a sample code snippet for your reference: Replace Text with Document Elements

        static void Main(string[] args)
        {
            string placeHolder = "image_field";
            RadFlowDocument document = new RadFlowDocument();
            RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
            editor.InsertField("MERGEFIELD Name", "");
            editor.InsertParagraph();
            editor.InsertField("MERGEFIELD Photo", "");


            List<MailMergeRecord> mailMergeDataSource = new List<MailMergeRecord>() { new MailMergeRecord() { Name = "My Name", Photo = placeHolder } };
            RadFlowDocument mailMergeResult = document.MailMerge(mailMergeDataSource);

            editor = new RadFlowDocumentEditor(mailMergeResult);

            // Replace 
            ImageInline imageInline = new ImageInline(mailMergeResult);
            byte[] data = File.ReadAllBytes(@"..\..\ProgressNinjas.png");
            imageInline.Image.ImageSource = new ImageSource(data, "png");
            imageInline.Image.Size = new System.Windows.Size(100, 100);
              
            editor.ReplaceText(placeHolder, imageInline, true, true);
            DocxFormatProvider provider = new DocxFormatProvider();

            string originalFilePath = @"..\..\\original.docx";
            string mergedFilePath = @"..\..\merged.docx";
            File.Delete(originalFilePath);
            File.Delete(mergedFilePath);
            using (Stream output = File.OpenWrite(originalFilePath))
            {
                provider.Export(document, output);
            }
            using (Stream output = File.OpenWrite(mergedFilePath))
            {
                provider.Export(mailMergeResult, output);
            }
            Process.Start(mergedFilePath);
        }
        public class MailMergeRecord
        {
            public MailMergeRecord()
            { }
            public string Name { get; set; }
            public string Photo { get; set; }

        }

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
WordsProcessing
Asked by
RainMaker
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or