Merge Barcodes

With GemBox.Document you can import your barcode data using a mail merge operation.

If you have a barcode image, then you can import it using the Merge Pictures example.

If you have a barcode number or text value, then you can import it using a barcode font like Code39 or Code128. Note that when saving a document to PDF, XPS, or an image format, you'll need to provide the barcode font by installing it on the system or setting it as a Private Font.

The following example shows how you can merge barcode data using the MERGEFIELD element formatted with the Code128 barcode font.

Merge barcode in C# and VB.NET
Screenshot of merged barcode
using GemBox.Document;

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

        // Create a new document.
        var document = new DocumentModel();
        document.DefaultParagraphFormat.Alignment = HorizontalAlignment.Center;

        // Create a barcode merge field that will display the value in a barcode font.
        var barcodeField = new Field(document, FieldType.MergeField, "Barcode", "«Barcode»")
        {
            CharacterFormat =
            {
                FontName = "Code 128",
                Size = 80
            }
        };

        // Create a label merge field that will display the value with a '*' character as the prefix and suffix.
        var labelField = new Field(document, FieldType.MergeField, @"Label \b * \f *", "*«Label»*")
        {
            CharacterFormat =
            {
                FontName = "Arial Black",
                Size = 20,
                FontColor = Color.Red
            }
        };

        // Add merge fields to the document.
        document.Sections.Add(
            new Section(document,
                new Paragraph(document, barcodeField),
                new Paragraph(document, labelField)));

        document.MailMerge.Execute(new { Barcode = "%BarcodeValue%", Label = "%BarcodeValue%" });

        document.Save("Barcode Output.%OutputFileType%");
    }
}
Imports GemBox.Document

Module Program

    Sub Main()

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

        ' Create a new document.
        Dim document As New DocumentModel()
        document.DefaultParagraphFormat.Alignment = HorizontalAlignment.Center

        ' Create a barcode merge field that will display the value in a barcode font.
        Dim barcodeField As New Field(document, FieldType.MergeField, "Barcode", "«Barcode»") With
        {
            .CharacterFormat = New CharacterFormat() With
            {
                .FontName = "Code 128",
                .Size = 80
            }
        }

        ' Create a label merge field that will display the value with a '*' character as the prefix and suffix.
        Dim labelField As New Field(document, FieldType.MergeField, "Label \b * \f *", "*«Label»*") With
        {
            .CharacterFormat = New CharacterFormat() With
            {
                .FontName = "Arial Black",
                .Size = 20,
                .FontColor = Color.Red
            }
        }

        ' Add merge fields to the document.
        document.Sections.Add(
            New Section(document,
                New Paragraph(document, barcodeField),
                New Paragraph(document, labelField)))

        document.MailMerge.Execute(New With {.Barcode = "%BarcodeValue%", .Label = "%BarcodeValue%"})

        document.Save("Barcode Output.%OutputFileType%")

    End Sub
End Module

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.