Products Technologies Demo Docs Blog Support Company

Merging Barcodes with JSON Data in C#

Barcodes are used in various applications to connect the electronic world with a paper document or to gain access to additional information. This article shows how to merge JSON data into barcode placeholders in a document.

Merging Barcodes with JSON Data in C#

Barcodes are used in various applications to connect the electronic world with a paper document or to gain access to additional information. TX Text Control supports the following barcode types:

  • 14 one-dimensional barcodes including Code 39 and EAN.
  • 6 two-dimensional barcodes including QR, DataMatrix and PDF417.

Adding Barcodes Programmatically

The following code uses a ServerTextControl object to insert QR code into a new document.

// new non-UI ServerTextControl
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {

    tx.Create();

    // create a new Barcode object
    TXTextControl.Barcode.TXBarcodeControl txBarcode1 =
        new TXTextControl.Barcode.TXBarcodeControl();

    var sBarcodeText = "www.textcontrol.com";

    txBarcode1.BarcodeType =
            TXTextControl.Barcode.BarcodeType.QRCode;
    txBarcode1.UpperTextLength = sBarcodeText.Length;
    txBarcode1.Text = sBarcodeText;
    txBarcode1.Name = "uniqueId";

    // create a Barcode frame object
    TXTextControl.DataVisualization.BarcodeFrame bf =
            new TXTextControl.DataVisualization.BarcodeFrame(txBarcode1);

    // add the frame with the Barcode to the document
    tx.Barcodes.Add(bf, -1);
}

The integrated barcodes have been designed for document processing purposes. For these requirements, it is essential that the barcode object doesn't displace other objects or text in the document. Therefore, the size must be fixed and the inner content should be resized instead.

This is controlled by the UpperTextLength property. This property defines the maximum number of characters. If the maximum number of characters is encrypted, it fills the complete space of the object. If less characters are encrypted, the barcode size doesn't change, but the content is getting smaller.

Merge Data into Barcodes

Any barcode can be dynamically filled with data during the merging process of the MailMerge class. The Name property of a barcode is used in the same way like merge fields to merge data. In the above code snippet, the Name property is set in line 16. Now, consider the following JSON merge data:

[
  {
    "name":"Product A",
    "uniqueId":"012345678901",
    "price":123.12
  },
  {
    "name":"Product B",
    "uniqueId":"012345678123",
    "price":223.00
  }
]

In a template, the data field uniqueId is bound to the barcode placeholder object:

Creating barcodes with TX Text Control

The following code merges JSON data into a template that contains the barcode placeholder:

// new non-UI ServerTextControl
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {

    tx.Create();

    // loda a template
    tx.Load("App_Data/template.tx", TXTextControl.StreamType.InternalUnicodeFormat);

    // load data from file
    var sData = System.IO.File.ReadAllText("App_Data/data.json");

    // merge data
    using (TXTextControl.DocumentServer.MailMerge mm = 
           new TXTextControl.DocumentServer.MailMerge()) {
        mm.TextComponent = tx;
        mm.MergeJsonData(sData);
    }

    // save the document into a byte array
    byte[] document;
    tx.Save(out document, TXTextControl.BinaryStreamType.AdobePDF);
}

The following screenshot shows the resulting PDF with the merged QR Code:

Creating barcodes with TX Text Control

See this live in one of our online demos:

Live Demos

Or test this on your own and download the fully-functional trial version.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETBlazorASP.NET Core

TX Text Control for Blazor: Mail Merge Integration Tutorial

This tutorial shows how to integrate the TX Text Control MailMerge component into a Blazor application using the TX Text Control .NET Server.


ASP.NETASP.NET CoreMail Merge

Mail Merge: Skipping Records During the Merge Process in .NET C#

This article shows how to skip records during the merge process in .NET C# using the MailMerge class of the Text Control Reporting engine.


ASP.NETASP.NET CoreMail Merge

Mail Merge MS Word DOCX Documents and Convert to PDF in .NET C#

This article shows how to merge data into MS Word DOCX documents and convert them to PDF using TX Text Control .NET Server.


ASP.NETASP.NET CoreMail Merge

Merging Self-Calculating Business Objects with TX Text Control MailMerge in C#

This article shows how to merge self-calculating business objects with TX Text Control MailMerge in C#. The sample shows how to merge a list of products with a total sum.


ASP.NETASP.NET CoreBarcode

Enhancing Documents with QR Codes and Barcodes in .NET C#: A Comprehensive Guide

QR codes and barcodes can be highly beneficial on various documents or PDFs, providing a convenient way to access information, verify authenticity, track items, and enhance user interaction. This…