Products Technologies Demo Docs Blog Support Company

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 article shows how to create documents with QR codes and barcodes in .NET C# using TX Text Control .NET Server.

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

QR codes and barcodes are great for adding value to documents and PDF files by providing easy access to information, authenticating information, tracking items and improving interactivity. A barcode can bridge the gap between paper or digital human-readable documents and digital information in web portals or web applications. Most users are familiar with QR codes and barcodes, which are ubiquitous in many processes such as:

  • Product packaging
  • Invoices
  • Inventory management
  • Event ticketing
  • Boarding passes
  • Payment systems

There are industries in which processes are no longer possible without the use of barcodes. This includes healthcare, where patient data can be accessed directly by scanning a wristband worn by a patient, or daily medication doses can be tracked. Or logistics, where orders can be tracked or found in the warehouse by simply scanning an item code on a packing slip or other piece of documentation. In the retail industry, barcodes are used to track inventory, manage stock levels, and speed up the checkout process.

What is a barcode?

A barcode is a machine-readable representation of data in a visual format. It consists of lines of varying widths and spacing that are read by a barcode scanner. A QR code is a type of barcode that can store more information than a traditional barcode. It is a two-dimensional code that can store text, URLs, contact information, and other data.

Most typical barcodes are:

  • QR code

    QR Code

    A QR code is a two-dimensional barcode that can store more information than a traditional barcode. It is commonly used in marketing, advertising, and other applications where more information needs to be stored in a small space.

  • UPC (Universal Product Code)

    UPC Code

    A UPC code is a type of barcode that is commonly used in retail stores to track inventory and sales. It consists of 12 digits that are read by a barcode scanner.

  • EAN (European Article Number)

    EAN Code

    An EAN code is a type of barcode that is commonly used in Europe to track inventory and sales. It consists of 13 digits that are read by a barcode scanner.

  • Code 39

    Code 39

    Code 39 is a type of barcode that is commonly used in logistics and inventory management. It consists of lines of varying widths and spacing that are read by a barcode scanner.

Example: Packing Slip

Suppose you have a packing slip containing a list of items to be shipped to a customer. To track an item in the warehouse or during shipping, each item has a barcode that can be scanned. The barcode can be scanned using a barcode scanner or a mobile device with a barcode scanning application. The barcode contains information about the item, such as the item number, description, and quantity.

Look at the following screenshot of a packing slip containing barcodes in various places, generated using TX Text Control. TX Text Control provides out-of-the-box support for all industry-standard 1D and 2D barcodes.

Packing Slip created with TX Text Control

Generating the Packing Slip

Creating this packing slip is very easy using TX Text Control's MailMerge engine. First, as shown below, we need a data source, such as a JSON object.

[
    {
        "packing_slip_guid": "12345678-1234-1234-1234-123456789012",
        "dispatch_date": "2024-01-01",
        "recipient": {
            "id": 1,
            "name": "John Doe",
            "company": "Company A",
            "address": {
                "street": "123 Main St",
                "city": "Anytown",
                "state": "CA",
                "zip": "12345"
            }
        },
        "product": {
            "product_id": 1,
            "name": "Product 1",
            "qty": "Product 1 Description",
            "code": "123456789012"
        },
        "items": [
            {
                "item_id": 1,
                "name": "Item 1",
                "description": "Description 1",
                "quantity": 1,
                "price": 100,
                "upc": "12345678901"
            },
            {
                "item_id": 2,
                "name": "Item 2",
                "description": "Description 2",
                "quantity": 2,
                "price": 200,
                "upc": "12345678902"
            },
            {
                "item_id": 3,
                "name": "Item 3",
                "description": "Description 3",
                "quantity": 3,
                "price": 300,
                "upc": "12345678903"
            },
            [...]
        ]
    }
]

Next, we need a template document that contains merge fields and barcodes. The merge fields are replaced with data from the data source, and the barcodes are generated using the same data. In the following screenshot you can see the created template in the WYSIWYG document editor of TX Text Control, which can be easily integrated into your application.

