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
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.
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
staticvoidMain(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);
}
publicclassMailMergeRecord
{
publicMailMergeRecord()
{ }
publicstring Name { get; set; }
publicstring 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