barcode code 128 in classic asp

ASP Version of code 128 barcode

just follow three easy step to create barcode code 128 in classic asp.

step 1. create object from function and call stringToBarcode with string value, function returns code 128 compatible string

step 2. use font code 128 from here http://www.jtbarton.com/Barcodes/Code128.aspx and install on web server

setp 3. use font “Code 128” to print output string from function

<% 	class barcode128 		
                    Public Function stringToBarcode (ByVal value) 			
                               stringToBarcode ="" 			
                               Dim charPos, minCharPos, currentChar, checkSum 			
                               Dim isTableB : isTableB = True 			
                               Dim isValid : isValid = True 			
                               Dim returnValue : returnValue = "" 			
                               currentChar = "" 			
                               If Len(value) > 0 Then
				' devermie validity of data
				For charCount = 1 To Len(value)
					currentChar = Asc(Mid(value, charCount, 1))
					If Not (currentChar >= 32 And currentChar <= 126) Then
						isValid = False
						Exit For 
					End if
				Next 
				If isValid = True Then
					charPos = 1
					Do While charPos =< Len(value)
						If isTableB = True Then
							If charPos = 1 Or charPos + 3 = Len(value) Then
								minCharPos = 4
							Else
								minCharPos = 6
							End If
							minCharPos = isNumber(value, charPos, minCharPos)
							If minCharPos =< 0 Then
								If charPos = 1 Then
									returnValue =  chr(205)
								Else
									returnValue = returnValue & chr(199)
								End If 
								isTableB = false
							Else
								If charPos = 1 Then
									returnValue = chr(204)
								End If 
							End If 
						End if
						If Not isTableB Then
							minCharPos = 2
							minCharPos = isNumber(value, charPos, minCharPos)
							If minCharPos < 0 Then
								currentChar = Mid(value, charPos, 2)
								If currentChar < 95 Then 
									currentChar = currentChar + 32 
								Else 
									currentChar = currentChar + 100 
								End if
								returnValue = returnValue & chr(currentChar)
								charPos = charPos + 2
							Else
								returnValue = returnValue & chr(200)
								isTableB = true
							End If
						End If 
						If isTableB = true Then
							returnValue = returnValue & Mid(value, charPos, 1)
							charPos = charPos + 1
						End If													
					Loop 
					checkSum = 0
					Dim loopVar : loopVar = 0
					For loopVar = 1 To Len(returnValue)
						currentChar = Asc(Mid(returnValue, loopVar,1))
						If currentChar < 127 Then 
							currentChar = currentChar - 32 
						Else 
							currentChar = currentChar - 100
						End if
						If loopVar = 1 Then 
							checkSum = currentChar 
						Else 
							checkSum = (checkSum + ((loopVar - 1 )* currentChar)) Mod 103							
						End if
					Next
					If checkSum < 95 Then checkSum = checksum + 32 Else checkSum = checkSum + 100
					returnValue = returnValue & Chr(checkSum) & Chr(206)					
				End if
			End If
			stringToBarcode = returnValue
		End Function 
		Public Function isNumber(ByVal inputValue, ByVal charPos, ByVal minCharPos)
			minCharPos = minCharPos - 1
			If (charPos + minCharPos) =< Len(inputValue) Then 				
                                Do While minCharPos >= 0
					var_1 = Asc(Mid(inputValue, charPos + minCharPos, 1))
					If var_1 < 48  Or var_1 > 57 Then
						Exit do
					End If
					minCharPos = minCharPos - 1					
				Loop 
			End If
			isNumber = minCharPos
		End Function 
	End class
%>

Just copy this above asp script and use as class and create object from class like as below

<%
dim barcodeobj
set barcodeobj = new barcode128
barcodetext = barcodeobj.stringtobarcode("12345678910")
%>

Import code128 font from above link and import using css3 font-face

@font-face
{
font-family: mybarcode128;
src: url('barcode128.ttf'),
url('barcode128.eot'); /* IE9 */
}
.barcodetext{
font-family: 'mybarcode128';
}

and use to display barcodetext

<div class="barcodetext"><%=barcodetext%></div>

cheers,

Advertisements
About

hi there, I am software engineer, working in e-commerce company and passionate about all thing digital. On this blog, I share my experiments with different technology.

Tagged with: , , , ,
Posted in SQL Server
9 comments on “barcode code 128 in classic asp
  1. Hector Leon says:

    Excelent.!! just what i need it

  2. Jeff says:

    I am getting funky characters. �,BXn{�0v� What does this mean? Great work by the way.

  3. Jeff says:

    Here is a sample page. With sample information. As you can see, the space above order number is where the barcode is supposed to be. http://liquitronics.com/packinglabel.asp any idea as to why this is happening? Sometimes it shows a partial barcode along with weird symbols.

    • Jignesh says:

      Thanks Jeff for stopping by,

      from the character it looks like it’s showing correct values but I can see there is no barcode font linked to the page.

      Use following css code and insert into your text into div with class “barcodetext”.
      To get barcode128.tt, google barcode128 and you can find open barcode fonts library you can download barcode fonts from.
      Here is one http://www.dafont.com/code-128.font.

      /* to import barcode fonts */
      @font-face
      {
      font-family: mybarcode128;
      src: url(‘barcode128.ttf’),
      url(‘barcode128.eot’); /* IE9 */
      }
      /* use barcode font for class*/
      .barcodetext{
      font-family: ‘mybarcode128’;
      }

  4. Autotech says:

    Was trying out your code , but the barcode font is not applying on the text generated.
    https://ehr2014beta01.rademr.com/barcodetest.asp

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What people say about post?
seo hizmeti on Awesome wordpress ecommerce…

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 38 other followers

%d bloggers like this: