Somacon.com: Articles on websites & etc.

§ Home > Index > ASP Programming

PDFLib POSTNET Bar Codes ASP VBScript Class

Below is an ASP-VBScript class to print POSTNET bar codes in a PDF using PDFLib.

<%

' SOFTWARE LICENSE AGREEMENT

'

' AUTHOR:

' Shailesh N. Humbad maintains all rights to this code.

'

' RESPONSIBILITIES:

' You may not claim this code as your own work or resell this code

' substantially unmodified and stand-alone.

' If you use this code in a commercial, for-profit enterprise, you must

' pay $5.00 to the author for usage rights.

' You must interpret this license agreement keeping with the intent of the author.

' You must contact the author for clarification where the terms are unclear.

'

' RIGHTS:

' You may evaluate this code for free.

' You may distribute this code for profit provided this license agreement

' is kept with it unmodified.

' You may resell this code as part of your system whose primary

' purpose is not the functionality provided by this code.

' You may make customizations to this code to suit your system.

' You may use this code for free in not-for-profit enterprises.

'

' DISCLAIMER:

' No warranty is provided with this code.

' No technical support is provided with this code.

'

' CONTACT:

' To purchase a license or obtain charged technical support, please visit

' http://www.somacon.com/

'

' SOFTWARE DOCUMENTATION

'

' TITLE: PDFLibPOSTNETBarCodes Class

' VERSION: 1.0

' SYNOPSIS: ASP class to print a POSTNET bar code in a PDF using PDFlib.

'

' PUBLIC FUNCTIONS:

' void PrintBarCode(String zipcode)

' Zipcode is a string containing the 5 or 9 digit zipcode.

' For a 9 digit zipcode, the string must contain the hyphen

' separating the first five digits from the last four.

'

' String parsedZipCode ParseZipCode(String stringToParse)

' The parsed zip code returned will be the first string matching

' DDDD or DDDDD-DDDD, where D is a digit from 0 through 9, encountered

' while searching starting from the end of the strintToParse.

' If a valid zip code is not found, it returns an empty string.

' Otherwise, it returns the parsed zip code (including the hyphen if it's a 9-digit zip).

'

' PUBLIC PROPERTIES:

' objPDFLib

' Reference to an instantiated PDFLib object. This must be set

' before calling the PrintBarCode function.

' OffsetX

' The integer X offset at which to print the bar code.

' OffsetY

' The integer Y offset at which to print the bar code.

' Offsets are units in the reference system of PDFLib (default 1 unit = 1/72").

' ExceptionBase

' The offset number for exceptions thrown by the class.

' This parameter is only required if your code handles

' or throws user-defined exceptions.

'

' SIMPLIFIED USAGE:

' Dim oPDF, objPBC, strZipCode

' Set oPDF = Server.CreateObject("PDFlib_com.PDF")

' Set objPBC = New PDFLibPOSTNETBarCodes

' Set objPBC.objPDFLib = oPDF

' objPBC.OffsetX = 100

' objPBC.OffsetY = 500

' objPBC.ExceptionBase = 1000

' strZipCode = objPBC.ParseZipCode("Detroit, MI 48226-1038")

' objPBC.PrintBarCode strZipCode

'

'

' EXCEPTIONS:

' An exception is raised on the following conditions:

' Non-numeric value set for either Offset.

' Non-object set for objPDFLib.

' PrintBarCode called before valid PDFLib object reference set.

' Invalid zip code passed to PrintBarCode.

' This version has 9 possible exceptions codes.

'

' POSTNET BAR CODES:

' The specifications for POSTNET Bar Codes can be found in U.S. Postal Service

' Publication 25, Direct Mail Guidelines, available via:

' http://www.usps.com/businessmail101/resources/

' See Chapter 4 for bar code reading and writing specifications,

' and Chapter 2 for placement specifications.

' In general, the bar code should be printed a minimum of 1/25" above the first

' line of the address, and below the top edge of the envelope window. The left

