Merge Pictures

GemBox Document offers three ways how to merge pictures:

The following example demonstrates how you can merge pictures using MERGEFIELD elements with a default Picture: prefix.

Screenshot of merged pictures
Merge pictures in C# and VB.NET
Upload your file (Drag file here)
using System;
using System.IO;
using GemBox.Document;

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

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

        // Using picture's path, stream and byte array as a data source.
        var dataSource = new
        {
            Img1 = "%#Penguins.png%",
            Img2 = File.OpenRead("%#Penguins.png%"),
            Img3 = File.ReadAllBytes("%#Penguins.png%"),
        };

        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 = DocumentModel.Load("%InputFileName%")

        ' Using picture's path, stream and byte array as a data source.
        Dim dataSource = New With
        {
            .Img1 = "%#Penguins.png%",
            .Img2 = File.OpenRead("%#Penguins.png%"),
            .Img3 = File.ReadAllBytes("%#Penguins.png%")
        }

        document.MailMerge.Execute(dataSource)

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

    End Sub
End Module

Picture field with template

GemBox.Document supports Picture, TextBox and Shape as a template shape for a merged picture.

Properties that are specified in a template shape, like size and position, are copied to a merged picture.

Also, you can keep the aspect ratio of the picture that's being merged, by using the picture field switch(s).

  • \x - resize resulting picture horizontally, keep the template shape's height.
  • \y - resize resulting picture vertically, keep the template shape's width.
  • \x \y - resize resulting picture either horizontally or vertically.

The following example demonstrates how you can merge pictures using template shapes and picture field switches.

Screenshot of merged placeholder pictures
Merge pictures with templates in C# and VB.NET
Upload your file (Drag file here)
using System;
using System.IO;
using GemBox.Document;

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

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

        var dataSource = new
        {
            Pic1 = "%#Penguins.png%",
            Pic2 = "%#Penguins.png%",
            Pic3 = "%#Penguins.png%"
        };

        // You can use FieldMerging event to set picture properties
        // that were not specified in template placeholder.
        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 With Templates.%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 = DocumentModel.Load("%InputFileName%")

        Dim dataSource = New With
        {
            .Pic1 = "%#Penguins.png%",
            .Pic2 = "%#Penguins.png%",
            .Pic3 = "%#Penguins.png%"
        }

        ' You can use FieldMerging event to set picture properties
        ' that were not specified in template placeholder.
        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 With Templates.%OutputFileType%")

    End Sub
End Module

To read more about picture merging, visit the Mail Merge - Picture mail merge help page.

Check next example or download examples from GitHub.