1

I have tried to generate the Barcode128 barcode to be placed on label and usually labels are less width. I am wondering that 3 digit(100) String barcode width is larger than 4 digit(1001) string barcode. 3 digit barcode should be less than the 4 digit barcode right? I have verified in online barcode generation 3 digit barcode width is less than the 4 digit barcode width where as iText api takes width more for 3 digit string compared to 4 digit string.

Could anybody please let us know the reason for this and how to make 3 digit barcode shorter than the 4 digit barcode?

Online barcode generator URL here

3 digit barcode and 4 digit barcode with iText API

Code sample used to generate the barcode.

Barcode128 code128 = new Barcode128();
    code128.setCode(myText);
    Image myBarCodeImage128 = code128.createImageWithBarcode(contentByte,
        null, null);

then image is added to pdf document.

4
  • Can you show us how you generate the barcode?
    – SubOptimal
    Commented Dec 10, 2015 at 7:06
  • Thanks. Barcode image is added Commented Dec 10, 2015 at 13:09
  • You are right your generated 3 digit barcode is wider then the 4 digit barcode. On the URL you posted the 3 digit barcode is narrower then the 4 digit barcode. But based on what we should give you an answer why it's like it it with your code? If a guess is enough for you: Maybe you do something wrong? The guess is based on the amount of information you have provided.
    – SubOptimal
    Commented Dec 10, 2015 at 13:19
  • It is very direct and code sample was added to the post. Commented Dec 10, 2015 at 14:07

2 Answers 2

5

Barcode 128 has several ways to encode digits. One of the ways is to encode 2 digits in the same symbol. In 4 digit case case you have two symbols each with two digits, two symbols in total. In the 3 digit case you have a symbol for two digits, a symbol to switch to alphanumeric and symbol for the last digit, three symbols in total. As you can see, less is more in this case.

5
  • Thanks for the response. Is there any way to minimize the length of the 3 digit barcode than 4 digit as i can generate the small length 3 digit barcode than 4 digit barcode in link online Commented Dec 10, 2015 at 13:25
  • 1
    You can force the code set with setCodeSet(Barcode128CodeSet.B). Commented Dec 10, 2015 at 14:11
  • 1
    there are 3 code sets (A, B, C) for Code128. Code set C can print 2 numerical characters with a single barcode bar, so Code set C should be used for purely numerical data. In your particular case the problem may be the fact that you only have 3 characters of data in your second example. Encoding 3 digits needs more space because you first need a bar in code C for the first two digits, then a bar to switch to code A or B, then the third digit in code A or B. With 4 digits, you only need two bars in code C and you're done. Code c becomes more economic with larger numbers. Commented Dec 10, 2015 at 14:14
  • Thanks paulo, I did not find such code in iText5.2.1.jar, Is it exists in latest jar ? Commented Dec 10, 2015 at 14:25
  • Thank you Dirk for detail explanation Commented Dec 10, 2015 at 14:27
1

Find a working snippet based on the answer from Paulo Soares.

Document document = new Document(new Rectangle(340, 842));
PdfWriter writer = PdfWriter.getInstance(document, 
    new FileOutputStream("barcodes.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();

document.add(new Paragraph("Barcode 128 demo"));

Barcode128 code128 = new Barcode128();
code128.setCode("100");
code128.setCodeSet(Barcode128CodeSet.B);
document.add(code128.createImageWithBarcode(cb, null, null));

code128 = new Barcode128();
code128.setCode("1001");
code128.setCodeSet(Barcode128CodeSet.B);
document.add(code128.createImageWithBarcode(cb, null, null));
document.close();
3
  • Thank you example is now showing 3 digit barcode length is not more than the 4 digit barcode. could you please confirm that is there any impact of setting codeSet as B ? i have to generate the barcode not only for numbers but also for alphanumeric characters using the same code. Commented Dec 11, 2015 at 6:07
  • @Dhorrairaajj The characterset 'B' in barcode 128 would fulfil the requirement if you are using numbers, and latin charcaters (lower- and uppercase). For a complete list of supported characters in the different modes have a look at this encoding table.
    – SubOptimal
    Commented Dec 11, 2015 at 7:35
  • Help full link to understand the concept. Thank you Commented Dec 11, 2015 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.