Customize Barcode Appearance in Python via.NET

This article describes how to adjust barcode parameters, such as size, rotation angle, paddings, and image borders.

Overview

Aspose.BarCode for Python via .NET provides class BarcodeGenerator to generate barcodes according to the predefined settings so that each element of a barcode image gets its fixed position relative to other elements, as shown in the scheme below. A barcode label comprises the following parts: barcode text, bars, top and bottom captions, image borders, and paddings. All elements besides barcode bars are optional.

Barcode Image Sizing Modes

In general, the library defines barcode image size in terms of height and width automatically. The library enables developers to manage image size settings manually by determining the height and width of barcode labels through image_height and image_width properties of class BaseGenerationParameters.

Barcode image size can be varied by using various sizing modes that can be enabled using the AutoSizeMode enum. AutoSizeMode provides the following options: NONE, NEAREST, and INTERPOLATION. In INTERPOLATION and NEAREST modes, barcode image size is managed based on the values of width and height, and other parameters are ignored. In turn, when the NONE mode is enabled, barcode image size is defined ignoring width and height; instead, other parameters, such as XDimension, are used. AutoSizeMode takes the NONE value by default.

Sizing Mode NONE

As outlined above, setting the NONE mode implies that barcode image size is managed based on different parameters ignoring width and height. To specify barcode size, class BarcodeParameters provides a special properties called x_dimension. It is used to define the minimum size of bars in 1D barcodes or cells in 2D barcodes. Other barcode sizing parameters are calculated based on the XDimension value.

Various barcode standards often determine XDimension to provide compatibility between printing and scanning equipment units so that barcode images could be captured by scanners used in various companies. XDimension is related to the data density of a barcode type, meaning that it determines the amount of data that can be encoded in one barcode. Setting a small value of XDimension results in covering less space to place each encoded character in a barcode image. In contrast, setting a bigger value of XDimension leads to enlarging the area required to encode each character and decreasing the number of characters per inch.

The barcode label shown below has been generated using the NONE mode.

Sizing Mode INTERPOLATION

Setting the auto_size_mode property to AutoSizeMode.INTERPOLATION means that only the values specified using image_height and image_width properties are used in barcode sizing. In this case, barcode image size is determined using the manually specified values of height and width even when it results in producing distorted barcode proportions and the deterioration of barcode readability. The INTERPOLATION mode is appropriate to create barcode images with a resolution of 300 dpi or higher. Such resolution settings will allow keeping barcode distortions negligible and avoiding the deterioration of barcode readability.

The barcode label generated through the INTERPOLATION mode is demonstrated below.

Sizing Mode NEAREST

To set barcode image size, the NEAREST mode uses only the values specified using image_height and image_width properties similarly to the INTERPOLATION mode. In this mode, BarcodeGenerator aims at finding the most suitable image size to avoid barcode proportion distortion and readability deterioration.

The barcode label created through the NEAREST mode is provided below.

Bar Width Reduction

The other important property that needs to be determined accurately is bar width. Setting an appropriate value of this parameter is critical to assure successful barcode scanning. Due to the so-called ink floating phenomenon, some barcode printing techniques may result in increasing actual bar width after printing out barcode images. This happens often in commercial printing in cases when conventional printing presses are used. To ensure that printed barcode images will be printed out with appropriate bar width, setting a bar width reduction value may be required.

Bar width reduction (BWR) is a way to mitigate the effect of ink floating in a graphic design file of a barcode. The barcode library allows modifying bar width through the bar_width_reduction property of class BarcodeParameters. Passing the required value while setting this property allows decreasing bar width in 1D barcodes or cell size in 2D barcodes. To find out the suitable BWR value for a printer, it is necessary to check special tables provided by printer manufacturers. Modifying this parameter is not applicable to laser printers.

The sample barcodes shown below have been generated with and without applying bar width reduction.

Barcode Type Bar Width Reduction 0 Bar Width Reduction 3
Code 128
Data Matrix

Customizing Barcode Borders and Padding

The barcode library allows customizing barcode image borders and paddings. Corresponding properties are described further.

Border Settings

Applying default border settings results in generating barcode images without borders. Alternatively, they can be defined manually using five styles: solid, dotted, dashed, dash-dot, and dash-dot-dot. The border style can be modified using class BorderParameters. In addition, this class allows setting border thickness in any available units and colors using width and color properties. Border styles can be changed using the dash_style property of this class.

Barcode labels demonstrated below have been generated using different border styles.

Border Style Solid Dashed Dotted Dash-Dot Dash-Dot-Dot

Paddings

Paddings from the edges of a barcode image or its borders can be set in four directions using a special class called Padding and its properties: left, right, top, and bottom. By default, the padding values are set to 5 points in all directions.

Padding Millimeters Pixels

Barcode Rotation

Barcode image rotation can be set using the rotation_angle property of class BaseGenerationParameters. Setting this property to a value in degrees allows generating a barcode image rotated at the desired angle clockwise or counterclockwise.

The sample barcode labels rotated by various angles are shown below.

Rotation Angle Is Set to +90° Is Set to -90° Is Set to +45° Is Set to -45° Is Set to 180°