1. code128 basic knowledge
Code128There are three versions:
Code128a: Standard Number and letter, control character, Special Character
Code128b: standard numbers and letters, lowercase letters, special characters
Code128c: [00]-[99] Number pair set, 100 in total
Here we useCode128b version. The code 128 font must be installed in the system.
2. bar code encoding rules
A bar code consists of the Start bit, Data, check bit, and stop bit.
Bar Code = convert. tochar (204) + Data + convert. tochar (Verification Code) + convert. tochar (206 );
The verification code needs to be calculated. The conversion function is provided below:
Private string get128codestring (string inputdata)
{
String result;
Int checksum= 104;
For (int ii = 0; II <inputdata. length; II ++)
{
If (inputdata [II]> = 32)
{
Checksum + = (inputdata [II]-32) * (II + 1 );
}
Else
{
Checksum + = (inputdata [II] + 64) * (II + 1 );
}
}
Checksum = checksum % 103;
If (checksum <95)
{
Checksum + = 32;
}
Else
{
Checksum ++ = 100;
}
Result = convert. tochar (204) + inputdata. tostring () + convert. tochar (checksum) + convert. tochar (206 );
Return result;
}
3. Print
Public void printlable ()
{
Printdocument Pd = new printdocument ();
Standardprintcontroller controler = new standardprintcontroller ();
try
{< br> PD. printpage + = new printpageeventhandler (this. printcustomlable);
PD. printcontroller = controler;
PD. print ();
return;
}< br> catch (exception ERR)
{< br> console. writeline (err. message);
return;
}< br> finally
{< br> PD. dispose ();
}
}
Public void printcustomlable (Object sender, printpageeventargs AV)
{
Font FT1 = new system. Drawing. Font ("Times New Roman", 18, fontstyle. Regular, graphicsunit. World );
Font FT2 = new system. Drawing. Font ("code 128", 64, fontstyle. Regular, graphicsunit. World );
Brush BR = new solidbrush (color. Black );
Margins margins = new margins (50, 50, 50,145 );
Av. pagesettings. Margins = margins;
Av. Graphics. drawstring (get128codestring (inputstring), FT2, BR, 50,-3 );
Av. Graphics. drawstring (inputstring, FT1, BR, 110,60 );
Av. hasmorepages = false;
}
I saw the comment saying that it was not usable. I tested it today and found that I was wrong:
Bar Code = convert. tochar (204) + Data + convert. tochar (Verification Code) + convert. tochar (206 );
Code:
Result = convert. tochar (204) + checksum. tostring () + convert. tochar (checksum) + convert. tochar (206 );
It should be changed:
Result = convert. tochar (204) + inputdata. tostring () + convert. tochar (checksum) + convert. tochar (206 );
Simple TestProgram:
Vs2008 Test Program
The printed content of the Code must be referenced:
Using system. Drawing. printing;
Define global variables:
String inputstring;
Test in button click:
Inputstrings = "0123456789 ";
// View the code128 code value:
// Lblbarcode. Text = get128codestring (inputstring );
Printlable ();
If the problem persists, the code 128 font code128 font is not installed in the system for downloading.