Specific Parameters for 1D barcodes

Overview

Aspose.BarCode for .NET enables customizing various parameters that are specific for 1D barcode generation. Particularly for 1D barcode standards, developers can adjust the following display properties: the height of bars, the mode of bar filling, the wide-to-narrow ratio, and the automatic correction of invalid barcode text.
This article describes how to manage these properties using specified classes and properties of the library.

Setting Bar Height

Aspose.BarCode for .NET allows adjusting the height of bars for 1D single-row barcodes. This can be done only when the barcode size property, AutoSizeMode, is set to AutoSizeMode.None (see more information about this property here). In this case, regardless of the value specified in the XDimension property, the bar height can be regulated using the BarHeight property of class BarcodeParameters. This property cannot be set for multiple-row barcodes and 2D barcodes.

Bar Height

Is Set to 40 Pixels

Is Set to 80 Pixels

The code snippet below explains how to set different values of bar height for Code 128.

BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.Code128, "ASPOSE");
gen.Parameters.Barcode.XDimension.Pixels = 2;
//set BarHeight 40
gen.Parameters.Barcode.BarHeight.Pixels = 40;
gen.Save($"{path}BarHeight40Code128.png", BarCodeImageFormat.Png);
//set BarHeight 80
gen.Parameters.Barcode.BarHeight.Pixels = 80;
gen.Save($"{path}BarHeight80Code128.png", BarCodeImageFormat.Png);

Bar Filling Modes

For 1D barcodes, Aspose.BarCode for .NET provides a specific mode to generate barcodes with empty bars instead of filled ones. Such a modification can be done using the FilledBars property of class BarcodeParameters. This property is set to True by default and is valid only for 1D barcodes.

Bar Filling

Filled

Empty

The following code sample demonstrates how to adjust the bar filling mode for Code 128.

BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.Code128, "ASPOSE");
gen.Parameters.Barcode.XDimension.Pixels = 2;
//set bars filled
gen.Parameters.Barcode.FilledBars = true;
gen.Save($"{path}BarsFilledCode128.png", BarCodeImageFormat.Png);
//set bars empty
gen.Parameters.Barcode.FilledBars = false;
gen.Save($"{path}BarsEmptyCode128.png", BarCodeImageFormat.Png);

Setting Wide-to-Narrow Ratio

Two-width 1D barcodes are based on the binary code principle, meaning that information is encoded using bars and spaces with two options of width: wide and narrow. Two-width barcode symbologies include the following: Codabar, Code 11, Code 32, Code 39, DataLogic 2-of-5, IATA 2-of-5, Interleaved 2-of-5, ITF 6, ITF 14, Matrix 2-of-5, MSI, OPC, PZN, Standard 2-of-5, and VIN.

In Aspose.BarCode for .NET, the wide-to-narrow ratio defines the relation between the width of wide and narrow elements. It can be set in the WideNarrowRatio property of class BarcodeParameters. The larger if the value of the wide-to-narrow ratio, the larger is the width of the generated barcode. However, the readability also improves with an increase in this parameter. By default, WideNarrowRatio is set to 3.

Wide-to-Narrow Ratio

Is Set to 2

Is Set to 5

The code snippet provided below illustrates how to adjust the setting of the wide-to-narrow ratio for Code 39.

BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.Code39Extended, "ASPOSE");
gen.Parameters.Barcode.XDimension.Pixels = 2;
//set Wide-to-Narrow Ratio to 2
gen.Parameters.Barcode.WideNarrowRatio = 2;
gen.Save($"{path}WideNarrow2Code39.png", BarCodeImageFormat.Png);
//set Wide-to-Narrow Ratio to 5
gen.Parameters.Barcode.WideNarrowRatio = 5;
gen.Save($"{path}WideNarrow5Code39.png", BarCodeImageFormat.Png);

Handling Exception on Incorrect Barcode Text

In case when a barcode has not been created correctly due to invalid barcode text, by default, the library can generate additional dummy data to bring the barcode into line with the standard or delete conflicting characters. Thereafter barcode generation is considered successful.

Developers can change this behaviour by setting the ThrowExceptionWhenCodeTextIncorrect property of BarcodeParameters class. When this property is enabled, an exception is thrown if the barcode text has been found incorrect or incomplete.

Barcode Text Correctness

Correct with Valid Barcode Text

Adjusted with Invalid Barcode Text

The code sample given below illustrates how to set the ThrowExceptionWhenCodeTextIncorrect property. In this example, the following exception will be thrown: “Symbology ITF 6 - codetext is invalid”.

BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.ITF6, "123457");
gen.Parameters.Barcode.XDimension.Pixels = 2;
//correct codetext with correction check
gen.CodeText = "12345";
gen.Parameters.Barcode.ThrowExceptionWhenCodeTextIncorrect = true;
gen.Save($"{path}ITF6Correct.png", BarCodeImageFormat.Png);
//incorrect codetext without correction check
gen.CodeText = "12";
gen.Parameters.Barcode.ThrowExceptionWhenCodeTextIncorrect = false;
gen.Save($"{path}ITF6Filled.png", BarCodeImageFormat.Png);
//incorrect codetext without correction check
try
{
    gen.CodeText = "12";
    gen.Parameters.Barcode.ThrowExceptionWhenCodeTextIncorrect = true;
    gen.GenerateBarCodeImage();
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}