Merge Ranges

With GemBox.Document you can perform mail merging on a whole document or just parts of it, 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 provide 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 and merged with the record's data.

The following example shows how to perform a mail merge on a named merge range using MERGEFIELD elements with default "RangeStart:" and "RangeEnd:" prefixes.

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

class Program
{
    static void Main()
    {
        // If using the 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 GemBox.Document
Imports System
Imports System.Data
Imports System.Linq

Module Program

    Sub Main()

        ' If using the 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 example above 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.

See also


Next steps

GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API. How about testing it today?

Download Buy