Generate Barcodes and QR Codes in C# and VB.NET
With the GemBox.Document Word library, you can:
- Generate and insert QR codes into various types of documents (DOCX, PDF, HTML, etc).
- Export a QR code into PNG, JPEG, GIF and other image formats.
- Create custom barcodes by using the advanced options (height, scaling, colours, etc).
- Use 3rd party libraries to create barcodes that are not supported by Microsoft Word.
Supported barcode types
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)
Generating a QR code
The first example below shows how you can use GemBox.Document to create and insert a QR Code into a document using a Field element and save the document to PDF or other file types in C# and VB.NET:
using GemBox.Document;
class Program
{
static void Main()
{
// If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
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 the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Dim document As New DocumentModel()
Dim qrCodeValue = "%BarcodeValue%"
Dim qrCodeField As 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

Creating different barcodes in C# and VB.NET
The following example shows how you can create different barcode types:
using GemBox.Document;
using System.Collections.Generic;
class Program
{
static void Main()
{
// If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
var document = new DocumentModel();
var barcodeType = "%BarcodeType%";
var barcodeValues = new Dictionary<string, string>()
{
["CODE39"] = "GEMBOX123",
["CODE128"] = "GemBox-Document-123",
["EAN8"] = "9638507",
["EAN13"] = "490123456789",
["JAN8"] = "9638507",
["JAN13"] = "490123456789",
["UPCA"] = "036000291452",
["NW7"] = "123456"
};
var barcodeValue = barcodeValues[barcodeType];
var barcodeField = new Field(document, FieldType.DisplayBarcode, $"{barcodeValue} {barcodeType}");
document.Sections.Add(
new Section(document,
new Paragraph(document, $"Barcode '{barcodeType}' with value '{barcodeValue}':"),
new Paragraph(document, barcodeField)));
document.Save("Barcode Output.%OutputFileType%");
}
}
Imports GemBox.Document
Imports System.Collections.Generic
Module Program
Sub Main()
' If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Dim document As New DocumentModel()
Dim barcodeType = "%BarcodeType%"
Dim barcodeValues As New Dictionary(Of String, String)() From
{
{"CODE39", "GEMBOX123"},
{"CODE128", "GemBox-Document-123"},
{"EAN8", "9638507"},
{"EAN13", "490123456789"},
{"JAN8", "9638507"},
{"JAN13", "490123456789"},
{"UPCA", "036000291452"},
{"NW7", "123456"}
}
Dim barcodeValue = barcodeValues(barcodeType)
Dim barcodeField As New Field(document, FieldType.DisplayBarcode, $"{barcodeValue} {barcodeType}")
document.Sections.Add(
New Section(document,
New Paragraph(document, $"Barcode '{barcodeType}' with value '{barcodeValue}':"),
New Paragraph(document, barcodeField)))
document.Save("Barcode Output.%OutputFileType%")
End Sub
End Module

Different barcode types require different value formats. For example, Code 39 supports uppercase letters, digits, and a limited set of special characters, while UPC-A requires 12 numeric digits with a valid check digit.
Customize barcode appearance
The following example shows how you can customize a barcode by using the DISPLAYBARCODE field switches. You can change options such as barcode size, foreground color, background color, and displayed text.
using GemBox.Document;
class Program
{
static void Main()
{
// If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
var document = new DocumentModel();
var foregroundSwitch = @"\f %ForegroundColor%";
var backgroundSwitch = @"\b %BackgroundColor%";
var showTextSwitch = @"\t";
var heightSwitch = @"\h 3000";
var barcodeSwitches = $" {foregroundSwitch} {backgroundSwitch} {showTextSwitch} {heightSwitch}";
var ean13 = new Field(document, FieldType.DisplayBarcode, "5901234123457 EAN13" + barcodeSwitches);
var upca = new Field(document, FieldType.DisplayBarcode, "123456789104 UPCA" + barcodeSwitches);
var code128 = new Field(document, FieldType.DisplayBarcode, "012345678 CODE128" + barcodeSwitches);
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, "CODE128:"),
new Paragraph(document, code128)));
document.Save("Formatted Barcodes.%OutputFileType%");
}
}
Imports GemBox.Document
Module Program
Sub Main()
' If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Dim document As New DocumentModel()
Dim foregroundSwitch = "\f %ForegroundColor%"
Dim backgroundSwitch = "\b %BackgroundColor%"
Dim showTextSwitch = "\t"
Dim heightSwitch = "\h 3000"
Dim barcodeSwitches = $" {foregroundSwitch} {backgroundSwitch} {showTextSwitch} {heightSwitch}"
Dim ean13 = New Field(document, FieldType.DisplayBarcode, "5901234123457 EAN13" & barcodeSwitches)
Dim upca = New Field(document, FieldType.DisplayBarcode, "123456789104 UPCA" & barcodeSwitches)
Dim code128 = New Field(document, FieldType.DisplayBarcode, "012345678 CODE128" & barcodeSwitches)
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, "CODE128:"),
New Paragraph(document, code128)))
document.Save("Formatted Barcodes.%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. MS Word interprets colors in the BGR format, so the least significant bytes represent the red color component and the most significant bytes represent the blue color component. For example, 0x0000FF represents the red color instead of blue.
- \b - the background color of the barcode. Similar to the foreground color, it is interpreted in the BGR format.
- \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.
If you want to create a barcode during a mail merge operation you can check out our Barcodes Mail Merge example.
Creating unsupported barcodes
Not all barcode types are supported by the DisplayBarcode field in MS Word. However, you can use 3rd party libraries in combination with GemBox.Document to create documents with unsupported barcode types like PDF417.
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 following commands in the NuGet Package Manager Console:
Install-Package ZXing.NetInstall-Package ZXing.Net.Bindings.SkiaSharp
The following example shows how to use ZXing.Net in combination with GemBox.Document to create a PDF file with a PDF417 barcode image.
using GemBox.Document;
using SkiaSharp;
using System.IO;
using ZXing;
using ZXing.Common;
class Program
{
static void Main()
{
// If using the 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.
static MemoryStream GenerateBarcodeStream(string data)
{
var writer = new ZXing.SkiaSharp.BarcodeWriter()
{
Format = BarcodeFormat.PDF_417,
Options = new EncodingOptions()
{
Width = 300,
Height = 150
}
};
var stream = new MemoryStream();
using (var bitmap = writer.Write(data))
using (var bitmapData = bitmap.Encode(SKEncodedImageFormat.Png, 100))
{
bitmapData.SaveTo(stream);
stream.Position = 0;
return stream;
}
}
}
Imports GemBox.Document
Imports SkiaSharp
Imports System.IO
Imports ZXing
Imports ZXing.Common
Module Program
Sub Main()
' If using the 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 As 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.
Function GenerateBarcodeStream(ByVal data As String) As MemoryStream
Dim writer = New ZXing.SkiaSharp.BarcodeWriter() With
{
.Format = BarcodeFormat.PDF_417,
.Options = New EncodingOptions() With
{
.Width = 300,
.Height = 150
}
}
Dim stream = New MemoryStream()
Using bitmap = writer.Write(data)
Using bitmapData = bitmap.Encode(SKEncodedImageFormat.Png, 100)
bitmapData.SaveTo(stream)
stream.Position = 0
Return stream
End Using
End Using
End Function
End Module


