報表中的 barcode使用

by adonis 12. 十一月 2012 16:01

作    者:楊先民
審    稿:張智凱

前言

之前在上課被問過一個有關於條碼如何顯示在 Reporting Service上
的問題,剛好我對這方面的問題也很有興趣,所以稍微搜尋了一下發現其實在網路上已經有免費的範例,所以就稍微說明一下有關在 Reporting Service上顯示 barcode的方式,也算是做個記錄,因為事實上很多相關 Reporting Service的資訊與資源,只是沒有相關的整理,本期就來談談 barcode條碼吧!

條碼(barcode)的用途

條碼主要是用來做「商業自動化」之用,條碼的構成是由長方形黑線條,以及空白線平行組合而成,並加上檢查碼的字元群相互平行配置。在條碼開頭的字元以及結束的字元之後,都要留下空白,以做為安全空間,這樣的配置就是所謂的條碼符號。

若有多種商品需要記錄,最簡單的方式就是用條碼機製作條碼(有專門做條碼貼紙的條碼機),把商品代號轉換成條碼格式,這樣利用光學掃描儀器可以快速讀取商品資訊,以利做庫存管理(快遞業者則是包裹追蹤碼轉換成條碼格式,以利追蹤貸品送貨進度)。

image 條碼範例

條碼(barcode)的種類
條碼有分不同的種類,簡單介紹如下,詳細內容請參考各大條碼網站

a.EAN碼:European Article Number的縮寫,EAN 碼共有13位數字,由0~9所組成,這些數字中有國碼、廠商號碼、產品編號及檢查碼。因此商品條碼可以說是任何國、任何廠商、以及任何商品獨一無二的 「 商品身分證統一號」,也可說是品流於國際市場中一種通行無阻的「共通語言 」。我國於1996年取得 EAN 會員國資格,以「471」為國家代碼。

image 取自中華民國商品條碼策進會

b. UPC 碼:Universal Product Code的縮寫,即國際產品碼,是1973年美國所制訂的,與 EAN 碼同樣為目前全世界利用最廣、且統一規格的條碼系統。UPC 碼僅有 12 位數字,且彼此的資料內容排列順序不同。每個字由四個直線條所組成,其中兩個是暗線條,兩個明線條。UPC 碼的縮短碼為 7 位數字, EAN 碼則為 8 位數字。

c.39 碼(Code 39):
39 碼(Code 39)為 1975 年美國 INTERMAKE 公司所發表的,比起 EAN/UPC 碼, 39 碼廣泛應用在製造業。它可代表 26 個英文字母、數字、及特殊符號,並且可增加到美國資訊交換標準碼(ASC)所定的 128 個字。39 碼是由九個直條所組成,其中五個是暗線條,四個明線條;三個寬條、六個窄條。39 碼雙向可讀,且是包含的文字數字最多之條碼系統。

d.Code 128 barcode (Code 128 條碼):
Code 128條碼是一種長度可變、連續性的字母數位元條碼。其應用領域非常廣泛,包括製造業的生產流程控制、批發物流業或運輸業的倉儲管理、車輛調配、貨物追蹤、醫院血液樣本的管理、政府對管制藥品的控制追蹤等。

在 Reporting Service中顯示條碼

那麼,如何在 Reporting Service中顯示條碼呢?

