You have to remember that creating the image will be a little slow, but here is a complete script you can use. It is loading and using a locally installed font "Free 3 of 9". With your Barcode DLL, you can remove and replace the code that generates the barcode image and just pass your barcode image into the conversion section of the script.
'Write your code here. The BoundEntryInfo property will access the entry, RASession will get the Repository Access session
' Add References to:
' System.Drawing
' System.Runtime.InteropServices
' Add Import statements for:
' Imports System.Drawing
' Imports System.Drawing.Imaging
' Imports System.Runtime.InteropServices
' ********************************************
' **********Edit stamp settings here**********
' ********************************************
' Set Stamp Name
Dim sStampName as String = "OPG3BarcodeStamp"
' Set page to apply stamp to
Dim iPageNumber As Integer = 1
' Set Stamp distance from left
Dim iStampLeft As Integer = 50
' Set Stamp distance from top
Dim iStampTop As Integer = 10
' Set Stamp Rotation - Use 0, 90, 180, or 270
Dim iStampRotation as Integer = 0
' Create Scaling factor - the larger this number the larger the stamp gets
Dim fScale As Single = 1.5F ' Use format of #.#F (1.0F = no scale)
' Load Barcode value (replace RetrieveFieldValues_BC1 with your Token name)
Dim sBarcodeValue As String = GetTokenValue("RetrieveFieldValues_BC1").ToString()
' Set Barcode Font name
Dim sFontName As String = "Free 3 of 9"
' Set Font Size - Use Format #.#F
Dim fFontSize as Single = 20.0F
' ********************************************
' ********Do not edit below this point********
' ********************************************
If BoundEntryInfo.EntryType = EntryType.Document Then
Try
Using CurrentDoc as DocumentInfo = DirectCast(BoundEntryInfo, DocumentInfo)
' Create bitmap object for stamp image
Dim BarCodeStampImage As Bitmap
' Create bitmap image
Dim bmp As New Bitmap(1000, 1000)
bmp.SetResolution(300, 300)
' FromImage method creates a new Graphics from the specified Image.
Dim grfx As Graphics = Graphics.FromImage(bmp)
' Load Barcode value (use * as start and end for 3 of 9)
Dim sBarcodeText As String = "*" & sBarcodeValue & "*"
' Create 3 of 9 font
Using fnt As Font = new Font(sFontName, fFontSize, FontStyle.Regular)
' Measure to get required size
Dim stringSize As SizeF = grfx.MeasureString(sBarcodeText, fnt)
Dim iProjWidth As Integer = Convert.ToInt32(stringSize.Width)
' Instantiating object of Bitmap image again with the correct size for the text and font.
bmp = New Bitmap(bmp, iProjWidth, Convert.ToInt32(stringSize.Height))
bmp.SetResolution(300, 300)
' Grab Graphics object from Bitmap
grfx = Graphics.FromImage(bmp)
' Make white background
grfx.FillRectangle(New SolidBrush(Color.White), 0, 0, bmp.Width, bmp.Height)
' Draw Specified text with specified format
grfx.DrawString(sBarcodeText, fnt, Brushes.Black, 0, 0)
End Using
' Clean up Graphic object
grfx.Flush()
grfx.Dispose()
' Lock source bitmap in memory
Dim sourceData As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.[ReadOnly], PixelFormat.Format32bppArgb)
' Copy image data to binary array
Dim imageSize As Integer = sourceData.Stride * sourceData.Height
Dim sourceBuffer As Byte() = New Byte(imageSize - 1) {}
Marshal.Copy(sourceData.Scan0, sourceBuffer, 0, imageSize)
' Unlock source bitmap
bmp.UnlockBits(sourceData)
' Create Black and White Stamp bitmap
BarCodeStampImage = New Bitmap(bmp.Width, bmp.Height, PixelFormat.Format1bppIndexed)
BarCodeStampImage.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution)
' Lock Stamp bitmap in memory
Dim destinationData As BitmapData = BarCodeStampImage.LockBits(New Rectangle(0, 0, BarCodeStampImage.Width, BarCodeStampImage.Height), ImageLockMode.[WriteOnly], PixelFormat.Format1bppIndexed)
' Create destination buffer
imageSize = destinationData.Stride * destinationData.Height
Dim destinationBuffer As Byte() = New Byte(imageSize - 1) {}
' Create objects for converting bmp
Dim sourceIndex As Integer = 0
Dim destinationIndex As Integer = 0
Dim pixelTotal As Integer = 0
Dim destinationValue As Byte = 0
Dim pixelValue As Integer = 128
Dim height As Integer = bmp.Height
Dim width As Integer = bmp.Width
Dim threshold As Integer = 500
' Iterate lines
For y As Integer = 0 To height - 1
sourceIndex = y * sourceData.Stride
destinationIndex = y * destinationData.Stride
destinationValue = 0
pixelValue = 128
' Iterate pixels
For x As Integer = 0 To width - 1
' Compute pixel brightness (i.e. total of Red, Green, and Blue values) - Thanks murx
' B G R
pixelTotal = Convert.ToInt32(sourceBuffer(sourceIndex)) + Convert.ToInt32(sourceBuffer(sourceIndex + 1)) + Convert.ToInt32(sourceBuffer(sourceIndex + 2))
If pixelTotal > threshold Then
destinationValue += CByte(pixelValue)
End If
If pixelValue = 1 Then
destinationBuffer(destinationIndex) = destinationValue
destinationIndex += 1
destinationValue = 0
pixelValue = 128
Else
pixelValue >>= 1
End If
sourceIndex += 4
Next
If pixelValue <> 128 Then
destinationBuffer(destinationIndex) = destinationValue
End If
Next
' Copy binary image data to Stamp bitmap
Marshal.Copy(destinationBuffer, 0, destinationData.Scan0, imageSize)
' Unlock destination bitmap
BarCodeStampImage.UnlockBits(destinationData)
' Dispose of Color Bitmap
bmp.Dispose()
' Create Byte arrey to hold Stamp Bitmap
Dim SourceByteArray() As Byte
' Load Stamp image into Stream
Using memStream As System.IO.MemoryStream = New System.IO.MemoryStream
BarCodeStampImage.Save(memStream, System.Drawing.Imaging.ImageFormat.Bmp)
SourceByteArray = memStream.ToArray()
End Using
' Dispose of Stamp Bitmap
BarCodeStampImage.Dispose()
' RepositoryAccess expects image data without a bitmap file's header information.
' A bitmap's first 14 bytes contains header information.
Dim StampBytes(SourceByteArray.Length - 15) As Byte
Array.Copy(SourceByteArray, 14, StampBytes, 0, StampBytes.Length - 1)
' Create new stamp and load image into it
Dim BCStamp As StampInfo = New StampInfo(RASession)
BCStamp.Name = sStampName
BCStamp.ImageData = StampBytes
BCStamp.UpdateImageData()
' Lock entry before applying Stamp
CurrentDoc.Lock(LockType.Exclusive)
' Do not allow negative left value
If iStampLeft < 0 Then iStampLeft = 0
' Do not allow negative top value
If iStampTop < 0 Then iStampTop = 0
' Do not accept Page number less than 1
if iPageNumber < 1 Then iPageNumber = 1
if iPageNumber > CurrentDoc.PageCount Then iPageNumber = CurrentDoc.PageCount
' Create Annotation object on page
Dim BCStampAnnotation As New StampAnnotation(CurrentDoc, iPageNumber, BCStamp)
BCStampAnnotation.Position = New Laserfiche.RepositoryAccess.Common.LfPoint(iStampLeft, iStampTop)
' Set Stamp Rotation
BCStampAnnotation.Rotation = iStampRotation
' Set Stamp color
BCStampAnnotation.Color = Laserfiche.RepositoryAccess.Common.LfColor.BLACK
' ********************************************
' Use the below section if you need to scale up your stamp
' Comment out the section below to skip scaling up the stamp
' ********************************************
'Do not allow negative scaling
If fScale < 1.0F then fScale = 1.0F
Dim StampRect As Laserfiche.RepositoryAccess.Common.LfRectangle = BCStampAnnotation.Coordinates
StampRect.Inflate(StampRect.Width * fScale, StampRect.Height * fScale)
StampRect.Location = New Laserfiche.RepositoryAccess.Common.LfPoint(iStampLeft, iStampTop)
BCStampAnnotation.Coordinates = StampRect
' ********************************************
' Apply Stamp to page
CurrentDoc.GetPageInfo(iPageNumber).AddAnnotation(BCStampAnnotation)
CurrentDoc.Save()
CurrentDoc.Unlock()
End Using
Catch Ex As Exception
WorkflowApi.TrackError(Ex)
End Try
End If
Edit: I Modified the code and placed all the Settings variables at the top of the code in lines 12 through 29.