Set Barcode Text

This documentation covers how to set barcode text in the Aspose.BarCode Cloud SDK for Node.js. The Barcode API supports multiple methods to generate barcodes in different formats using GET and POST requests through the GenerateApi. Barcodes can be generated by specifying various parameters such as barcode type, data to encode, image format and display options. The data encoding process involves defining the type of data and the actual content.

GenerateApi

The GenerateApi interface includes methods for generating barcodes:

  • GET Request: Uses parameters in the URL route and query string.
  • POST Request (JSON/XML): Parameters are provided in the request body.
  • POST Request (Multipart Form): Parameters are sent as URL-encoded form data.

Methods

  • generate: Generates a barcode via GET request.
  • generateBody: Generates a barcode via POST request with JSON or XML body.
  • generateMultipart: Generates a barcode via POST request with multipart form data.

Setting Barcode Text

Properties

  • DataType: Specifies the type of data encoding (can be string, base64, or hex). If not set, the data value is used as plain text.
  • data: A string that represents the data to be encoded.

Supported Data Types

Value Description
StringData (Default value) Plain text
Base64Bytes Base64 encoded binary data
HexBytes Hexadecimal encoded binary data

Barcode Generation Example

Below are examples to demonstrate barcode generation using the GenerateApi methods.

Example 1: Generating a Barcode with GET Request

const fs = require('fs');
const path = require('path');
const Barcode = require('aspose-barcode-cloud-node');

function makeConfiguration() {
    const envToken = process.env['TEST_CONFIGURATION_ACCESS_TOKEN'];
    if (!envToken) {
        return new Barcode.Configuration(
            'Client Id from https://dashboard.aspose.cloud/applications',
            'Client Secret from https://dashboard.aspose.cloud/applications',
            null,
            null
        );
    } else {
        return new Barcode.Configuration(
            null,
            null,
            null,
            envToken
        );
    }
}
const config = makeConfiguration();

async function generateBarcode(api, fileName) {
    const getRequest = new Barcode.GenerateRequestWrapper(
        Barcode.EncodeBarcodeType.Qr,
        "Aspose.BarCode.Cloud"
    );
    getRequest.dataType = Barcode.EncodeDataType.StringData;

    const generated = await api.generate(getRequest);

    fs.writeFileSync(fileName, generated.body);
}

const genApi = new Barcode.GenerateApi(config);
const fileName = path.resolve('testdata','Qr.png');

generateBarcode(genApi, fileName)
    .then(() => {
        console.log('Barcode saved to ' + fileName);
    })
    .catch(err => {
        console.error("Error: " + JSON.stringify(err, null, 2));
        process.exitCode = 1;
    });

Result Image is: Result image

Example 2: Generating a Barcode with POST Request (JSON Body)

const fs = require('fs');
const path = require('path');
const Barcode = require('aspose-barcode-cloud-node');

function makeConfiguration() {
    const envToken = process.env['TEST_CONFIGURATION_ACCESS_TOKEN'];
    if (!envToken) {
        return new Barcode.Configuration(
            'Client Id from https://dashboard.aspose.cloud/applications',
            'Client Secret from https://dashboard.aspose.cloud/applications',
            null,
            null
        );
    } else {
        return new Barcode.Configuration(
            null,
            null,
            null,
            envToken
        );
    }
}
const config = makeConfiguration();

async function generateBarcode(api, fileName) {
    const postParams = new Barcode.GenerateParams();
    postParams.barcodeType = Barcode.EncodeBarcodeType.Pdf417;
    postParams.encodeData = new Barcode.EncodeData();
    postParams.encodeData.dataType = Barcode.EncodeDataType.Base64Bytes;
    postParams.encodeData.data = "QXNwb3NlLkJhckNvZGUuQ2xvdWQ=";

    const postRequest = new Barcode.GenerateBodyRequestWrapper(postParams);
    const generated = await api.generateBody(postRequest);

    fs.writeFileSync(fileName, generated.body);
}

const genApi = new Barcode.GenerateApi(config);
const fileName = path.resolve('testdata','Pdf417.png');

generateBarcode(genApi, fileName)
    .then(() => {
        console.log('File \'' + fileName + '\' generated.');
    })
    .catch(err => {
        console.error("Error: " + JSON.stringify(err, null, 2));
        process.exitCode = 1;
    });

Result Image is: Result image

Example 3: Generating a Barcode with POST Request (Multipart Form)

const fs = require('fs');
const path = require('path');
const Barcode = require('aspose-barcode-cloud-node');

function makeConfiguration() {
    const envToken = process.env['TEST_CONFIGURATION_ACCESS_TOKEN'];
    if (!envToken) {
        return new Barcode.Configuration(
            'Client Id from https://dashboard.aspose.cloud/applications',
            'Client Secret from https://dashboard.aspose.cloud/applications',
            null,
            null
        );
    } else {
        return new Barcode.Configuration(
            null,
            null,
            null,
            envToken
        );
    }
}
const config = makeConfiguration();

async function generateBarcode(api, fileName) {
    const formRequest = new Barcode.GenerateMultipartRequestWrapper(Barcode.EncodeBarcodeType.Code128, "4173706F73652E426172436F64652E436C6F7564");
    formRequest.dataType = Barcode.EncodeDataType.HexBytes;

    const generated = await api.generateMultipart(formRequest);

    fs.writeFileSync(fileName, generated.body);
}

const genApi = new Barcode.GenerateApi(config);
const fileName = path.resolve('testdata','Code128.png');

generateBarcode(genApi, fileName)
    .then(() => {
        console.log(`File '${fileName}' generated.`);
    })
    .catch(err => {
        console.error("Error: " + JSON.stringify(err, null, 2));
        process.exitCode = 1;
    });

Result Image is: Result image

This documentation covers how to use the GenerateApi to set barcodes text.