Merge Pictures in Word Documents Using C# and VB.NET

GemBox.Document offers three ways to merge pictures in Word documents programmatically:

The example below shows how to merge pictures using MERGEFIELD elements with a default "Picture:" prefix.

Merge pictures in C# and VB.NET
Screenshot of merged pictures
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 templates for a merged picture. Properties 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 switches.

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

The following example shows how to merge pictures using template shapes and picture field switches.

Merge pictures with templates in C# and VB.NET
Screenshot of merged placeholder pictures
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

Visit the Mail Merge - Picture mail merge to read more about picture merging.

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Document yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.