Search code examples
vb.netreporting-servicesbarcodezxingssrs-2012

How to handle special characters in Code128 using zxing.net


I created a SSRS Report which contains a few Code128 Barcodes. The Barcodes are generated using the latest zxing.net library. I would like to include tabs (char(9)) in an Code128 Barcode. But it fail with the following exception message:

System.ArgumentException: Bad character in input:

Needless to say that it works like a charm without the tabulator character.

The GetBarCodeHorizontal is used in the report to generate the barcodes. However, for testing purpose i wrapped it into a visual studio vb project:

Class MainWindow
    Public Function GetBarCodeHorizontal(ByVal s As String, ByVal width As Integer) As Byte()
        Dim writer As New ZXing.BarcodeWriter()
        Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream()

        writer.Format = ZXing.BarcodeFormat.CODE_128
        writer.Options = New ZXing.Common.EncodingOptions
        writer.Options.Width = width
        writer.Options.Height = 60
        writer.Options.PureBarcode = False
        'writer.Options.Hints.Add(ZXing.EncodeHintType.CHARACTER_SET, "UTF-8")

        Dim bmp As System.Drawing.Bitmap = writer.Write(s)

        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
        Dim imagedata As Byte()
        imagedata = ms.GetBuffer()
        Return imagedata
    End Function


    Private Sub MainWindow_OnLoaded(sender As Object, e As RoutedEventArgs)
        Try
            Dim barCodeHorizontal = GetBarCodeHorizontal("3999999   80  1XXXXXX8    r1XX3", 200)
        Catch ex As Exception
            Console.WriteLine(ex)
        End Try
    End Sub
End Class

Questions:

  • How can i solve this problem?
  • Is this a limitation of the zxing library?
  • Is there any suitable workaround (or maybe even another library)?

Solution

  • I ended up with another (free) library which turns out to work very well.

    There is also a tutorial how to embed barcodes into SSRS for this specific library.

    For those of interest here is my code to create the barcodes:

     Public Function GetBarcode(ByVal text As String, ByVal barcodeWidth As Integer, ByVal barcodeHeight As Integer) As Byte()
            Dim b As System.Drawing.Bitmap
            Dim bar As New BarcodeLib.Barcode
            bar.Alignment = BarcodeLib.AlignmentPositions.CENTER
    
            bar.IncludeLabel = False
            b = bar.Encode(BarcodeLib.TYPE.CODE128, text, barcodeWidth, barcodeHeight)
            Dim bitmapData As Byte() = Nothing
            Using ms As New System.IO.MemoryStream()
                b.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
                bitmapData = ms.ToArray()
            End Using
            Return bitmapData
        End Function
    

    The barcode data comes directly from the query as shown here:

    SELECT        MilkrunID, Code, Quantity, Batch, PickLocation, Code + CHAR(9) + CAST(Quantity AS NVARCHAR(20)) + CHAR(9) + Batch + CHAR(9) + PickLocation AS Barcode
    FROM            Tbl_ReportData_ProductionReplenishment_MilkrunSummary
    

    char(9) creates a Tab.