We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Barcode fit placeholder


I am checking your library Aspose Barcode for the possibility in using it in the system to add barcode in pdf. In the system I need to fit a barcode in a place holder and the placeholder can vary in size (will always be larger than the minimum size needed to fit data), however I found the following 4 issues/concerns when trying to get it working

When I used the code below

//encode array to string
StringBuilder strBld = new StringBuilder();
foreach (byte bval in encodedArr)
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.DataMatrix, strBld.ToString());
generator.Parameters.Barcode.CodeTextParameters.Location = CodeLocation.None;
generator.Parameters.Barcode.DataMatrix.DataMatrixEncodeMode = DataMatrixEncodeMode.Full;
generator.Parameters.Barcode.Padding.Bottom.Pixels = 1;
generator.Parameters.Barcode.Padding.Left.Pixels = 1;
generator.Parameters.Barcode.Padding.Right.Pixels = 1;
generator.Parameters.Barcode.Padding.Top.Pixels = 1;

Using code above it results in the barcode below

However if I add the below code to use autosizemode nearest instead of none:

generator.Parameters.AutoSizeMode = AutoSizeMode.Nearest;
generator.Parameters.ImageHeight.Pixels = 60;
generator.Parameters.ImageWidth.Pixels = 60;

The result is the below.
image.png (5.3 KB)

1)My issue is that when setting nearest it is ignoring the fact that I set padding to 1,is there a correct way to force the padding when using nearest ( this happens with any image height and image width)

2)Another concern is that if the ImageHeight and ImageWidth are not the same size for example if I set the height to 60 and width to 280 it does not take the whole width(Also notice that as mentioned in one, the top and bottom margins are not 1 pixel neither).
generator.Parameters.AutoSizeMode = AutoSizeMode.Nearest;
generator.Parameters.ImageHeight.Pixels = 40;
generator.Parameters.ImageWidth.Pixels = 180;

The result is the below
image.png (5.2 KB)

I tried to use the aspect ration to solve it but as seen from image below it still does not take the whole size(might be due to the padding as mentioned in 1)
generator.Parameters.Barcode.DataMatrix.AspectRatio =(float)40 / 180;

The result is the below
image.png (9.7 KB)

  1. Referring to no 2 when setting the aspect ratio, according to https://docs.aspose.com/barcode/net/datamatrix-barcode/#aspect-ratio-settings “In general, the Aspect Ratio value should be set to 1.”. Is there any limitation to what the aspect ratio can be set?

  2. Also referring to no 2, when setting aspect ratio it seems that the barcode is stretched out is there a way to safely calculate and set the rows and columns according the aspect ratio so that barcode remains valid but not stretched out?


I recommend you to kindly see and browse the document for your complete reference and understanding on auto size modes.

Yes, in AutoSizeMode.Nearest, it will ignore all other attributes except ImageHeight and ImageWidth.

Yes, this expected. As I mentioned above, all other attributes are taken into account except ImageWidth and ImageHeight. I tested using the following code and it works as expected.
Sample code:

  Aspose.BarCode.Generation.BarcodeGenerator generator = new Aspose.BarCode.Generation.BarcodeGenerator(Aspose.BarCode.Generation.EncodeTypes.DataMatrix, "Åspóse.Barcóde©");
            generator.Parameters.Barcode.CodeTextParameters.Location = Aspose.BarCode.Generation.CodeLocation.None;
            generator.Parameters.Barcode.DataMatrix.DataMatrixEncodeMode = Aspose.BarCode.Generation.DataMatrixEncodeMode.Full;
            generator.Parameters.Barcode.Padding.Bottom.Pixels = 1;
            generator.Parameters.Barcode.Padding.Left.Pixels = 1;
            generator.Parameters.Barcode.Padding.Right.Pixels = 1;
            generator.Parameters.Barcode.Padding.Top.Pixels = 1;
            generator.Parameters.AutoSizeMode = Aspose.BarCode.Generation.AutoSizeMode.Nearest;
            generator.Parameters.ImageHeight.Pixels = 140;
            generator.Parameters.ImageWidth.Pixels = 180;

            generator.Save("e:\\test2\\out1.png", Aspose.BarCode.Generation.BarCodeImageFormat.Png);

I checked image height is 140 pixels and image width is 180 pixels which is ok.

This is how aspect ratio attribute works this way, see the document for your reference. Aspect ration will stretch out the barcode accordingly.

In understand you problem. Currently DataMatrix ECC200 in automatic sizing mode can be selected only from square type. We created task to add more options for this (auto_aquare, auto_rectangular, row_columns), but we have no ETA for this. Currently you can only manually select required rows and columns.

Aspect ratio affects only barcode cell aspect ratio / width-height difference.