将条形码添加到 Report Builder 3.0
Adding Barcode to Report Builder 3.0
我正在尝试在我的 RDL 报告中嵌入条形码,该报告由 Microsoft SQL Server Report Builder 3.0.
开发
我在互联网上找到了几个建议的解决方案,其中之一是添加一个 DLL
引用并添加一些代码,但它不起作用并且总是无法加载 DLL
, 我通过使用 API 作为图像源找到了另一种解决方案,但这对我来说不是一个可行的解决方案,因为我的服务器并不总是有互联网连接。
有没有办法在我的报告中使用 Code 128
字体?或者任何其他不需要互联网连接的解决方案?
有一些非常复杂的解决方案,但我仅使用一种字体就能够做到这一点。确保在值的开头和结尾有星号。如果尚未在计算机上安装该字体,则可能需要安装它。我在 Visual Studio 中使用了 SSDT,但 Report Builder 应该也可以。如果您 运行 遇到特定问题或错误,请 post 进行更新。
经过大量研究,我设法在我的报告中嵌入了条形码,并使用 Code 128
字体使其 运行。
但是,字体本身是不够的因为您需要先准备文本才能扫描条形码,以下解决方案不需要安装字体客户端机器,你只需要在服务器上安装它:
首先你需要安装 Code 128
字体,如果你还没有的话!您可以从 here.
下载
打开您的报告或创建一个新报告。
我们需要添加System.Drawing
DLL
文件作为参考:
这可以通过转到报表属性来完成(在正文外右键单击 -> 报表属性 ), 单击 References 选项卡,单击 Add 按钮 Add or remove程序集部分,单击“打开”按钮...
浏览 C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll
文件,然后单击 打开 。
你应该有这样的东西:
- 现在我们需要向报告中添加一些自定义代码:
转到报表属性中的代码选项卡,复制并粘贴以下代码,然后单击确定:
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
在你的报表中插入一张图片,select数据库作为图片来源,使用image/png
作为MIMI类型:
单击fx按钮使用此字段按钮,并使用此功能 =Code.Code128(Fields!your_field_name.Value)
,按 Ok 和 Ok。
运行 报告,现在你有一个条形码:)
我正在尝试在我的 RDL 报告中嵌入条形码,该报告由 Microsoft SQL Server Report Builder 3.0.
开发我在互联网上找到了几个建议的解决方案,其中之一是添加一个 DLL
引用并添加一些代码,但它不起作用并且总是无法加载 DLL
, 我通过使用 API 作为图像源找到了另一种解决方案,但这对我来说不是一个可行的解决方案,因为我的服务器并不总是有互联网连接。
有没有办法在我的报告中使用 Code 128
字体?或者任何其他不需要互联网连接的解决方案?
有一些非常复杂的解决方案,但我仅使用一种字体就能够做到这一点。确保在值的开头和结尾有星号。如果尚未在计算机上安装该字体,则可能需要安装它。我在 Visual Studio 中使用了 SSDT,但 Report Builder 应该也可以。如果您 运行 遇到特定问题或错误,请 post 进行更新。
经过大量研究,我设法在我的报告中嵌入了条形码,并使用 Code 128
字体使其 运行。
但是,字体本身是不够的因为您需要先准备文本才能扫描条形码,以下解决方案不需要安装字体客户端机器,你只需要在服务器上安装它:
首先你需要安装
Code 128
字体,如果你还没有的话!您可以从 here. 下载
打开您的报告或创建一个新报告。
我们需要添加
System.Drawing
DLL
文件作为参考:这可以通过转到报表属性来完成(在正文外右键单击 -> 报表属性 ), 单击 References 选项卡,单击 Add 按钮 Add or remove程序集部分,单击“打开”按钮... 浏览
C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll
文件,然后单击 打开 。你应该有这样的东西:
- 现在我们需要向报告中添加一些自定义代码:
转到报表属性中的代码选项卡,复制并粘贴以下代码,然后单击确定:
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
在你的报表中插入一张图片,select数据库作为图片来源,使用
image/png
作为MIMI类型:
单击fx按钮使用此字段按钮,并使用此功能
=Code.Code128(Fields!your_field_name.Value)
,按 Ok 和 Ok。
运行 报告,现在你有一个条形码:)