Skip to content

Latest commit

 

History

History
85 lines (66 loc) · 3 KB

inserting-images-using-mail-merge-radwordsprocessing.md

File metadata and controls

85 lines (66 loc) · 3 KB
title description type page_title slug tags res_type
Inserting Images using Mail Merge
Learn how to insert images into a merge field using Mail Merge in RadWordsProcessing for Document Processing.
how-to
How to Insert Images using Mail Merge in RadWordsProcessing
inserting-images-using-mail-merge-radwordsprocessing
radwordsprocessing, mail merge, images, insert
kb

Environment

Version Product Author
2024.1.124 RadWordsProcessing Desislava Yordanova

Description

To insert images into a merge field using [Mail Merge]({%slug radwordsprocessing-editing-mail-merge%}) in RadWordsProcessing, follow these steps:

  1. Use specific text as a placeholder for the image in your DOCX template.
  2. Utilize the [Find and Replace]({%slug radwordsprocessing-editing-replace-document-elements%}) functionality to insert the image.

Solution

Here is a sample code snippet that demonstrates how to replace the placeholder text with an image using RadWordsProcessing:

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; }

}

The achieved result is illustrated below:

Image mail merge

See Also

  • [Mail Merge]({%slug radwordsprocessing-editing-mail-merge%})
  • [Hiding MailMerge Line in Output Word Document If Blank]({%slug hide-mailmerge-line-output-word-document-if-blank%})
  • [Find and Replace]({%slug radwordsprocessing-editing-replace-document-elements%})