How to insert a Picture element in mail merge process - Knowledgebase / GemBox.Document - GemBox 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

Helpful Unhelpful

30 of 47 people found this page helpful

Comments(6)

Mario Petit
When I compiled the vb codes, I got the errors. I am using VS 2012 professional.

Error 1 Lambda parameter 'sender' hides a variable in an enclosing block, a previously defined range variable, or an implicitly declared variable in a query expression.
Error 2 Lambda parameter 'e' hides a variable in an enclosing block, a previously defined range variable, or an implicitly declared variable in a query expression.
What am I doing wrong?
Mario - GemBox
Hi,

This error indicates that you have variables named "sender" and "e" somewhere before this:

AddHandler document.MailMerge.FieldMerging, _
Sub(sender, e)

In other words, you have something like the following:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim document = New DocumentModel()
AddHandler document.MailMerge.FieldMerging,
Sub(sender, e)
If e.IsValueFound Then
' ...
End If
End Sub
End Sub

Notice that both "Button1.Click" and "FieldMerging" even handlers are declaring "sender" and "e".
So to resolve this you just need to rename them, for example:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim document = New DocumentModel()
AddHandler document.MailMerge.FieldMerging,
Sub(mergeSender, mergeEventArgs)
If mergeEventArgs.IsValueFound Then
' Here use "mergeEventArgs", note "e" ...
End If
End Sub
End Sub

I hope this helps.

Regards,

Mario
GemBox d.o.o.
Jean-Christophe Bertolini
How to fix the size of the picture ? If the picture is large I would like that the mail merge result get a fixed size to avoid document page modification.
Thanks
Mario - GemBox
Hi,

You can specify any desired size by setting the Picture.Layout.Size property, for example:

var picture = new Picture(e.Document, e.Value.ToString());
picture.Layout.Size = new Size(100, 50, LengthUnit.Pixel);
e.Inline = picture;

Or you can use one of the Picture's constructors that takes the size parameters, for example:

e.Inline = new Picture(e.Document, e.Value.ToString(), 100, 50, LengthUnit.Pixel);

I hope this helps.

Regards,

Mario
GemBox d.o.o.
Gord
Hello Mario,

I am new to GemBox but I have to say this is the best looking app out there that does what this does. Many very good examples, I am impressed. I have a few questions but I will ask one that is related to this topic:

Can you insert images part of the mail merge without having a physical image (base64 string or stream)?

Regards,
Gord
Mario - GemBox
Hi,

Yes, you can insert an image from stream or from base64 string.
For example, in your data source you could have something like the following:

document.MailMerge.Execute(new { SinglePicture = File.ReadAllBytes("Sample.png") });

And in mail merge customization you would have something like the following:

if (e.FieldName == "SinglePicture")
e.Inline = new Picture(e.Document, new MemoryStream((byte[])e.Value));

As you can see, you are able to use whatever data you want in your source.
You just need to handle it appropriately in mail merge customization.

I hope this helps.

Regards,

Mario
GemBox d.o.o.

Add a comment

You need to log in before you can submit a comment.

Need a password reminder?