C++ .H Header File Barcode Font Encoder

The C++ .H Header File is a Barcode Generator for use with Barcode Fonts including QR Code, Data Matrix, DataBar, Code 128, ITF, UPC, EAN, Code 39, and others.The C++ .H Header File is a Barcode Generator for use with Barcode Fonts including QR Code, Data Matrix, Code 128, ITF, UPC, EAN, Code 39, and others.

  • Adds barcode generation capability to C++ apps with a single included .h header file.
  • The method declaration and implementation are in the header file, so no .cpp file is necessary.
  • Supports UTF-8 Unicode for Data Matrix and QR Code.
  • QR Code supports the Swiss QR-Bill with Center OverrideGS1 QR Code, and TLV (Tag-Length-Value) encoding in Base64.
  • Complete unencrypted source code is included with the product.
  • Provided with any Developer License of an applicable font package.

The C++ .H files are provided with all applicable Developer Licenses. To understand implementation or for testing purposes, refer to the example project provided in the Developer Tools file. The header file is a font encoder that formats data to IDAutomation's barcode fonts. It returns a string that when viewed or printed with the applicable barcode font creates a correct barcode symbol.

Implementation is a 2-step process:

1.
Open the downloaded ZIP file and Install the Barcode Font.

2.
Implement the .h header file in your project and add an include statement such as #include "IDAutomationQRCode.h" to the project.

#include "IDAutomationQRCode.h"

NOTE: If your barcode is something other than QR Code, you will need to include the appropriate .h file for that barcode type and not the QRCode.h file in addition to calling the functions for that barcode type. QR Code is used as an example for the tutorial.

Format data from a text string to the font by accessing the function, which is viewable in the top portion of the .h file:

string result = IDAutomation_QRCodeFontEncode(DataToEncode);

When encoding UTF-8 Unicode characters for Data Matrix and QR Code, be sure to include u8 before the string. For example:

DataToEncode = u8"条形码字体";

If issues are experienced, use a process of elimination to identify the problem by comparing the example project with your project. Refer to the top of the .h file to view the function declarations that are supported.

Specific Implementation Examples

Data Matrix

The following is an implementation example that will display the encoded text at the command prompt:

#include <string>
#include <iostream> 
#include "IDAutomationDataMatrix.h"
 
int main() {
    string DataToEncode = "IDAutomation.com";
    string EncodedData = "";
    int ApplyTilde = 0;
    int EncodingMode = 0;
    int PreferredFormat = 0;
    EncodedData = IDAutomation_DMFontEncode(DataToEncode, ApplyTilde, EncodingMode, PreferredFormat);
    printf("Data Matrix Encoded Data:\n%s\n", EncodedData.c_str());
}

The function declaration is as follows:
string IDAutomation_DMFontEncode(string DataToEncode, int ApplyTilde, int EncodingMode, int PreferredFormat)

PDF417

The PDF417 header file is an unobfuscated C++ source code that makes use of character vectors. It was compiled and tested with Visual Studio 2005 and 2008 and does not contain any OS-specific code.

#include <string>
#include <iostream>
#include "PDF417FontEncoder.h"
 
int main ()
{
     PDF417FontEncoder PDF;
     long ECL = 3;
     PDF.setApplyTilde(true);
     PDF.setErrorCorrectionLevel(ECL);
     PDF.PDF417Mode = PDF417FontEncoder::PDF417Modes::Binary;
     PDF.setDataToEncode("IDAutomation.com");
     printf(PDF.FontEncode().c_str());
}

QR Code

Following is a C++ Implementation example of QR Code:

#include <string>
#include <iostream>
#include <math.h>
#include <fstream>
#include "IDAutomationQRCode.h"
int main ()
{
     QRCodeFontEncoder QR;
     QR.ApplyTilde = true;
     QR.ErrorCorrectionLevel = QR.ECL_M;
     QR.EncodingMode = QR.Enc_Byte;
     QR.Version = QR.V_07;
     QR.DataToEncode = "www.idautomation.com";
     printf(QR.FontEncode().c_str());
}

GS1 Code 128 using UniversalIDAutomation.h File

This tutorial is a C++ console application built in Visual Studio 2022, it gives an example of what the C++ Universal Header file is capable of.

If using a GS1 font with a space in the title, it is recommended to add a '%20' in place of any space in the file; structure the path as 'C:/Path/To/Font/File /IDAutomationGS1%2024.ttf' Take note of where the font is located, for ease in this example it is placed where the html and pdf file will generate.

Once the C++ console application is up, add the UniversalIDAutomation.h file to the project.

