Support Center

How to use barcodes in mail merge process

To use a barcode font in mail merge process, we can customize GemBox.Document’s mail merging operation in order to achieve this task.
For example, we can set the required document element’s FontName property to a barcode font inside our FieldMerging event handler. I will use a Free 3 of 9 Extended (FRE3OF9X.ttf) font, which is a TrueType barcode font installed on my machine.

C# code

DocumentModel document = new DocumentModel();
    new Section(document,
        new Paragraph(document,
            new TextBox(document,
                    new HorizontalPosition(HorizontalPositionType.Center, HorizontalPositionAnchor.Margin),
                    new VerticalPosition(VerticalPositionType.Top, VerticalPositionAnchor.Margin),
                    new Size(385, 100)),
                new Paragraph(document,
                    new Field(document, FieldType.MergeField, "BarCodeID"))))));

document.MailMerge.FieldMerging += (sender, e) =>
    if (e.IsValueFound && e.FieldName.Equals("BarCodeID"))
        Run run = (Run)e.Inline;
        run.CharacterFormat.FontName = "Free 3 of 9 Extended";
        run.CharacterFormat.Size = 120;

document.MailMerge.Execute(new { BarCodeID = 11223344 });
document.Save("Barcode Sample.pdf");

VB.NET code

Dim document As New DocumentModel()
document.Sections.Add( _
    New Section(document, _
        New Paragraph(document, _
            New TextBox(document, _
               Layout.Floating( _
                    New HorizontalPosition(HorizontalPositionType.Center, HorizontalPositionAnchor.Margin), _
                    New VerticalPosition(VerticalPositionType.Top, VerticalPositionAnchor.Margin), _
                    New Size(385, 100)), _
                New Paragraph(document, _
                    New Field(document, FieldType.MergeField, "BarCodeID"))))))

AddHandler document.MailMerge.FieldMerging, _
    Sub(sender, e)
        If e.IsValueFound AndAlso e.FieldName.Equals("BarCodeID"Then
            Dim run As Run = DirectCast(e.Inline, Run)
            run.CharacterFormat.FontName = "Free 3 of 9 Extended"
            run.CharacterFormat.Size = 120
        End If
    End Sub

document.MailMerge.Execute(New With {.BarCodeID = 11223344})
document.Save("Barcode Sample.pdf")

The following is a resulting "Barcode Sample.pdf" file:

Barcode Sample.pdf

Also GemBox.Document supports the use of private fonts that can enable us to specify the location where GemBox.Document should look for a font file when saving a document to PDF, XPS or image file formats, printing a document, or converting it to XpsDocument or ImageSource.
See the private font demonstration sample:

1 of 1 people found this page helpful
Subscribe to this article to get an email notification when it is updated.


  • There are no comments.