The code below can be downloaded as a ready to use, editable ASP script here
The "Option Explicit" Statement near the top of the script makes it necessary to delcare all variable names, this is useful because without it if you mistype a variable name your script will not execute and will warn you that your mis spelled variable name is undeclared. Without it your script would execute, but with unexpected results. Suppose, for example that some vital piece of data is stored in a variable called "theData" but due to a typo part of your code is testing a variable called "theDate". Since theDate contains no data your calculations will be wrong or your logic flawed and it could take you hours to find out where the problem lies.
<% @ LANGUAGE = VBScript %> <% Option Explicit %> <html> <head> <title>ActiveX Asp Test</title> </head> <body> <div align="center"> <table width="500" border="0"> <tr> <td> <h1 align="center"><font size="5" color="#000099">TAL Bar Code ActiveX Plus <br> - Active Server Page Example</font></h1> </td> </tr> <tr> <td> <h2><font face="Arial" size="3" color="#009999">The sample application below was designed to demonstrate how to use the TAL Bar Code ActiveX control (Plus version) with an Active Server Page.</font></h2> </td> </tr> </table></div> <% 'If the form has not yet been submitted the content lenth will be 0 and this code will be skipped over to display the form, otherwise it will try to generate the barcode If Request.ServerVariables("Content_Length") <> 0 then
Visitors to the page will use the HTML form to record information about the barcode they wish to generate, for example the message to be encoded, the symbology, and the height of the bars.
This ASP Page will look at the form data, set the Properties of the ActiveX control to those specified on the form, then generate a barcode image .PNG or .GIF (depending on the Browser since not all browsers suport .PNG files). This Barcode Image must be saved on the Server, so we shall specify a folder in which to save it. The filename has to be different for every visitor or one visitor's barcode will be overwritten by the next. To this end we shall use an incrementing filename: "1.png", "2.png", etc.
In order for the Server to "remember" the last filename it created, we must store it somewhere on the server, so we shall create a text file to store the latest file number used and then read the last number from the text file and add one to it when generating the next barcode.
Here we create a constant representing the maximum number of image files to store on server. This should exceed expected number of concurrent users or one user could be overwriting another's image. We shall use this number when incrementing the filename: If the last filename (number) is the same as our maximum number of files, then reset the numbering to 1, otherwise increment the number. 'Note: Since the ActiveX supports several image file types you should multiply the maximum by the number of file types you offer in your code. Here we are offering 2 types (.png and .gif). If one user creates 1.png and then the next time through a user creates 1.gif then the original #1 file will not be replaced, so eventually you could end up with twice as many files as you expected. Of course, you can easily add more code to delete all #1 files before generating a new one:
Const maxFiles = 10 Const nTwipsPerInch = 1440 Const lngPixelsPerInch = 96 'Windows always assumes there are 96 Pixels per inch regardless of screen resolution 'Folder to store barcode images Const ImgDir = "c:\webshare\wwwroot\" 'File to store current file number info Const txtFileNum = "c:\webshare\wwwroot\fnum.txt" Const ForReading = 1, ForWriting=2, ForAppending = 8 'Input/Output constants Const TriStateUseDefault = -2 'opens the file using the system default Const TriStateTrue = -1 'Opens the file as Unicode Const TristateFalse = 0 'Opens the file as Ascii 'declare variables (The "Option Explicit" Statement at the top expects all variable names to be declared, this is useful because if you mistype a variable name your code will not run, and tell you why, otherwise it would run but you may get unexpected results and spend a long time trying to find out why its not working. dim myBarcode dim dTime dim strMessage, strComment, lngSymbology,lngBarHeight, strFileType Dim filenum Dim pxlWidth, pxlHeight Dim objFS, objTextS
The following function is required because we intend to insert a Raster style image into the resulting web page. The dimensions of a Raster image are dependent upon the dot resolution of the device used to display it. For example a graphic that is 300 pixels wide and has 300 rows of pixels displayed on a printer that has a dot resolution of 300 dots per inch will be one inch square. But if you display the same image on a computer screen that has a dot resolution of approx 100 dots per inch, you end up with an image that is three inches square. When we Query the ActiveX control to find out it's height and Width the returning information is in "Twips". In order to Set the Height and Width Properties of the tag, we need this information to be converted into Pixels.
Function ConvertTwipsToPixels(lngTwips) ConvertTwipsToPixels = (lngTwips * lngPixelsPerInch)/ nTwipsPerInch End Function 'The Following functions allow reading and writing to a text file on the server Function GetFileNum() Set objFS = Server.CreateObject("Scripting.FileSystemObject") 'open and read file number from file Set objTextS = objFS.OpenTextFile(txtFileNum, ForReading, TriStateFalse) Filenum = objTextS.Readline objTextS.Close 'Since the filenumber is stored as a string we must convert it to an integer, otherwise when we try to add "1" to the number the ASP interpreter will assume we are mistaken because text cannot be treated as a number and will ignore our request. Getfilenum = cInt(filenum) 'cInt is a built in function 'destroy objects Set objFS = nothing Set objTextS = nothing End Function Function SetFileNum(newFileNumber) Set objFS = Server.CreateObject("Scripting.FileSystemObject") 'Write the new file number to the text file Set objTextS = objFS.OpenTextFile(txtFileNum, ForWriting, TriStateFalse) objTextS.WriteLine newFileNumber objTextS.Close SetFileNum = 1 Set objFS = nothing Set objTextS = nothing End Function 'Read last file number from the file by calling the GetFileNum function above. filenum = GetFileNum 'if last file number was less than maximum number of files, increment the number, otherwise reset it to overwrite file #1 If filenum < maxFiles then filenum = filenum + 1 Else filenum = 1 End If 'Write the new file number to the text file ready for the next pass SetFileNum filenum 'Get the form information from the server strMessage = Trim(Request.Form("txtMessage")) strComment = Trim(Request.Form("txtComment")) lngSymbology = Trim(Request.Form("symbology")) strFileType = Trim(Request.Form("cboImageFormat")) 'get the current Server time dTime = Time 'Create an "instance" of TAL Active X control Set myBarcode = Server.CreateObject("TALBarCd.TALBarCd.1") 'set some barcode properties with data from the form myBarcode.Message = strMessage myBarcode.Comment = strComment myBarcode.Symbology = lngSymbology 'set other properties by "hard coding" the values if you wish myBarcode.BarHeight = 1000 myBarcode.NarrowBarWidth = 13 myBarcode.ForeColor = "0" myBarcode.BackColor = "16777215" 'Note: Properties that you do not set will use their default values
Next we save barcode in chosen directory in a format suitable for the browser (The form uses JavaScript to evaluate the browser, then writes the most suitable filetype to a "hidden" field in the form called "Imagetype". When we requested the form information from the server "Imagetype" was saved into a new variable called "strFileType". Below we concatenate 3 variables together in order to save the barcode.
Since our ImgDir is stored in a constant above as "c:\webshare\wwwroot\", filenum will be a number between 1 and 10 (since 10 is the maximum number of files we wish to store on the server) and strFileType will be either "PNG" or "GIF". The "&" character simply joins the three variables together in order to create a single string, for example "c:\webshare\wwwroot\6.png"
myBarcode.SaveBarCode ImgDir & filenum & "." & strFileType 'Since our ImgDir is stored in a constant above as "c:\webshare\wwwroot\", filenum will be a number between 1 and 10 (since 10 is the maximum number of files we wish to store on the server) and strFileType will be either "PNG" or "GIF". The "&" character simply joins the three variables together in order to create a single string, for example ""c:\webshare\wwwroot\6.png" 'Get height and width for display in Browser '(these properties only exist on the plus version of the TAL active X control) pxlWidth = ConvertTwipsToPixels(myBarcode.Width) pxlHeight = ConvertTwipsToPixels(myBarcode.Height) 'reset objects to free memory resources on the server set myBarcode = nothing %> <div align="center"><!-- Print Server time --> This server's time is: <%= dTime %><br> <!-- Display barocode image --> <img src='<%= filenum & "." & strFileType & "' Height=" & pxlHeight & " Width=" & pxlWidth %>'></div> <% Else %> <!-- Display HTML Form --><form method="Post" name="BarCodeForm" Action="Barcode.asp"> <div align="center"> <table width="500" border=0 bgcolor="#efefe7"> <tbody> <tr> <td align=right width="50%" bgcolor=#efefe7><small>Barcode message:</small></td> <td width="50%" bgcolor=#efefe7> <input tabindex=1 size=12 value=12345678901 name="txtMessage"> </td> </tr> <tr> <td align=right width="50%"><small>Comment:</small></td> <td width="50%"> <input tabindex=2 size=12 name="txtComment"> </td> </tr> <tr> <td align=right width="50%" bgcolor=#efefe7><small>Comment Alignment:</small></td> <td width="50%" bgcolor=#efefe7> <select tabindex=3 size=1 name="commentalignment"> <option value=0 selected>Left Align</option> <option value=1>Center Align</option> <option value=2>Right Align</option> </select> </td> </tr> <tr> <td align=right width="50%"><small>Bar Height (mils):</small></td> <td width="50%"> <input tabindex=4 size=12 value=1000 name="barheight"> </td> </tr> <tr> <td align=right width="50%" bgcolor=#efefe7><small>Narrow Bar Width (mils):</small></td> <td width="50%" bgcolor=#efefe7> <input tabindex=5 size=12 value=13 name="narrowbarwidth"> </td> </tr> <tr> <td align=right width="50%"><small>Bar Code Symbology:</small></td> <td width="50%"> <select tabindex=6 size=1 name="symbology"> <option value=0 selected>Code 39</option> <option value=1>Full ASCII 39</option> <option value=2>Code 39 HIBC</option> <option value=3>Codabar</option> <option value=4>Code 93</option> <option value=5>Code 128</option> <option value=6>UCC/EAN 128</option> <option value=7>Interleaved 2 of 5</option> <option value=8>PostNET</option> <option value=9>UPC - A</option> <option value=10>UPC - E</option> <option value=11>EAN - 8</option> <option value=12>EAN - 13</option> <option value=13>Bookland</option> <option value=14>MSI/Plessey</option> <option value=15>PDF417</option> <option value=16>Aztec Code</option> <option value=17>Data Matrix</option> <option value=18>MaxiCode</option> </select> </td> </tr> <tr> <td align=right width="50%" bgcolor=#efefe7><small>Rotation:</small></td> <td width="50%" bgcolor=#efefe7> <select tabindex=7 size=1 name="rotation"> <option value=0 selected>0 degrees</option> <option value=1>90 degrees</option> <option value=2>180 degrees</option> <option value=3>270 degrees</option> </select> </td> </tr> <tr> <td align=right width="50%"><small>Foreground Color:</small></td> <td width="50%"> <select tabindex=8 size=1 name="forecolor"> <option value=0 selected>Black</option> <option value=255>Red</option> <option value=65280>Green</option> <option value=16711680>Blue</option> </select> </td> </tr> <tr> <td align=right width="50%" bgcolor=#efefe7><small>Background Color:</small></td> <td width="50%" bgcolor=#efefe7> <select tabindex=9 size=1 name="backcolor"> <option value=16777215 selected>White</option> <option value=255>Red</option> <option value=65280>Green</option> <option value=16711680>Blue</option> </select> </td> </tr> <tr> <td align=right width="50%" bgcolor=#efefe7>Image Format:</td> <td width="50%" bgcolor=#efefe7> <select name="cboImageFormat"> <option value=".png">PNG</option> <option value=".gif" selected>GIF</option> </select> </td> </tr> <tr> <td valign=top align=right width="50%" height=44> <div align="right"> <input tabindex=10 type="checkbox" CHECKED value=ON name="quietzones"> <small>Include Quiet Zones</small></div> </td> <td valign=top width="50%" height=44> <input language=JavaScript tabindex=11 type="checkbox" value=ON name="bearerbars"> <small>Include Bearer Bars</small></td> </tr> <tr> <td valign=top align=right colspan="2" height=44> <div align="center"> <input type=hidden name=scrHeight> <input type=hidden name=scrWidth> <input type=hidden name="Browser"> <input type=hidden name="Version"> <input tabindex=12 type="submit" value="Submit" name="Submit"> </div> </td> </tr> </tbody> </table></div> </FORM> <% End If %></BODY></HTML>