Merge Ranges

With GemBox.Document you can perform mail merging on a whole document or parts of the document (on so called named merge ranges).

To define a merge range you need a pair of MERGEFIELD elements; one should have MailMerge.RangeStartPrefix and the other should have MailMerge.RangeEndPrefix in its name.

When you're providing a sequence as a data source (like IEnumerable with multiple items or DataTable with multiple rows) the merge range will be duplicated for each record, appended to the document's end and merged with the record's data.

The following example demonstrates how you can perform a mail merge on a named merge range using MERGEFIELD elements with a default RangeStart: and RangeEnd: prefix.

Screenshot of Word file created with merged range
Word document generated from merging named range
Upload your file (Drag file here)
using System;
using System.Data;
using System.Linq;
using GemBox.Document;

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

        int numberOfItems = %NumberOfItems%;

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

        // Create DataTable as a data source for merge range.
        var table = new DataTable() { TableName = "Items" };

        table.Columns.Add("Date", typeof(DateTime));
        table.Columns.Add("Hours", typeof(int));
        table.Columns.Add("Unit", typeof(double));
        table.Columns.Add("Price", typeof(double));

        for (int rowIndex = 1; rowIndex <= numberOfItems; rowIndex++)
        {
            DateTime date = DateTime.Today.AddDays(rowIndex - numberOfItems);
            int hours = rowIndex % 3 + 6;
            double unit = 35.0;
            double price = hours * unit;

            table.Rows.Add(date, hours, unit, price);
        }

        // Execute mail merge process for "Items" merge range.
        document.MailMerge.Execute(table);

        // Execute mail merge process again for "Number", "Date" and "TotalPrice" fields.
        document.MailMerge.Execute(
            new
            {
                Number = 10203,
                Date = DateTime.Now,
                TotalPrice = table.Rows.Cast<DataRow>().Sum(row => (double)row["Price"])
            });

        document.Save("Merged Ranges Output.%OutputFileType%");
    }
}
Imports System
Imports System.Data
Imports System.Linq
Imports GemBox.Document

Module Program

    Sub Main()

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

        Dim numberOfItems As Integer = %NumberOfItems%

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

        ' Create DataTable as a data source for merge range.
        Dim table As New DataTable() With {.TableName = "Items"}

        table.Columns.Add("Date", GetType(DateTime))
        table.Columns.Add("Hours", GetType(Integer))
        table.Columns.Add("Unit", GetType(Double))
        table.Columns.Add("Price", GetType(Double))

        For rowIndex As Integer = 1 To numberOfItems

            Dim [date] As DateTime = DateTime.Today.AddDays(rowIndex - numberOfItems)
            Dim hours As Integer = rowIndex Mod 3 + 6
            Dim unit As Double = 35.0
            Dim price As Double = hours * unit

            table.Rows.Add([date], hours, unit, price)

        Next

        ' Execute mail merge process for "Items" merge range.
        document.MailMerge.Execute(table)

        ' Execute mail merge process again for "Number", "Date" and "TotalPrice" fields.
        document.MailMerge.Execute(
            New With
            {
                .Number = 10203,
                .Date = DateTime.Now,
                .TotalPrice = table.Rows.Cast(Of DataRow)().Sum(Function(row) CDbl(row("Price")))
            })

        document.Save("Merged Ranges Output.%OutputFileType%")

    End Sub
End Module

You may notice that the above example calls the MailMerge.Execute method multiple times. It is safe to chain mail merge operations (perform multiple successive mail merges) on the same document.

In that case, you'll probably want to specify the Clear Options only for the last mail merge operation.

Check next example or download examples from GitHub.