' and right clearance for the address should be a minimum of 1/8", and the

' bottom clearance should be 1/25" minimum. The address must satisfy the

' minimum clearances regardless of where the mailpiece shifts to within the envelope.

' Note that 1/4" of clearance is preferred.

' Refer to the Guidelines for complete details.

Class PDFLibPOSTNETBarCodes

' PROPERTY STORAGE VARIABLES

Private mintOffsetX

Private mintOffsetY

Private mintExceptionBase

Private mobjPDFLib

' PRIVATE CONSTANT MEMBERS

Private FullBarHeight,HalfBarHeight,BarWidth,BarSpacing

Private CurrentX

' EVENT HANDLERS

Private Sub Class_Initialize()

' Full Bar Nominal Height = 0.125"

FullBarHeight = 9

' Half Bar Nominal Height = 0.050"

HalfBarHeight = 3.6

' Full and Half Bar Nominal Width = 0.020"

BarWidth = 1.44

' Bar Spacing = 0.050"

BarSpacing = 3.6

CurrentX = 0

mintExceptionBase = 1200

End Sub

Private Sub Class_Terminate()

End Sub

' PROPERT SET FUNCTIONS

Public Property Set objPDFLib(new_objPDFLib)

Set mobjPDFLib = new_objPDFLib

End Property

' PROPERT LET FUNCTIONS

Public Property Let OffsetX(new_OffsetX)

' handle error conditions

If Not IsNumeric(new_OffsetX) Then

Err.Raise vbObjectError + mintExceptionBase + 0, _

"OffsetX Property-PDFPOSTNETBarCodes Class", _

"OffsetX setting should be a number."

End If

' set the properties

mintOffsetX = new_OffsetX

CurrentX = new_OffsetX

End Property

Public Property Let OffsetY(new_OffsetY)

' handle error conditions

If Not IsNumeric(new_OffsetY) Then

Err.Raise vbObjectError + mintExceptionBase + 1, _

"OffsetY Property-PDFPOSTNETBarCodes Class", _

"OffsetY setting should be a number."

End If

' set the properties

mintOffsetY = new_OffsetY

End Property

Public Property Let ExceptionBase(new_ExceptionBase)

' handle error conditions

If Not IsNumeric(new_ExceptionBase) Then

Err.Raise vbObjectError + mintExceptionBase + 2, _

"ExceptionBase Property-PDFPOSTNETBarCodes Class", _

"ExceptionBase setting should be a number."

End If

' set the properties

mintExceptionBase = new_ExceptionBase

End Property

' PUBLIC PROCEDURES

' prints to pdf the POSTNET barcode for the 5 digit or 10 digit zip code

' the zipcode is validated before being printed

' an exception is raised if the zipcode fails validation

Public Sub PrintBarCode(zipcode)

' check that PDFLib object has been initialized

If Not IsObject(mobjPDFLib) Then

Err.Raise vbObjectError + mintExceptionBase + 3, _

"PrintBarCode Sub-PDFPOSTNETBarCodes Class", _

"The PDFLib object must be set before printing a bar code."

End If

' check that the zip code is correct

zipcode = ValidateZipCode(zipcode)

' set the line width

mobjPDFLib.setlinewidth BarWidth

' start frame bar

DrawBar FullBarHeight

' zip code digits

DrawDigitBars(Mid(zipcode,1,1))

DrawDigitBars(Mid(zipcode,2,1))

DrawDigitBars(Mid(zipcode,3,1))

DrawDigitBars(Mid(zipcode,4,1))

DrawDigitBars(Mid(zipcode,5,1))

If Len(zipcode) > 5 Then

DrawDigitBars(Mid(zipcode,7,1))

DrawDigitBars(Mid(zipcode,8,1))

DrawDigitBars(Mid(zipcode,9,1))

DrawDigitBars(Mid(zipcode,10,1))

End If

' checksum digit

DrawDigitBars(CalculateCheckSum(zipcode))

' end frame bar

