PDF documents are often the final format for contracts, invoices, certificates, and reports. Although they are static by design, users now expect to be able to interact with them, verify information, and access digital services directly from these files. This is where QR codes become essential.

QR Codes in PDF Documents

A QR code in a PDF document connects printed or digital content to dynamic online experiences. Users can verify a document, open a related website, access a customer portal, or continue a workflow simply by scanning a code. In environments where documents are printed and shared, QR codes ensure that digital connections remain intact.

Adding QR codes can also benefit logistics and document management. For example, delivery slips, purchase orders, and visitor passes can contain embedded information that can be scanned. This allows companies to automate processes and improve tracking. In industries such as healthcare and law, QR codes can verify authenticity and direct users to supplementary instructions without altering the original document's layout.

TX Text Control offers two primary methods for embedding QR codes in PDF documents. You can either add QR codes programmatically using the API or create templates with QR code placeholders and merge them with data from JSON files. Depending on the application, both methods offer flexibility and serve different needs.

Adding QR Codes Programmatically

Using the barcode object in TX Text Control, developers can create and insert QR codes directly into a document. This method provides complete control over the position, size, and content of the QR code.

Prerequisites

You need to download and install the trial version of TX Text Control .NET Server for ASP.NET:

Creating the Application

Make sure that you downloaded the latest version of Visual Studio 2022 that comes with the .NET 8 SDK.

  1. In Visual Studio 2022, create a new project by choosing Create a new project.

  2. Select Console App as the project template and confirm with Next.

  3. Enter a project name and choose a location to save the project. Confirm with Next.

  4. Choose .NET 8.0 (Long Term Support) as the Framework.

  5. Enable the Enable container support checkbox and choose Linux as the Container OS.

  6. Choose Dockerfile for the Container build type option and confirm with Create.

    Creating the .NET 8 project

Adding the NuGet Packages

  1. In the Solution Explorer, select your created project and choose Manage NuGet Packages... from the Project main menu. Select Text Control Offline Packages as the Package source.

    Install the following package:

    • TXTextControl.TextControl.Core.SDK

    ASP.NET Core Web Application

Using ServerTextControl

  1. Find the Program.cs file in the Solution Explorer and replace the code with the following code snippet:

    using TXTextControl;
    using TXTextControl.Barcode;
    using TXTextControl.DataVisualization;
    using System.Drawing;
    // Create a new ServerTextControl instance for server-side document generation
    using var tx = new ServerTextControl();
    // Initialize a new, empty document
    tx.Create();
    // The text to be encoded in the QR code
    var barCodeText = "https://www.textcontrol.com";
    // Create the barcode core with type QR code and set the encoded text
    var barcodeCore = new TXBarcodeCore
    {
    BarcodeType = BarcodeType.QRCode,
    Text = barCodeText,
    UpperTextLength = barCodeText.Length // Optional visual setting
    };
    // Create a frame for the barcode and define its visual size
    var barcodeFrame = new BarcodeFrame(barcodeCore)
    {
    Size = new Size(5000, 5000)
    };
    // Add the barcode to the document at the end (-1 means current input position)
    tx.Barcodes.Add(barcodeFrame, -1);
    // Save the document as a PDF
    tx.Save("results.pdf", StreamType.AdobePDF);
    view raw test.cs hosted with ❤ by GitHub
  2. Now run the project that runs the application in a Docker container.

This creates a PDF document with an embedded QR code pointing to a given website.

Using MailMerge

In many business applications, it is important to separate a document's design from its data. TX Text Control allows templates to contain placeholders for QR codes that are filled with dynamic content during the merge process. Design these templates visually using TX Text Control's editor. Add a QR code placeholder and give it a name.

During document generation, the application loads the template and merges it with a JSON data file. The value in the JSON object is automatically applied to the QR code placeholder. This method is ideal for batch processing tasks such as generating certificates, membership cards, or invoices, where each document requires a unique QR code but maintains the same layout.

Now, let's take a look at the packing slip example shown in the screenshot at the beginning.

QR Codes in PDF Documents

It's very easy to create this packing slip using TX Text Control's MailMerge TX Text Control .NET Server for ASP.NET
DocumentServer Namespace
MailMerge Class
The MailMerge class is a .NET component that can be used to effortlessly merge template documents with database content in .NET projects, such as ASP.NET web applications, web services or Windows services.
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"
},
[...]
]
}
]
view raw test.json hosted with ❤ by GitHub

We need a template document that includes merge fields and barcodes. The merge fields will be populated with data from the data source, and the barcodes will be generated based on that same data. The screenshot below shows the prepared template in TX Text Control's WYSIWYG document editor, which can be seamlessly integrated into your application.

Template with QR Code Placeholder

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

Template with QR Code Placeholder

This code demonstrates how to generate a PDF document by merging dynamic JSON data into a pre-designed template using TX Text Control's MailMerge class.

using TXTextControl;
using TXTextControl.DocumentServer;
using System.IO;
// Create a new ServerTextControl instance for document processing
using var tx = new ServerTextControl();
// Create a new, empty document
tx.Create();
// Load the template document in TX Text Control's internal format
tx.Load("packing_slip.tx", StreamType.InternalUnicodeFormat);
// Initialize the MailMerge component with the loaded document
var mailMerge = new MailMerge
{
TextComponent = tx
};
// Load JSON data from file
var jsonData = File.ReadAllText("data.json");
// Merge the JSON data into the template
mailMerge.MergeJsonData(jsonData);
// Save the merged document as a PDF
tx.Save("output.pdf", StreamType.AdobePDF);
view raw test.cs hosted with ❤ by GitHub

Conclusion

With TX Text Control, adding QR codes to PDF documents is a straightforward process. Whether you generate them programmatically or use MailMerge with templates, the API's flexibility allows you to create dynamic, interactive documents that enhance the user experience and streamline workflows.

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 for ASP.NET.

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