首先,你要先取得「條碼字型」,有些是要付費的,例如你可以搜尋EAN條碼字型,就有 DEMO版可以下載,但某些字元印不出來(請買正版),而我們這邊的範例所使用的是 Code 39的字型(從http://www.barcodesinc.com/free-barcode-font/)可以下載,你必需先安裝起來,如下圖:

image 
這時在 c:\windows\fonts目錄下才有 Code 39的字型,如下圖:

image

這樣就可以開始在你的報表中加入程式碼:

有兩種方式可以顯示,一種是用 Textbox的方式,需要把字型改成 Code 39,另一種則是使用 image的控制項顯示圖型,不過根據國外網站報導,使用 TextBox的方式,轉譯成PDF會出現問題,所以還是選擇轉成圖型的方式吧!

先在報表 Code區填入下面的程式碼:
    Private Shared Function IsNumber(ByVal InputValue As String, ByVal CharPos As Integer, ByVal MinCharPos As Integer) As Integer
        ' if the MinCharPos characters from CharPos are numeric, then MinCharPos = -1
        MinCharPos -= 1
        If CharPos + MinCharPos < InputValue.Length Then
            While MinCharPos >= 0
                If AscW(Char.Parse(InputValue.Substring(CharPos + MinCharPos, 1))) < 48 OrElse AscW(Char.Parse(InputValue.Substring(CharPos + MinCharPos, 1))) > 57 Then
                    Exit While
                End If
                MinCharPos -= 1
            End While
        End If
        Return MinCharPos
    End Function
    Public Shared Function StringToBarcode39String(ByVal value As String, Optional ByVal addChecksum As Boolean = False) As String
        ' Parameters : a string
        ' Return     : a string which give the bar code when it is dispayed with CODE128.TTF font
        '              : an empty string if the supplied parameter is no good
        Dim isValid As Boolean = True
        Dim currentChar As Char
        Dim returnValue As String = String.Empty
        Dim checksum As Integer = 0
        If value.Length > 0 Then
            'Check for valid characters
            For CharPos As Integer = 0 To value.Length - 1
                currentChar = Char.Parse(value.Substring(CharPos, 1))
                If Not ((currentChar >= "0"c AndAlso currentChar <= "9"c) OrElse (currentChar >= "A"c AndAlso currentChar <= "Z"c) OrElse currentChar = " "c OrElse currentChar = "-"c OrElse currentChar = "."c OrElse currentChar = "$"c OrElse currentChar = "/"c OrElse currentChar = "+"c OrElse currentChar = "%"c) Then
                    isValid = False
                    Exit For
                End If
            Next
            If isValid Then
                ' Add start char
                returnValue = "*"
                ' Add other chars, and calc checksum
                For CharPos As Integer = 0 To value.Length - 1
                    currentChar = Char.Parse(value.Substring(CharPos, 1))
                    returnValue += currentChar.ToString()
                    If currentChar >= "0"c AndAlso currentChar <= "9"c Then
                        checksum = checksum + AscW(currentChar) - 48
                    ElseIf currentChar >= "A"c AndAlso currentChar <= "Z"c Then
                        checksum = checksum + AscW(currentChar) - 55
                    Else
                        Select Case currentChar
                            Case "-"c
                                checksum = checksum + AscW(currentChar) - 9
                                Exit Select
                            Case "."c
                                checksum = checksum + AscW(currentChar) - 9
                                Exit Select
                            Case "$"c
                                checksum = checksum + AscW(currentChar) + 3
                                Exit Select
                            Case "/"c
                                checksum = checksum + AscW(currentChar) - 7
                                Exit Select
                            Case "+"c
                                checksum = checksum + AscW(currentChar) - 2
                                Exit Select
                            Case "%"c
                                checksum = checksum + AscW(currentChar) + 5
                                Exit Select
                            Case " "c
                                checksum = checksum + AscW(currentChar) + 6
                                Exit Select
                        End Select
                    End If
                Next
                ' Calculation of the checksum ASCII code
                If addChecksum Then
                    checksum = checksum Mod 43
                    If checksum >= 0 AndAlso checksum <= 9 Then
                        returnValue += (ChrW(checksum + 48)).ToString()
                    ElseIf checksum >= 10 AndAlso checksum <= 35 Then
                        returnValue += (ChrW(checksum + 55)).ToString()
                    Else
                        Select Case checksum
                            Case 36
                                returnValue += "-"
                                Exit Select
                            Case 37
                                returnValue += "."
                                Exit Select
                            Case 38
                                returnValue += " "
                                Exit Select
                            Case 39
                                returnValue += "$"
                                Exit Select
                            Case 40
                                returnValue += "/"
                                Exit Select
                            Case 41
                                returnValue += "+"
                                Exit Select
                            Case 42
                                returnValue += "%"
                                Exit Select
                        End Select
                    End If
                End If
                ' Add stop char
                returnValue += "*"
            End If
        End If
        Return returnValue
    End Function

Public Shared Function Code39(ByVal stringText As String) As Byte()
        Dim result As Byte() = Nothing
        Try
            result = GenerateImage("Code 3 de 9", StringToBarcode39String(stringText))
        Catch ex As Exception
        End Try
        Return result
    End Function

    Public Shared Function GenerateImage(ByVal fontName As String, ByVal stringText As String) As Byte()
        Dim oGraphics As System.Drawing.Graphics
        Dim barcodeSize As System.Drawing.SizeF
        Dim ms As System.IO.MemoryStream
        Using font As New System.Drawing.Font(New System.Drawing.FontFamily(fontName), 36)
            Using tmpBitmap As New System.Drawing.Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
                oGraphics = System.Drawing.Graphics.FromImage(tmpBitmap)
                oGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel
                barcodeSize = oGraphics.MeasureString(stringText, font)
                oGraphics.Dispose()
            End Using
            Using newBitmap As New System.Drawing.Bitmap(barcodeSize.Width, barcodeSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
                oGraphics = System.Drawing.Graphics.FromImage(newBitmap)
                oGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel
                Using oSolidBrushWhite As New System.Drawing.SolidBrush(System.Drawing.Color.White)
                    Using oSolidBrushBlack As New System.Drawing.SolidBrush(System.Drawing.Color.Black)
                        oGraphics.FillRectangle(oSolidBrushWhite, New System.Drawing.Rectangle(0, 0, barcodeSize.Width, barcodeSize.Height))
                        oGraphics.DrawString(stringText, font, oSolidBrushBlack, 0, 0)
                    End Using
                End Using
                ms = New System.IO.MemoryStream()
                newBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
            End Using
        End Using
        Return ms.ToArray()
    End Function

由於要產生圖片,所以最後會回傳 byte()陣列,接下來拖進一個 image控制項,如下圖:

image

其中,運算式的部分如下:
=Code.Code39("12345")

接下來,別忘了參照 System.Drawing進來,不然無法畫圖,如下圖:

image

最後,設計出來的結果如下(右邊為 TextBox的測試,但可以不理它)

image 
最後, preview出來的結果如下圖:

image

以上,就是示範用 Reporting Service顯示出條碼的方式,可以給各位一個參考。

image

Tags:

新增評論




  Country flag
biuquote
  • 評論
  • 線上預覽
Loading






NET Magazine國際中文電子雜誌

NET Magazine國際中文電子版雜誌,由恆逸資訊創立於2000,自發刊日起迄今已發行超過500篇.NET相關技術文章,擁有超過40000名註冊讀者群。NET Magazine國際中文電子版雜誌希望藉於電子雜誌與NET Developer達到共同學習與技術新知分享,歡迎每一位對.NET 技術有興趣的朋友們多多支持本雜誌,讓作者群們可以有持續性的動力繼續爬文。<請加入免費訂閱>

月分類Month List