0

I am using this code to generate barcode128 :

String stb1 = "123456789";
code128.setCode(stb1);
//code128.setCodeType(Barcode128.CODE128_UCC);
//code128.setCode("1234567890");
code128.setStartStopText(true);
Image image128 = code128.createImageWithBarcode(cbd, null, Color.white);
image128.setAbsolutePosition(20 + x, 626 + y);
image128.scaleAbsolute(109, 33);
document.add(image128);

My client want the barcode 128 type B. How to set it to type B? I tried CODE128_UCC, CODE128_RAW, CODE128 in setCodyType() but three of them looks similar.

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Nibha Jain
  • 7,742
  • 11
  • 47
  • 71
  • What third party library are you using? – jontro Aug 30 '12 at 10:40
  • one cannot "set barcode 128 to type B". Char Set A = ASCII values from 0 to 95, Char Set B = ASCII values from 32 to 127, Char Set C only supports pairs of digits. for the string `123456789` this makes no difference at all. – Martin Zeitler Sep 16 '17 at 16:48

2 Answers2

4

thanks to the previous answer by pmoleri, I succeded in using Itext (Itextsharp version 5.3.3.0) for generating a barcode128 Type B. This is the c# code I wrote, hope it might help:

String stb1 = "123456789";
Barcode128 code128 = new Barcode128();
code128.CodeType = Barcode.CODE128_RAW;
code128.ChecksumText = true;
code128.GenerateChecksum = true;
code128.StartStopText = true;
code128.Code = (StringEncode128(stb1) + "\uffff");
System.Drawing.Bitmap bm = new System.Drawing.Bitmap(code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White));
bm.Save(Server.MapPath("barcode128.gif"), System.Drawing.Imaging.ImageFormat.Gif);
ImageBarCode128 = Image.GetInstance(Server.MapPath("barcode.gif"));

String Encoding function:

public String StringEncode128(String text) {
const int Ascii_FNC1 = 102;
const int Ascii_STARTB = 104;

int CharAscii = 0;
for (int CharPos = 0; CharPos < text.Length; ++CharPos)
{
  string letter = text.Substring(CharPos, 1);
  CharAscii = (int)Convert.ToChar(letter);
  if (CharAscii > 127 && CharAscii != Ascii_FNC1) {
    throw new Exception(text + "|" + CharAscii);
    }
}

StringBuilder outstring = new StringBuilder();
outstring.Append((char)(Ascii_STARTB));

for (int CharPos = 0; CharPos < text.Length; ++CharPos)
{
    string letter = text.Substring(CharPos, 1);
    CharAscii = (int)Convert.ToChar(letter) - 32;
    outstring.Append((char)(CharAscii));
}

return outstring.ToString();
}
fakeChina
  • 76
  • 4
0

Itext probably makes the most compact version of the barcode, that's why you can't choose the charset. For example, it will choose charset C if it starts with a large sequence of digits and then change to charset B if a sequence of alpha chars follows.

Any barcode scanner able to read Code 128 must be able to read any of the charsets, so I don't see the need of sticking to charset B.

Update

I don't think you can do it with iText, unless you pass the raw code. Luckily charset B is pretty straightforward, just put toghether a string with:

  • start character for charset B: 104
  • substract 32 from the ascii code of every character in the original string
  • finish the string with "\uffff"

Then create the barcode in raw mode using: setCodeType(Barcode.CODE128_RAW)

You can get addition information here: http://grandzebu.net/informatique/codbar-en/code128.htm

pmoleri
  • 4,238
  • 1
  • 15
  • 25
  • thanks for the answer but my client want the barcode 128b .Do you have any idea how can i set barcode 128b in java – Nibha Jain Aug 31 '12 at 13:31