Packing Slip created with TX Text Control

The out-of-the-box user interface provides all the functionality needed to insert merge fields, repeating sections, and barcodes.

Packing Slip created with TX Text Control

Barcodes can be mapped to a data source field to update with the correct values during final document generation.

Packing Slip created with TX Text Control

Preparing the Application

A .NET 8 console application is created for the purposes of this demo.

Prerequisites

The following tutorial requires a trial version of TX Text Control .NET Server.

  1. In Visual Studio, create a new Console App using .NET 8.

  2. In the Solution Explorer, select your created project and choose Manage NuGet Packages... from the Project main menu.

    Select Text Control Offline Packages from the Package source drop-down.

    Install the latest versions of the following package:

    • TXTextControl.TextControl.ASP.SDK

    Create PDF

  3. Download the sample template and JSON data from the GitHub repository at the end of this article and add it to your project.

  4. Open the project settings by double-clicking the project node in the Solution Explorer and add the UseWindowsForms property to the PropertyGroup section. Your completed .csproj file should look like this:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0-windows</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <UseWindowsForms>true</UseWindowsForms>
        <Nullable>enable</Nullable>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="TXTextControl.TextControl.ASP.SDK" Version="32.0.3" />
      </ItemGroup>
    
      <ItemGroup>
        <None Update="data.json">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </None>
        <None Update="packing_slip.tx">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </None>
      </ItemGroup>
    
    </Project>

Creating the Document

  1. Open the Program.cs file and add the following code:

    using TXTextControl.DocumentServer;
    
    using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
    {
            tx.Create();
            tx.Load("packing_slip.tx", TXTextControl.StreamType.InternalUnicodeFormat);
    
            MailMerge mailMerge = new MailMerge()
            {
                    TextComponent = tx
            };
    
            string jsonData = System.IO.File.ReadAllText("data.json");
    
            mailMerge.MergeJsonData(jsonData);
    
            tx.Save("output.pdf", TXTextControl.StreamType.AdobePDF);
    }

When running the application, the document is created and saved as output.pdf in the project directory. The barcodes are generated using the data from the JSON data source.

Conclusion

Barcodes are a great way to add value to documents and PDF files by providing easy access to information, authenticating information, tracking items, and improving interactivity. TX Text Control provides out-of-the-box support for all industry-standard 1D and 2D barcodes. The MailMerge class can be used to merge data into a template document and generate a final document with barcodes.

Download the sample project from GitHub and test it with your own data.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • TX Text Control .NET Server 32.0
  • Visual Studio 2022

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.NETASP.NET CoreDocument Automation

Document Automation 101: Leveraging TX Text Control for Business Efficiency…

Document automation is a powerful tool that can help businesses save time and money. In this article, we will explore how to leverage TX Text Control for document automation in ASP.NET and ASP.NET…


ASP.NETASP.NET CoreBarcode

Using QR Codes to Open Documents in Document Portals

This sample application shows how to add QR codes to documents that open the document in a document portal using the DocumentViewer.


ASP.NETASP.NET CorePDF

Adding QR Codes to PDF Documents in C# .NET

This article explains how to add QR codes to PDF documents with the Text Control .NET Server component in C#. It provides the necessary steps and code snippets for effectively implementing this…


ASP.NETASP.NET CoreDOCX

Use MailMerge in .NET on Linux to Generate Pixel-Perfect PDFs from DOCX…

This article explores how to use the TX Text Control MailMerge feature in .NET applications on Linux to generate pixel-perfect PDFs from DOCX templates. This powerful combination enables…


ASP.NETASP.NET CoreContract

Generating Dynamic NDAs Using TX Text Control MailMerge in C# .NET

This article demonstrates how to generate dynamic NDAs using TX Text Control MailMerge in C# .NET. It covers the process of creating a template, binding data, and generating the final document.