DrawBar FullBarHeight

End Sub

Public Function ParseZipCode(stringToParse)

Dim i, strlen, zipcodeLength, zipcode

If IsNull(stringToParse) Then

ParseZipCode = ""

Exit Function

End If

stringToParse = CStr(stringToParse)

strlen = Len(stringToParse)

If strlen < 5 Then

ParseZipCode = ""

Exit Function

End If

' parse the zip code backward

' matches the first occurrence of DDDDD or DDDDD-DDDD, where D is a digit

zipcodeLength = 0

For i = strlen To 1 Step -1

' conditions to continue the zip code

Select Case zipcodeLength

Case 0,1,2,3

If IsNumeric(Mid(stringToParse,i,1)) Then

zipcodeLength = zipcodeLength + 1

zipcode = zipcode & Mid(stringToParse,i,1)

Else

zipcodeLength = 0

zipcode = ""

End If

Case 4

If Mid(stringToParse,i,1) = "-" Then

zipcodeLength = zipcodeLength + 1

zipcode = zipcode & Mid(stringToParse,i,1)

ElseIf IsNumeric(Mid(stringToParse,i,1)) Then

zipcodeLength = zipcodeLength + 1

zipcode = zipcode & Mid(stringToParse,i,1)

Exit For

Else

zipcodeLength = 0

zipcode = ""

End If

Case 5,6,7,8

If IsNumeric(Mid(stringToParse,i,1)) Then

zipcodeLength = zipcodeLength + 1

zipcode = zipcode & Mid(stringToParse,i,1)

Else

zipcodeLength = 0

zipcode = ""

End If

Case 9

If IsNumeric(Mid(stringToParse,i,1)) Then

zipcodeLength = zipcodeLength + 1

zipcode = zipcode & Mid(stringToParse,i,1)

Exit For

Else

zipcodeLength = 0

zipcode = ""

End If

End Select

Next

' return the parsed zip code if found

If zipcodeLength = 5 Or zipcodeLength = 10 Then

' reverse the zip code

ParseZipCode = StrReverse(zipcode)

Else

ParseZipCode = ""

End If

End Function

' PRIVATE PROCEDURES

' returns validated zip code

Private Function ValidateZipCode(zipcode)

Dim i, digit

' check that zip code is not null

If IsNull(zipcode) Then

Err.Raise vbObjectError + mintExceptionBase + 4, _

"ValidateZipCode Sub-PDFPOSTNETBarCodes Class", _

"Can't draw null zip code."

End If

' convert parameter to string

zipcode = Cstr(zipcode)

' check if length is 5

If Len(zipcode) <> 5 And Len(zipcode) <> 10 Then

Err.Raise vbObjectError + mintExceptionBase + 5, _

"ValidateZipCode Sub-PDFPOSTNETBarCodes Class", _

"Zip code must be 5 digits or 10 digits including hyphen. len:'" & Len(zipcode) & _

"' zipcode: '" & zipcode & "'"

End If

If Len(zipcode) = 5 Then

' check that all characters are numeric

For i = 1 To 5

If IsNumeric(Mid(zipcode,i,1)) = False Then

Err.Raise vbObjectError + mintExceptionBase + 6, _

"ValidateZipCode Sub-PDFPOSTNETBarCodes Class", _

"5 digit zip code contains non-numeric character."

End If

Next

Else

' check for hyphen

If Mid(zipcode,6,1) <> "-" Then

If IsNumeric(Mid(zipcode,i,1)) = False Then

Err.Raise vbObjectError + mintExceptionBase + 7, _

"ValidateZipCode Sub-PDFPOSTNETBarCodes Class", _

"10 digit zip code does not contain hyphen in right place."

End If

End If

' check that all characters are numeric

For i = 1 To 10

If IsNumeric(Mid(zipcode,i,1)) = False And i <> 6 Then

Err.Raise vbObjectError + mintExceptionBase + 8, _

"ValidateZipCode Sub-PDFPOSTNETBarCodes Class", _

