Merge Pictures

GemBox Document offers three ways how to merge pictures:

  • using a merge field with a special prefix in the name of a picture,
  • using an IncludePicture field with a nested merge field,
  • customizing the mail merge operation by handling a FieldMerging event.

Go to the Mail merge help page for more information on picture merging.

This example shows the merging of pictures using a merge field with a default prefix of Picture:.

Merge Ranges Screenshot
Upload your file (Drag file here)
using System;
using System.IO;
using GemBox.Document;

class Program
{
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var document = DocumentModel.Load("%InputFileName%");

        // Uses all supported picture merging data source types.
        var dataSource = new
        {
            Field1 = "%#Penguins.png%",
            Field2 = File.OpenRead("%#Penguins.png%"),
            Field3 = File.ReadAllBytes("%#Penguins.png%"),
        };

        // Other picture properties that are not taken from the template shape 
        // can be set via FieldMerging event.
        document.MailMerge.FieldMerging += (sender, e) =>
        {
            if (e.Inline is Picture picture)
                picture.Metadata.Description = "Three dancing penguins.";
        };

        document.MailMerge.Execute(dataSource);

        document.Save("Merge Pictures.%OutputFileType%");
    }
}
Imports System
Imports System.IO
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document As DocumentModel = DocumentModel.Load("%InputFileName%")

        ' Uses all supported picture merging data source types.
        Dim dataSource = New With {
            .Field1 = "%#Penguins.png%",
            .Field2 = File.OpenRead("%#Penguins.png%"),
            .Field3 = File.ReadAllBytes("%#Penguins.png%")
        }

        ' Other picture properties that are not taken from the template shape 
        ' can be set via FieldMerging event.
        AddHandler document.MailMerge.FieldMerging,
            Sub(sender, e)
                If TypeOf e.Inline Is Picture Then
                    DirectCast(e.Inline, Picture).Metadata.Description = "Three dancing penguins."
                End If
            End Sub

        document.MailMerge.Execute(dataSource)

        document.Save("Merge Pictures.%OutputFileType%")

    End Sub

End Module

Check next example or download examples from GitHub.