At first the UniversalIDAutomation.h file may not be recognized after it is included in the project. Be sure to include it, below is a recommendation should compile errors happen:

  1. In Visual Studio navigate to the project’s properties.
  2. Under “Configuration Properties” select C/C++
  3. Under “General” you should see at the top an “Additional Include Directories”
  4. Select and edit the “Additional Include Directories”
  5. Add a path to the .h file, without included the actual file.
  6. If the .h file is in “C:\Path\To\Header\File\UniversalIDAutomation.h”
  7. You’ll want to include “C:\Path\To\Header\File\”
  8. Once applied, return to your project and the .h file should be findable by the compiler now.

Depending on the environment, it may be recommended to disable the specfific warning for: 4996, under the Project's Properties > C++ > Advanced > Disable Specific Warnings.

Don't forget to add the PDF Generator to the environment. This tutorial uses an open-source option called wkhtmltopdf. Feel free to use an alternative C++ PDF Generator, the code just may need to be adjusted to fit the new generator.

  1. After downloading the pdf converter, add it to the system path.
  2.  Press Win + S, search for: "Environment Variables".
  3. Click: Edit the system environment variables.
  4. Under System variables, find and select Path, then click Edit.
  5. Click New and paste in the path to wkhtmltopdf.exe, for example: C:\Program Files\wkhtmltopdf\bin
  6. Clcik OK to all dialogs and save.
  7. Restart Visual Studio and your terminal to load the new PATH.
  8. Open the cmd and test: wkhtmltopdf -version
  9. The console should display something like: wkhtmltopdf 0.12.6 (with patched qt)
  10. Time to add the code to the console application.
  11. Erase all the code in the main application and paste below:
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
#include "UniversalIDAutomation.h" // contains the Linear functions

using namespace std;

string encodeCode128ToHTML(const string& DataToEncode) {
    std::string htmlHeader = R"html(
<html>
<head>
  <meta charset="UTF-8">
  <title>GS1-128 Barcode Test</title>
<style>
@font-face{
font-family: 'IDAutomationGS1 24';
src: url('file:///C:/Path/To/Font/File/IDAutomationGS1%2024.ttf') format('truetype');
}
.barcode{
font-family: 'IDAutomationGS1 24';
font-size: 3rem;
}
</style>
</head>
<body>
  <div class="barcode">
)html";

    std::string htmlFooter = R"html(
  </div>
</body>
</html>
)html";

    // Buffer and size setup
    char output[1024] = { 0 };
    long size = sizeof(output);
    long applyTilde = 1;

    // Encode with Code128
    long result = IDAutomation_Universal_C128((char*)DataToEncode.c_str(), applyTilde, output, size);
     

    if (result != 0) {
        return "<p>Error encoding GS1-128. Error code: " + to_string(result) + "</p>";
    }

    // Replace carriage returns with <br>
    string encodedStr = std::regex_replace(string(output), std::regex("\r"), "<br>");

    // Combine with HTML structure
    return htmlHeader + encodedStr + htmlFooter;
}

int main() {
    string input = "~202(00)0000801234999999999~m17"; // GS1-128 input string (SSCC-18)
    string htmlOutput = encodeCode128ToHTML(input);

    ofstream fw("GS1_128_Test.html", ios::out);
    if (fw.is_open()) {
        fw << htmlOutput;
        fw.close();
        cout << "HTML file 'GS1_128_Test.html' created successfully.\n";
    }
    else {
        cerr << "Failed to write HTML output.\n";
        return -1;
    }
    int resultCode = system("wkhtmltopdf --enable-local-file-access GS1_128_Test.html GS1_128_Output.pdf");

    if (resultCode == 0) {
        cout << "PDF generated successfully as 'GS1_128_Output.pdf'.\n";
    }
    else {
        cerr << "Failed to generate PDF. Error code: " << resultCode << endl;
    }
    return 0;
}


  • Includes standard C++ libraries for input/output, string manipulation, and regex, plus a custom barcode encoder header (UniversalIDAutomation.h).
  • Defines a function encodeCode128ToHTML that builds an HTML document embedding a barcode using a local TrueType font (IDAutomationGS1 24.ttf) via a @font-face rule.
  • Initializes a character buffer and size, then calls IDAutomation_Universal_128 to encode a GS1-128 input string into barcode-compatible text.
  • Checks if the encoding was successful; if not, returns an HTML-formatted error message.
  • Replaces carriage return characters (\r) in the output with <br> tags so multiline barcode text formats correctly in HTML.
  • Returns a full HTML string with the barcode text wrapped in a styled <div> using the embedded barcode font.
  • In main(), sets a sample GS1-128 input string (e.g., SSCC-18 format), calls the encoding function, and writes the resulting HTML to GS1_128_Test.html.
  • Executes wkhtmltopdf with --enable-local-file-access to convert the HTML to a PDF (GS1_128_Output.pdf), preserving the embedded barcode font.
  • Logs success or failure messages for HTML creation and PDF generation to the console.