"10 digit zip code contains non-numeric character."

End If

Next

End If

' return the string

ValidateZipCode = zipcode

End Function

' takes a validated zip code and

' calculates the checksum for POSTNET

Function CalculateCheckSum(zipcode)

Dim sumOfDigits

' calculate sum of digits

If Len(zipcode) = 10 Then

sumOfDigits = CInt(Mid(zipcode,1,1)) + CInt(Mid(zipcode,2,1)) + _

CInt(Mid(zipcode,3,1)) + CInt(Mid(zipcode,4,1)) + CInt(Mid(zipcode,5,1)) + _

CInt(Mid(zipcode,7,1)) + CInt(Mid(zipcode,8,1)) + CInt(Mid(zipcode,9,1)) + _

CInt(Mid(zipcode,10,1))

Else

sumOfDigits = CInt(Mid(zipcode,1,1)) + CInt(Mid(zipcode,2,1)) + _

CInt(Mid(zipcode,3,1)) + CInt(Mid(zipcode,4,1)) + CInt(Mid(zipcode,5,1))

End If

' return checksum digit

If (sumOfDigits Mod 10) = 0 Then

CalculateCheckSum = 0

Else

CalculateCheckSum = 10 - (sumOfDigits Mod 10)

End If

End Function

' takes a digit and draws the corresponding POSTNET bars

Private Sub DrawDigitBars(digit)

Select Case digit

Case 1

DrawBar HalfBarHeight

DrawBar HalfBarHeight

DrawBar HalfBarHeight

DrawBar FullBarHeight

DrawBar FullBarHeight

Case 2

DrawBar HalfBarHeight

DrawBar HalfBarHeight

DrawBar FullBarHeight

DrawBar HalfBarHeight

DrawBar FullBarHeight

Case 3

DrawBar HalfBarHeight

DrawBar HalfBarHeight

DrawBar FullBarHeight

DrawBar FullBarHeight

DrawBar HalfBarHeight

Case 4

DrawBar HalfBarHeight

DrawBar FullBarHeight

DrawBar HalfBarHeight

DrawBar HalfBarHeight

DrawBar FullBarHeight

Case 5

DrawBar HalfBarHeight

DrawBar FullBarHeight

DrawBar HalfBarHeight

DrawBar FullBarHeight

DrawBar HalfBarHeight

Case 6

DrawBar HalfBarHeight

DrawBar FullBarHeight

DrawBar FullBarHeight

DrawBar HalfBarHeight

DrawBar HalfBarHeight

Case 7

DrawBar FullBarHeight

DrawBar HalfBarHeight

DrawBar HalfBarHeight

DrawBar HalfBarHeight

DrawBar FullBarHeight

Case 8

DrawBar FullBarHeight

DrawBar HalfBarHeight

DrawBar HalfBarHeight

DrawBar FullBarHeight

DrawBar HalfBarHeight

Case 9

DrawBar FullBarHeight

DrawBar HalfBarHeight

DrawBar FullBarHeight

DrawBar HalfBarHeight

DrawBar HalfBarHeight

Case 0

DrawBar FullBarHeight

DrawBar FullBarHeight

DrawBar HalfBarHeight

DrawBar HalfBarHeight

DrawBar HalfBarHeight

Case Else

' raise error

Err.Raise vbObjectError + mintExceptionBase + 9, _

"DrawDigitBars Sub-PDFPOSTNETBarCodes Class: Invalid digit."

End Select

End Sub

' draws a single bar of the given height

Private Sub DrawBar(height)

mobjPDFLib.moveto CurrentX, mintOffsetY

mobjPDFLib.lineto CurrentX, mintOffsetY + height

mobjPDFLib.stroke

CurrentX = CurrentX + BarSpacing

End Sub

End Class

%>


Created 2005-04-24, Last Modified 2011-07-24, © Shailesh N. Humbad
Disclaimer: This content is provided as-is. The information may be incorrect.