Support Center

How to insert a Picture element in mail merge process

The GemBox.Document enables you to customize its mail merging operation to achieve your requirements. The following sample will demonstrate some simple ways you can handle a FieldMerging event to import a Picture element in the mail merge execution.

C# code

var document = new DocumentModel();

document.Sections.Add(
    new Section(document,
        new Paragraph(document,
            new Run(document, "Merge a single picture!"),
            new SpecialCharacter(document, SpecialCharacterType.LineBreak),
            new Field(document, FieldType.MergeField, "SinglePicture")),
        new Paragraph(document,
            new Run(document, "Merge multiple pictures!"),
            new SpecialCharacter(document, SpecialCharacterType.LineBreak),
            new Field(document, FieldType.MergeField, "MultiplePictures"))));

document.MailMerge.FieldMerging += (sender, e) =>
{
    if (e.IsValueFound)
    {
        if (e.FieldName == "SinglePicture")
            e.Inline = new Picture(e.Document, e.Value.ToString());

        if (e.FieldName == "MultiplePictures")
        {
            e.Inlines.Clear();
            var source = e.Value as string[];
            foreach (var picturePath in source)
                e.Inlines.Add(new Picture(e.Document, picturePath));
        }
    }
};

document.MailMerge.Execute(new { SinglePicture = "Sample.png" });
document.MailMerge.Execute(new { MultiplePictures = new string[] { "Sample.png", "Sample.png", "Sample.png" } });
document.Save("Merge Picture Sample.docx");

VB.NET code

Dim document = New DocumentModel()

document.Sections.Add(
    New Section(document,
       New Paragraph(document,
            New Run(document, "Merge a single picture!"),
            New SpecialCharacter(document, SpecialCharacterType.LineBreak),
            New Field(document, FieldType.MergeField, "SinglePicture")),
        New Paragraph(document,
            New Run(document, "Merge multiple pictures!"),
            New SpecialCharacter(document, SpecialCharacterType.LineBreak),
            New Field(document, FieldType.MergeField, "MultiplePictures"))))

AddHandler document.MailMerge.FieldMerging, _
    Sub(sender, e)
        If e.IsValueFound Then
            If e.FieldName = "SinglePicture" Then
                e.Inline = New Picture(e.Document, e.Value.ToString())
            End If

            If e.FieldName = "MultiplePictures" Then
                e.Inlines.Clear()
                Dim source = TryCast(e.Value, String())
                For Each picturePath As String In source
                    e.Inlines.Add(New Picture(e.Document, picturePath))
                Next
            End If
        End If
    End Sub

document.MailMerge.Execute(New With {.SinglePicture = "Sample.png"})
document.MailMerge.Execute(New With {.MultiplePictures = New String() {"Sample.png", "Sample.png", "Sample.png"}})
document.Save("Merge Picture Sample.docx")

The following is the resulting "Merge Picture Sample.docx" file:

Merge Picture Sample.docx

3 of 4 people found this page helpful
Subscribe to this article to get an email notification when it is updated.

0 Comments

  • There are no comments.