You are viewing limited content. For full access, please sign in.

Question

Question

create dynamic barcode stamp

asked on July 14, 2017

Dear all,

 

i am using the script in the sdk documentation, and creating a barcode stamp using external dll, after puting the stamp ( programatically ) on the entry, the stamp is applied with no image.

this is the code that i am using.

 
             dim barcde as new Barcode()
              dim imge as Image
             imge =  barcde.Encode(BarcodeLib.TYPE.CODE39Extended,"123456",Color.Black,Color.White,250,90)

'            Dim ms = new MemoryStream()
'            imge.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp) ' Use appropriate format here
'            Dim bytes = ms.ToArray()


                imge.Save("C:\Temp\img121222.bmp", ImageFormat.Bmp)



                Dim file As New System.IO.FileStream("C:\Temp\img11222.bmp", System.IO.FileMode.Open)

            Dim length As Integer = CInt(file.Seek(0, System.IO.SeekOrigin.[End]))
'             RepositoryAccess expects image data without a bitmap file's header information.
'             A bitmap's first 14 bytes contains header information.
            length -= 14

            Dim img As Byte() = New Byte(length - 1) {}


            msgbox("file done")
            '' Create a new StampInfo instance and assign stamp properties.
            '
            '
            '
            '

            Dim si As new StampInfo
            si.Name = "Stamp12f213"+GetTokenValue("Entry ID")
            msgbox("entryid")
            si.ImageData = img

            msgbox("entryid1")
            si.Session=me.Connection
            si.Save()
            Dim PT As LfPoint
            dim rect as LfRectangle
            rect.Width= 500
            rect.Height=300

            pt.X = 600
            pt.Y = 0
            dim SA as new StampAnnotation
            sa.StampId = si.id
            sa.Opacity = 100
            sa.CustomData("12344")
            ' position of stamp on the page.
            SA.Color = LfColor.YELLOW
            SA.Comment = "maher wel stamps"
            SA.Coordinates = rect
            SA.Position = PT

            msgbox("stamp created")
            Dim PI As PageInfo = Document.GetPageInfo(GetTokenValue("Entry ID"),1,me.Connection)

            PI.AddAnnotation(SA)
            msgbox("applied stamp")

after investigating, i have found that i have to save the bmp as Monochrome, but i couldn't do that through sdk.

Please advice,

Maher.

0 0

Answer

SELECTED ANSWER
replied on July 14, 2017 Show version history

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.

4 0

Replies

replied on July 14, 2017 Show version history

Maher,

When creating a stamp programmatically you will need to either create the image as a monochrome bitmap (not sure if your library can do this), or convert it, which can be done with the .NET libraries.

The client performs a conversion on import, but in the SDK your writing the data directly, so to convert it, you're looking for something like this once you have your barcode image:

System.Drawing.Imaging.BitmapData bmpData = barcodeBmp.LockBits(new System.Drawing.Rectangle(0, 0, barcodeBmp.Width, barcodeBmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
Bitmap newBitmap = new Bitmap(width, height, bmpData.Stride, System.Drawing.Imaging.PixelFormat.Format1bppIndexed, bmpData.Scan0);

Basically, you're taking the bmp drawn with the external barcode library, then drawing the pixels into the monochrome bitmap (newBitmap) that LF will accept as a stamp image.

From there, you can get the bytes to pass over to StampInfo.ImageData

I think the key is System.Drawing.Imaging.PixelFormat.Format1bppIndexed

0 0
replied on July 18, 2017

Hi Bert,

 

Thank you for your code.

Is it possible to have a simple full template (workflow) please?

0 0
replied on July 18, 2017

I do not understand what you are asking for.

0 0
replied on July 19, 2017

Thank you to reply me.

it's about your code above. It's a script, right? How do you launch it? Do you use workflow for that?

0 0
replied on July 20, 2017 Show version history

Yes, the code above is for use in a Workflow SDK script activity.

Once you add the SDK script activity, you open it and paste the code from above into it.  Then you must change the token name in line 3 ( "RetrieveFieldValues_BC1" ) to match your token with the text to put in the barcode.

Then you can test and modify lines 111, 113, 115, and 126 to adjust how the stamp is placed on the page.

If you need to have the stamp rotated, after line 118, add the following line and set it's value to 0, 90, 180, or 270

BCStampAnnotation.Rotation = 0

EDIT:

I changed the original code to include the rotate option and I moved all the  editable settings to the top of the code in lines 12 through 29.

1 0
replied on July 21, 2017

Thank you Bert!

This is awesome.

0 0
replied on October 21, 2020 Show version history

Hi Bert,

How are you ?

I don't have the lib System.Drawing.Imaging.

How can I install it?

 

My bad.

I have it.

Thanks you

0 0
replied on September 10, 2023

hello,

I tried the above code to create a barcode stamp but the stamp showing the value  (for example *1234*) on the page instead of the bars, I have all the references and the barcode font "Free 3 of 9" installed as well, am I missing something?

 thanks,

 

0 0
replied on September 11, 2023 Show version history

To find out what Font name to use in Line 27 of my code

Open Notepad and select your barcode font (Format > Font).  The name listed in Notepad is the name you need to use in line 27.

In the above sample, the line 27 would read:

Dim sFontName As String = "39 Tall Text"

 

0 0
replied on September 11, 2023

Hi Bert,

thank you for help,

this is exactly what I did but the barcode still not showing, look at the attached image below,

any suggestion please?

barcode stamp.png
0 0
replied on September 17, 2023

Hi Bert,

 

any suggestion on my post above please?

 

thanks,

 

0 0
replied on September 18, 2023

Again, all I can tell you is that if it is not using the font, it is not finding the font and then falling back to the default font.

Did you ensure that the font is installed on the Workflow Server?

1 0
replied on September 19, 2023

You've probably done this but if not, in your code can you set the font to Lucida Caligraphy, just to make sure your code is reading the font library?

 

0 0
replied on September 12, 2023

All I can tell you is that if it is not using the font, it is not finding the font and then falling back to the default font.

The font is installed on the Workflow server?

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.