Merge Labels in Word Documents

With GemBox.Document you can create mailing labels in Word file using the mail merge operation.

To create a template document with desired labels layout, use the Mailings -> Labels options from Microsoft Word. In the template document, use the Next field to indicate when the provided data source should move to the next record.

The following example shows how to create labels by executing a mail merge process with a data source that contains multiple records.

Word document with merged mailing labels
Screenshot of a Word file with merged labels
Upload your file (Drag file here)
using GemBox.Document;
using GemBox.Document.MailMerging;
using System.Linq;

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

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

        // Create data source with multiple records.
        var source = Enumerable.Range(1, numberOfLabels).Select(
            i => new { Name = "Person " + i, Company = "Company " + i });

        // Specify clear options to remove unmerged fields.
        document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveUnusedFields;

        // Execute mail merge process.
        document.MailMerge.Execute(source);

        document.Save("MergeLabelsOutput.%OutputFileType%");
    }
}
Imports GemBox.Document
Imports GemBox.Document.MailMerging
Imports System.Linq

Module Program

    Sub Main()

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

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

        ' Create data source with multiple records.
        Dim source = Enumerable.Range(1, numberOfLabels).Select(
            Function(i) New With {.Name = "Person " & i, .Company = "Company " & i})

        ' Specify clear options to remove unmerged fields.
        document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveUnusedFields

        ' Execute mail merge process.
        document.MailMerge.Execute(source)

        document.Save("MergeLabelsOutput.%OutputFileType%")

    End Sub
End Module

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