Generate Barcodes and QR Codes in C# and VB.NET

With GemBox.Document you can insert Barcodes and QR Codes into the document by using the DisplayBarcode field.

Currently, GemBox.Document supports the following barcode types:

  • QR Code
  • Code 39 (also called Code 3 of 9)
  • Code 128
  • EAN 13 / JAN 13
  • EAN 8 / JAN 8
  • UPCA
  • ITF14
  • NW7 (Codabar)

The following example shows how you can insert a QR Code into a document by using a Field element and save the document to PDF or other file formats.

QR Code in C# and VB.NET
Screenshot of QR Code
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();

        var qrCodeValue = "%BarcodeValue%";
        var qrCodeField = new Field(document, FieldType.DisplayBarcode, $"{qrCodeValue} QR");

        document.Sections.Add(
            new Section(document,
                new Paragraph(document, qrCodeField)));

        document.Save("QR Code 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()

        Dim qrCodeValue = "%BarcodeValue%"
        Dim qrCodeField = New Field(document, FieldType.DisplayBarcode, qrCodeValue + " QR")

        document.Sections.Add(
            New Section(document,
                New Paragraph(document, qrCodeField)))

        document.Save("QR Code Output.%OutputFileType%")

    End Sub
End Module

You can customize the resulting barcode with the following field switches:

  • \h - the height of the barcode in twips.
  • \s - the scaling factor of the QR Code.
  • \q - the error correction level of the QR Code.
  • \f - the foreground color of the barcode.
  • \b - the background color of the barcode.
  • \t - displays the barcode text along with the image.
  • \d - add start/stop characters (valid for Code39 and NW7)

You can find more information about DisplayBarcode switches in the official documentation

The following example shows how you can use a simple utility method to create various barcodes with advanced options:

Barcodes in C# and VB.NET
Screenshot of Barcodes
using System.Text;
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();

        var ean13 = CreateBarcodeField(
            document,
            barcodeType: "EAN13",
            barcodeValue: "5901234123457",
            heightInPoints: 100,
            showLabel: true);

        var upca = CreateBarcodeField(
            document,
            barcodeType: "UPCA",
            barcodeValue: "123456789104",
            showLabel: true);

        var code128 = CreateBarcodeField(
            document,
            barcodeType: "Code128",
            barcodeValue: "012345678",
            foregroundColor:"0x2572FF",
            backgroundColor:"0xffb225");


        document.Sections.Add(
            new Section(document,
                new Paragraph(document, "EAN13 Code:"),
                new Paragraph(document, ean13),
                new Paragraph(document, "UPCA Code:"),
                new Paragraph(document, upca),
                new Paragraph(document, "Code 128:"),
                new Paragraph(document, code128)));

        document.Save("Barcodes.%OutputFileType%");
    }

    private static Field CreateBarcodeField(DocumentModel document, string barcodeType, string barcodeValue,
        int? heightInPoints = null, string foregroundColor = null, string backgroundColor = null, bool showLabel = false)
    {
        var instructionText = new StringBuilder();
        instructionText.Append(barcodeValue);
        instructionText.Append(' ').Append(barcodeType);
        if (heightInPoints.HasValue)
            instructionText.Append(" \\h ").Append(LengthUnitConverter.Convert(heightInPoints.Value, LengthUnit.Point, LengthUnit.Twip));
        if (foregroundColor != null)
            instructionText.Append(" \\f ").Append(foregroundColor);
        if (backgroundColor != null)
            instructionText.Append(" \\b ").Append(backgroundColor);
        if (showLabel)
            instructionText.Append(" \\t");

        return new Field(document, FieldType.DisplayBarcode, instructionText.ToString());
    }
}
Imports System.Text
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()

        Dim ean13 = CreateBarcodeField(
            document,
            barcodeType:="EAN13",
            barcodeValue:="5901234123457",
            heightInPoints:=100,
            showLabel:=True)

        Dim upca = CreateBarcodeField(
            document,
            barcodeType:="UPCA",
            barcodeValue:="123456789104",
            showLabel:=True)

        Dim code128 = CreateBarcodeField(
            document,
            barcodeType:="Code128",
            barcodeValue:="012345678",
            foregroundColor:="0x2572FF",
            backgroundColor:="0xffb225")

        document.Sections.Add(
            New Section(document,
                New Paragraph(document, "EAN13:"),
                New Paragraph(document, ean13),
                New Paragraph(document, "UPCA:"),
                New Paragraph(document, upca),
                New Paragraph(document, "Code 128:"),
                New Paragraph(document, code128)))

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

    End Sub

    Private Function CreateBarcodeField(ByVal document As DocumentModel, ByVal barcodeType As String, ByVal barcodeValue As String, ByVal Optional heightInPoints As Integer? = Nothing, ByVal Optional foregroundColor As String = Nothing, ByVal Optional backgroundColor As String = Nothing, ByVal Optional showLabel As Boolean = False) As Field
        Dim instructionText = New StringBuilder()
        instructionText.Append(barcodeValue)
        instructionText.Append(" "c).Append(barcodeType)
        If heightInPoints.HasValue Then
            instructionText.Append(" \h ").Append(LengthUnitConverter.Convert(heightInPoints.Value, LengthUnit.Point, LengthUnit.Twip))
        End If
        If foregroundColor IsNot Nothing Then
            instructionText.Append(" \f ").Append(foregroundColor)
        End If
        If backgroundColor IsNot Nothing Then
            instructionText.Append(" \b ").Append(backgroundColor)
        End If
        If showLabel Then
            instructionText.Append(" \t")
        End If
        Return New Field(document, FieldType.DisplayBarcode, instructionText.ToString())
    End Function

