Barcode for Java - add spaces for fixed length value

Hello,

We are evaluating the BarCode for Java version to create 2D barcodes using the QR format.

We have a requirement to maintain fixed length values for the data encoded in the barcode. Will the BarCode component allow us to fill the data value with spaces in order to maintain the required length?

For example:

  • value1: DINUMB01 (8 characters)
  • value2 : SORTCOD (7 characters)
  • value3 : AIR (7 characters)
  • value4: RO123456789 (19 characters)

In this example, values 3 and 4 are smaller than the required length for the field. Can we pad the values with spaces and encode the barcode this way? Will the barcode retain the spaces during the encoding?

  • value3 : AIRxxxx (7 characters; x represents 4 spaces)
  • value4: RO123456789xxxxxxxx (19 characters; x represents 7 spaces)

The demo we tried appears to remove the spaces during encoding. Please see attached example test barcode.

Thank you very much for your support!

Hello,

Thank you for your post!

Those spaces '\u0020' WILL be encoded into the barcode image:

//Create a BarCodeBuilder instance
BarCodeBuilder b= new BarCodeBuilder();
//Width of the smallest module of QR
b.setXDimension(1);
//Symbology set to QR
b.setSymbology(b.SYMBOLOGY_QR);
Object[] content ={"DINUMB01", "SORTCOD", "AIR", "RO123456789"};
//Fixed length
String codetext = String.format("%1$-8s\n%2$-7s\n%3$-7s\n%4$-19s", content);
b.setCodeText(codetext);

try
{
b.save("c:\\testjava.jpg", "jpg");

}
catch(Exception ex)
{
}


Hello,

Thank you very much for your response. We tried using the code you supplied but now receive some junk characters in our barcode.

Here is our sample code:

import com.aspose.barcode.*;

public class BarcodeSample{

public static void main(String args[]){
//Create a BarCodeBuilder instance
BarCodeBuilder b= new BarCodeBuilder();
//Width of the smallest module of QR
b.setXDimension(1);
//Symbology set to QR
b.setSymbology(b.SYMBOLOGY_QR);
Object[] content ={"DINUMB01", "SORTCOD", "AIR", "RO123456789"};
//Fixed length
String codetext = String.format("%1$-8s\n%2$-7s\n%3$-7s\n%4$-19s", content);
b.setCodeText(codetext);
try
{
b.save("c:\\testjava.jpg", "jpg");

}
catch(Exception ex)
{
}
}
}

Is there any other setting we need to apply on the host machine?

Thank you!

Hello,

Thank you for your post!

I guess that's the '\n' new line character, please try to remove them from the Formatter, if you don't want line breaks:

String codetext = String.format("%1$-8s%2$-7s%3$-7s%4$-19s", content);

Best regards.