2016-05-24 425 views
0

我正試圖在我的RDL報表中嵌入條形碼,該報表由Microsoft SQL Server Report Builder 3.0開發。將條形碼添加到報表生成器3.0

我發現夫婦在網上建議的解決方案,其中之一是增加一個DLL參考,並添加一些代碼,但它沒有工作,它總是無法加載DLL,我發現了另一個解決方案通過使用API作爲圖像的來源,但這對我來說不是一個可行的解決方案,因爲我並不總是在我的服務器上連接互聯網。

有沒有辦法在我的報告中使用Code 128字體?或者其他任何不需要互聯網連接的解決方案?

+0

我已經有這個字體的運氣:http://www.squaregear.net/fonts/free3of9.shtml –

回答

3

經過大量的研究,我設法在我的報告中嵌入條形碼,並使用Code 128字體運行它。

不過,既然你需要先準備文本,以便條形碼才能進行掃描本身的字體是不夠的,以下解決方案不需要在客戶端計算機上安裝的字體,你只需要安裝在服務器上:

  1. 首先,你需要安裝Code 128字體,如果你沒有了!您可以從here下載它。

  2. 打開報告或創建一個新報告。

  3. 我們需要添加System.DrawingDLL文件作爲參考:

    這可以通過轉到報表屬性來完成(右鍵單擊體外 - >報告屬性),點擊參考文獻 tab,點擊添加按鈕添加或刪除程序集部分,點擊打開按鈕... 瀏覽C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll文件,然後單擊打開

    你應該有這樣的事情:

    enter image description here


  • 現在,我們需要一些自定義代碼添加到報告:
  • 轉至代碼選項卡中的Repor牛逼屬性,複製粘貼下面的代碼,然後單擊確定

    Function StringToBarcode(value As String) As String 
        Dim charPos, minCharPos As Integer 
        Dim currentChar, checksum As Integer 
        Dim isTableB As Boolean = True, isValid As Boolean = True 
        Dim returnValue As String = String.Empty 
    
        If (value Is Nothing OrElse value.Length = 0) Then 
        Return String.Empty 
        End If 
    
        'Check for valid characters 
        For charCount As Integer = 0 To value.Length - 1 
        currentChar = Asc(value.Substring(charCount, 1)) 
        If (Not (currentChar >= 32 AndAlso currentChar <= 126)) Then 
         isValid = False 
         Exit For 
        End If 
        Next 
    
        If Not (isValid) Then Return returnValue 
        charPos = 0 
        While (charPos < value.Length) 
        If (isTableB) Then 
         'See if interesting to switch to table C 
         'yes for 4 digits at start or end, else if 6 digits 
         If (charPos = 0 OrElse charPos + 4 = value.Length) Then 
         minCharPos = 4 
         Else 
         minCharPos = 6 
         End If 
         minCharPos = IsNumber(value, charPos, minCharPos) 
    
         If (minCharPos < 0) Then 
         'Choice table C 
         If (charPos = 0) Then 
         'Starting with table C 
         'char.ConvertFromUtf32(205) 
          returnValue = Chr(205).ToString() 
         Else 
          'Switch to table C 
          returnValue = returnValue + Chr(199).ToString() 
         End If 
         isTableB = False 
         Else 
         If (charPos = 0) Then 
          'Starting with table B 
          returnValue = Chr(204).ToString() 
          'char.ConvertFromUtf32(204); 
         End If 
         End If 
        End If 
    
        If (Not isTableB) Then 
         'We are on table C, try to process 2 digits 
         minCharPos = 2 
         minCharPos = IsNumber(value, charPos, minCharPos) 
    
         If (minCharPos < 0) Then 
         'OK for 2 digits, process it 
         currentChar = Integer.Parse(value.Substring(charPos, 2)) 
         If (currentChar < 95) Then 
          currentChar = currentChar + 32 
         Else 
          currentChar = currentChar + 100 
         End If 
         returnValue = returnValue + Chr(currentChar).ToString() 
         charPos += 2 
         Else 
         'We haven't 2 digits, switch to table B 
         returnValue = returnValue + Chr(200).ToString() 
         isTableB = True 
         End If 
        End If 
    
        If (isTableB) Then 
         'Process 1 digit with table B 
         returnValue = returnValue + value.Substring(charPos, 1) 
         charPos += 1 
        End If 
    
        End While 
    
        'Calculation of the checksum 
        checksum = 0 
        Dim loo As Integer 
        For loo = 0 To returnValue.Length - 1 
        currentChar = Asc(returnValue.Substring(loo, 1)) 
        If (currentChar < 127) Then 
         currentChar = currentChar - 32 
        Else 
         currentChar = currentChar - 100 
        End If 
        If (loo = 0) Then 
         checksum = currentChar 
        Else 
         checksum = (checksum + (loo * currentChar)) Mod 103 
        End If 
        Next 
    
        'Calculation of the checksum ASCII code 
        If (checksum < 95) Then 
        checksum = checksum + 32 
        Else 
        checksum = checksum + 100 
        End If 
    
        ' Add the checksum and the STOP 
        returnValue = returnValue + _ 
        Chr(checksum).ToString() + _ 
        Chr(206).ToString() 
    
        Return returnValue 
    End Function 
    
    Function IsNumber(InputValue As String, CharPos As Integer, MinCharPos As Integer) As Integer 
        MinCharPos -= 1 
        If (CharPos + MinCharPos < InputValue.Length) Then 
        While (MinCharPos >= 0) 
         If (Asc(InputValue.Substring(CharPos + MinCharPos, 1)) < 48 _ 
         OrElse Asc(InputValue.Substring(CharPos + MinCharPos, 1)) > 57) Then 
         Exit While 
        End If 
        MinCharPos -= 1 
        End While 
        End If 
        Return MinCharPos 
    End Function 
    
    Public Function Code128(ByVal stringText As String) As Byte() 
        Dim result As Byte() = Nothing 
    
        Try 
        result = GenerateImage("Code 128", StringToBarcode(stringText)) 
        Catch ex As Exception 
        End Try 
    
        Return result 
    End Function 
    
    Public 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), 30) 
        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 
    

  • 在報告中插入圖像,然後選擇數據庫作爲圖像源,並且使用作爲image/png MIMI 類型

    enter image description here


  • 點擊FX按鈕使用此字段按鈕,並使用該函數=Code.Code128(Fields!your_field_name.Value),按確定確定

    enter image description here


  • 運行報表,現在你有一個條形碼:)

    enter image description here

  • 0

    這裏有一些非常複雜的解決方案,但我可以通過使用字體來實現。確保您在值的開始和結束時有星號。如果尚未安裝,則可能需要在機器上安裝字體。我在Visual Studio中使用了SSDT,但報表生成器也應該可以工作。如果遇到特定問題或錯誤,請發佈更新。

    相關問題