End Module

If you want to create barcode during a mail merge operation you can check out our Barcodes Mail Merge example.

PDF417 Barcode

PDF417 barcode is not supported by the DisplayBarcode field in MS Word, therefore GemBox.Document doesn't have API support for this barcode type.

However, you can use 3rd party libraries in combination with GemBox.Document to create documents with PDF417 barcodes.

ZXing.Net is a popular open source library that is capable of creating PDF417 images. You can add this library as a package by using the Install-Package ZXing.Net command in the NuGet Package Manager Console.

The following example shows how to use ZXing.Net in combination with GemBox.Document to create a PDF file with a PDF417 barcode image.

PDF417 Barcode in C# and VB.NET
Screenshot of PDF417 barcode
using System.Drawing.Imaging;
using System.IO;
using GemBox.Document;
using ZXing;
using ZXing.Common;

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();

        var pdf417Stream = GenerateBarcodeStream("PDF417 barcode content");
        var pdf417Image = new Picture(document, pdf417Stream);

        document.Sections.Add(
            new Section(document,
                new Paragraph(document, "PDF417:"),
                new Paragraph(document, pdf417Image)));

        document.Save("PDF417.pdf");
    }

    // uses ZXing.NET library to save PDF417 image to memory stream
    private static MemoryStream GenerateBarcodeStream(string data)
    {
        var writer = new BarcodeWriter
        {
            Format = BarcodeFormat.PDF_417,
            Options = new EncodingOptions
            {
                Width = 300,
                Height = 150
            }
        };
        var bitmap = writer.Write(data);
        var stream = new MemoryStream();
        bitmap.Save(stream, ImageFormat.Png);
        return stream;
    }
}
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()

        Dim pdf417Stream = GenerateBarcodeStream("PDF417 barcode content")
        Dim pdf417Image = New Picture(document, pdf417Stream)

        document.Sections.Add(
            New Section(document,
                        New Paragraph(document, "PDF417: "),
                        New Paragraph(document, pdf417Image)))

        document.Save("PDF417.pdf")

    End Sub

    ' uses ZXing.NET library to save PDF417 image to memory stream
    Private Function GenerateBarcodeStream(data As String) As MemoryStream
        Dim writer = New BarcodeWriter With {
            .Format = BarcodeFormat.PDF_417,
            .Options = New EncodingOptions With {
                .Width = 300,
                .Height = 150
            }
        }
        Dim bitmap = writer.Write(data)
        Dim stream = New MemoryStream()
        bitmap.Save(stream, ImageFormat.Png)
        Return stream
    End Function
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.